class Group(db.Model): __tablename__ = 'group' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(512), nullable=False, unique=True) def __str__(self): return self.name def __repr__(self): return '<Group %r>' % self.name
class User(db.Model, UserMixin, MailMixin, ModelMixin): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(512), unique=True) email = db.Column(db.String(512), unique=True) password = db.Column(db.String(512), nullable=False) is_superuser = db.Column(db.Boolean, default=False) is_confirmed = db.Column(db.Boolean, default=False) register_time = db.Column(db.DateTime, default=datetime.now()) last_login = db.Column(db.DateTime, default=datetime.now()) groups = db.relationship(Group, secondary=user_group, lazy='dynamic', backref=db.backref('users', lazy='dynamic')) def __str__(self): return self.username def __repr__(self): return '<User %r>' % self.username def set_password(self, raw_password): self.password = generate_password_hash(raw_password) def check_password(self, raw_password): return check_password_hash(self.password, raw_password)
# Email: [email protected] # Created: 2016-12-07 13:12:42 (CST) # Last Update:星期三 2017-5-10 14:56:32 (CST) # By: # Description: # ************************************************************************** from datetime import datetime from flask_login import UserMixin from werkzeug.security import check_password_hash, generate_password_hash from flask_maple.mail import MailMixin from flask_maple.models import ModelMixin, db user_group = db.Table( 'user_group', db.Column('group_id', db.Integer, db.ForeignKey('group.id')), db.Column('user_id', db.Integer, db.ForeignKey('user.id'))) class Group(db.Model): __tablename__ = 'group' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(512), nullable=False, unique=True) def __str__(self): return self.name def __repr__(self): return '<Group %r>' % self.name
def last_login(cls): return db.Column(db.DateTime, default=datetime.now)
def register_time(cls): return db.Column(db.DateTime, default=datetime.now)
def is_confirmed(cls): return db.Column(db.Boolean, default=False)
def is_superuser(cls): return db.Column(db.Boolean, default=False)
def password(cls): return db.Column(db.String(512), nullable=False)
def code(cls): return db.Column(db.String(81), default='0000')
def username(cls): return db.Column(db.String(512), nullable=False, unique=True)
def id(cls): return db.Column(db.Integer, primary_key=True)
def parent_id(cls): return db.Column(db.Integer, db.ForeignKey('group.id'))
def description(cls): return db.Column(db.String(2048))
def resource_type(cls): return db.Column(db.String(81), nullable=False)
def resource(cls): return db.Column(db.String(1024), nullable=False)
def email(cls): return db.Column(db.String(512), nullable=False, unique=True)
# ************************************************************************** # Copyright © 2017 jianglin # File Name: models.py # Author: jianglin # Email: [email protected] # Created: 2017-08-29 16:47:32 (CST) # Last Update: 星期五 2018-02-23 22:59:49 (CST) # By: # Description: # ************************************************************************** from sqlalchemy.ext.declarative import declared_attr from flask_maple.models import ModelMixin, db user_permission = db.Table( 'user_permission', db.Column('user_id', db.Integer, db.ForeignKey('user.id')), db.Column('permission_id', db.Integer, db.ForeignKey('permission.id'))) group_permission = db.Table( 'group_permission', db.Column('group_id', db.Integer, db.ForeignKey('group.id')), db.Column('permission_id', db.Integer, db.ForeignKey('permission.id'))) class UserMixin(object): def perm_cache(self, action, resource, resource_type='endpoint', and_=False): return