class User(BaseModel): """ User Model for storing user related details """ __tablename__ = "ETL_USER" id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(50), unique=True) password_hash = db.Column(db.String(100)) email = db.Column(db.String(255), unique=True, nullable=False) created_time = db.Column(db.DateTime, nullable=False, default=datetime.now()) roles = db.relationship('Role', secondary=roles, cascade="save-update, merge, delete", lazy='subquery', backref=db.backref('users', lazy=True)) @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 as_dict(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns if c.name != 'password'} def __repr__(self): return "<User(id='%d', username='******', email='%s')>" % (self.id, self.username, self.email)
class Role(BaseModel): """ User Model for storing user related details """ __tablename__ = "ETL_ROLE" id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255), unique=True, nullable=False) description = db.Column(db.String(100), unique=True) created_time = db.Column(db.DateTime, nullable=False, default=datetime.now()) def __repr__(self): return "<Role(id='%d', name='%s')>" % (self.id, self.name)
class User(Base, UserMixin): email = db.Column(db.String, unique=True) password = db.Column(db.String(255)) active = db.Column(db.Boolean) created = db.Column(db.DateTime, default=datetime.utcnow) roles = db.relationship('Role', secondary='user_role_assoc', backref='users') def __repr__(self): return '<User(%s, %s)>' % (self.id, self.email) def __unicode__(self): return self.email
class Base(db.Model): __abstract__ = True id = db.Column(db.Integer, primary_key=True) @declared_attr def __tablename__(cls): return cls.__name__.lower()
class RevokeToken(db.Model): __tablename__ = "ETL_REVOKE_TOKEN" id = db.Column(db.Integer, primary_key=True) jti = db.Column(db.String(36), nullable=False) token_type = db.Column(db.String(10), nullable=False) user_identity = db.Column(db.String(50), nullable=False) revoked = db.Column(db.Boolean, nullable=False) expires = db.Column(db.DateTime, nullable=False) def to_dict(self): return { 'token_id': self.id, 'jti': self.jti, 'token_type': self.token_type, 'user_identity': self.user_identity, 'revoked': self.revoked, 'expires': self.expires }
class UserRoleAssoc(db.Model): __tablename__ = 'user_role_assoc' user_id = db.Column(db.ForeignKey('user.id'), primary_key=True) role_id = db.Column(db.ForeignKey('role.id'), primary_key=True)
class Role(Base, RoleMixin): name = db.Column(db.String) description = db.Column(db.String) def __repr__(self): return '<Role(%s, %s)>' % (self.id, self.name)
import flask_bcrypt from datetime import datetime from starter import db from model.base_model import BaseModel roles = db.Table('ETL_USER_ROLE', db.Column('USER_ID', db.Integer, db.ForeignKey('ETL_USER.id'), primary_key=True), db.Column('ROLE_ID', db.Integer, db.ForeignKey('ETL_ROLE.id'), primary_key=True)) class User(BaseModel): """ User Model for storing user related details """ __tablename__ = "ETL_USER" id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(50), unique=True) password_hash = db.Column(db.String(100)) email = db.Column(db.String(255), unique=True, nullable=False) created_time = db.Column(db.DateTime, nullable=False, default=datetime.now()) roles = db.relationship('Role', secondary=roles, cascade="save-update, merge, delete", lazy='subquery', backref=db.backref('users', lazy=True)) @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):