Source

flask-auth / flaskext / auth / models / mongo.py

Full commit
import datetime
from flaskext.auth import AuthUser, get_current_user_data

def now():
    return datetime.datetime.utcnow()

def get_user_class(db):
    class User(db.Document, AuthUser):
        """
        Implementation of User for SQLAlchemy.
        """
        username = db.StringField()
        password = db.StringField()
        salt = db.StringField()
        role = db.StringField(required=False)
        created = db.DateTimeField(default=now())
        modified = db.DateTimeField(default=now())

        def save(self):
            self.modified = now()
            super(User, self).save()

        def __getstate__(self):
            return {
                'id': self.id,
                'username': self.username,
                'role': self.role,
                'created': self.created,
                'modified': self.modified,
            }

        @classmethod
        def load_current_user(cls, apply_timeout=True):
            data = get_current_user_data(apply_timeout)
            if not data:
                return None
            return cls.query.filter(cls.username==data['username']).one()

    return User