mirror of
https://github.com/wassname/flask-bootstrap.git
synced 2026-09-11 12:10:40 +08:00
Initial commit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
"""
|
||||
Module to provide plug-and-play authentication support for SQLAlchemy.
|
||||
"""
|
||||
|
||||
import datetime
|
||||
from sqlalchemy import Column, Integer, String, DateTime
|
||||
from flaskext.auth import AuthUser, get_current_user_data
|
||||
|
||||
def get_user_class(declarative_base):
|
||||
"""
|
||||
Factory function to create an SQLAlchemy User model with a declarative
|
||||
base (for example db.Model from the Flask-SQLAlchemy extension).
|
||||
"""
|
||||
class User(declarative_base, AuthUser):
|
||||
"""
|
||||
Implementation of User for SQLAlchemy.
|
||||
"""
|
||||
id = Column(Integer, primary_key=True)
|
||||
username = Column(String(80), unique=True, nullable=False)
|
||||
password = Column(String(120), nullable=False)
|
||||
salt = Column(String(80))
|
||||
role = Column(String(80))
|
||||
title = Column(String(50))
|
||||
email = Column(String(50))
|
||||
first_name = Column(String(50))
|
||||
middle_initial = Column(String(50))
|
||||
last_name = Column(String(50))
|
||||
created = Column(DateTime(), default=datetime.datetime.utcnow)
|
||||
modified = Column(DateTime())
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(User, self).__init__(*args, **kwargs)
|
||||
password = kwargs.get('password')
|
||||
if password is not None and not self.id:
|
||||
self.created = datetime.datetime.utcnow()
|
||||
# Initialize and encrypt password before first save.
|
||||
self.set_and_encrypt_password(password)
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user