Building The Model
First we’ll build the model that will handle the credential data of the registering users.
[models.py]
import datetime
from flask.ext.login import UserMixin
from peewee import *
DATABASE = SqliteDatabase('social.db')
class User(UserMixin, Model):
username = CharField(unique=True)
email = CharField(unique=True)
password = CharField(max_length=100)
joined_at = DateTimeField(default=datetime.datetime.now)
is_admin = BooleanField(default=False)
class Meta:
database = DATABASE
order_by = ('-joined_at',)
order_by = (‘-joined_at’,)
Orders registered users when listing them by the order of their joining date, new first descending to old.
Flask-Login
To use this great package, you first need to install it.
pip install flask-login
flask-login
A package that handles user sessions. For now we’re only going to use one of its functionalities: UserMixin.
UserMixin
Provides us with a few properties about the user’s login status that we will make use of later, and they are:
is_authenticated, is_active, is_anonymous, get_id()
from flask.ext.login import UserMixin
Imports UserMixin from the flask-login package, which in the Flask echo-system as all packages is installed to this ext module, which stands for External Area.
class User( UserMixin, Model ):
UserMixins must be inherited first, then the Model.