class Employee(db.Model): __tablename__ = 'employee' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) type = db.Column(db.String(50)) __mapper_args__ = { 'polymorphic_identity': 'employee', 'polymorphic_on': type } def __repr__(self): return f'Employee {self.name}'
class BlacklistToken(db.Model): """ Token Model for storing JWT tokens """ __tablename__ = 'blacklist_tokens' id = db.Column(db.Integer, primary_key=True, autoincrement=True) token = db.Column(db.String(500), unique=True, nullable=False) blacklisted_on = db.Column(db.DateTime, nullable=False) def __init__(self, token): self.token = token self.blacklisted_on = datetime.datetime.now() def __repr__(self): return '<id: token: {}'.format(self.token) @staticmethod def check_blacklist(auth_token): # check whether auth token has been blacklisted res = BlacklistToken.query.filter_by(token=str(auth_token)).first() if res: return True else: return False
class Address(db.Model): """ User Model for storing user related details """ __tablename__ = "address" id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255), unique=True, nullable=False) users = db.relationship('User', secondary='user_address')
class Phone(db.Model): """ User Model for storing user related details """ __tablename__ = "phone" id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255), unique=True, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
class Engineer(Employee): __tablename__ = 'engineer' id = db.Column(db.Integer, db.ForeignKey('employee.id'), primary_key=True) engineer_name = db.Column(db.String(30)) __mapper_args__ = { 'polymorphic_identity': 'engineer', } def __repr__(self): return f'Engineer {self.engineer_name}'
class Boss(Employee): __tablename__ = 'boss' id = db.Column(db.Integer, db.ForeignKey('employee.id'), primary_key=True) boss_name = db.Column(db.String(30)) __mapper_args__ = { 'polymorphic_identity': 'boss', } def __repr__(self): return f'Boss {self.boss_name}'
class TreeDO(BaseDO): __tablename__ = "test_tree" # 类目英文名称 english_name: str = db.Column(db.String(64), unique=True) # 类目中文名称 chinese_name: str = db.Column(db.String(64), index=True) # 父类目id parent_id: int = db.Column(db.Integer) # 类目层级 tree_level: int = db.Column(db.Integer) # 是否叶子节点 leaf: bool = db.Column(db.Boolean, default=False) # 是否是有效类目 valid: bool = db.Column(db.Boolean, default=False)
class Category(db.Model): __tablename__ = 'category' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) children = db.relationship( 'Category', secondary=category_tree, primaryjoin=(category_tree.c.parent_id == id), secondaryjoin=(category_tree.c.children_id == id), backref=db.backref('parents', lazy='dynamic'), lazy='dynamic' ) def __repr__(self): return '<Category> {}'.format(self.id) # a child has a list of parents # a parent has a list of children
class User(db.Model, SQLAlchemyEmailMixin, SQLAlchemyPasswordMixin): """ User Model for storing user related details """ __tablename__ = "user" id = db.Column(db.Integer, primary_key=True, autoincrement=True) email = db.Column(db.String(255), unique=True, nullable=False) registered_on = db.Column(db.DateTime, nullable=False) admin = db.Column(db.Boolean, nullable=False, default=False) public_id = db.Column(db.String(100), unique=True) username = db.Column(db.String(50), unique=True) password_hash = db.Column(db.String(100)) addresses = db.relationship('Address', secondary='user_address') phones = db.relationship(Phone, backref='user', lazy='dynamic') @property def password(self): raise AttributeError('password: write-only field') @password.setter def password(self, password): self.password_hash = flask_bcrypt.generate_password_hash( password).decode('utf-8') def check_password(self, password): return flask_bcrypt.check_password_hash(self.password_hash, password) def encode_auth_token(self, user_id): """ Generates the Auth Token :return: string """ try: payload = { 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=1, seconds=5), 'iat': datetime.datetime.utcnow(), 'sub': user_id } return jwt.encode(payload, key, algorithm='HS256') except Exception as e: return e @staticmethod def decode_auth_token(auth_token): """ Decodes the auth token :param auth_token: :return: integer|string """ try: payload = jwt.decode(auth_token, key) is_blacklisted_token = BlacklistToken.check_blacklist(auth_token) if is_blacklisted_token: return 'Token blacklisted. Please log in again.' else: return payload['sub'] except jwt.ExpiredSignatureError: return 'Signature expired. Please log in again.' except jwt.InvalidTokenError: return 'Invalid token. Please log in again.' def __repr__(self): return "<User '{}'>".format(self.username)
from enum import Enum, unique from instance import db from base.model import BaseDO class ${className}DO(BaseDO): [#if dataSourceName??] __bind_key__ = "${dataSourceName}" [/#if] __tablename__ = "${table.sqlName}" [#list table.columns as field] # ${(field.remarks)!} [#if field.asType=='String'] ${field.sqlName} = db.Column(db.String(64)) [/#if] [#if field.asType=='Long'] ${field.sqlName} = db.Column(db.BigInteger) [/#if] [#if field.asType=='Integer'] ${field.sqlName} = db.Column(db.Integer) [/#if] [#if field.asType=='Boolean'] ${field.sqlName} = db.Column(db.Boolean, default=False) [/#if] [#if field.asType=='Enum'] ${field.sqlName} = db.Column(db.SmallInteger) [/#if] [#if field.asType=='Date'] ${field.sqlName} = db.Column(db.DateTime)