class UserMixin(object): """Time Stamped Mixin Attributes ---------- created_by : Integer Users/Admins Unique Identifier updated_by : Integer Users/Admins Unique Identifier """ created_by = db.Column(db.Integer, nullable=False) updated_by = db.Column(db.Integer, nullable=True)
class BaseMixin(object): """Time Stamped Mixin Attributes ---------- id : Integer Autoincrement unique identifier """ id = db.Column(db.Integer, primary_key=True, autoincrement=True)
class Role(db.Model, BaseMixin, TimestampMixin, UserMixin): """Permission ORM ... Attributes ---------- __tablename__ : String Table Name """ __tablename__ = 'roles' name = db.Column(db.String(), unique=True, nullable=False) uti = db.Column(db.String(), unique=True, nullable=False) permissions = db.Column(db.ARRAY(JSONB)) user = db.relationship('User')
class Credential(db.Model, BaseMixin, TimestampMixin): """Credentials ORM ... Attributes ---------- __tablename__ : String Table Name user_id : Integer identity : String Login email or phone number password : String blocked : Boolean note : Text """ __tablename__ = 'credential' user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE')) #user = db.relationship('User', uselist=False)#, lazy = 'select', backref=db.backref('credential', lazy='joined')) identity = db.Column(db.String(), unique=True, nullable=False) password = db.Column(db.String(), unique=False, nullable=True) blocked = db.Column(db.Boolean(), default=False) note = db.Column(db.Text(), nullable=True)
class Permission(db.Model, BaseMixin, TimestampMixin, UserMixin): """Permission ORM ... Attributes ---------- __tablename__ : String Table Name """ __tablename__ = 'permissions' name = db.Column(db.String(), unique=True, nullable=False) key = db.Column(db.String(), unique=True, nullable=False) create = db.Column(db.Boolean(), default=False) read = db.Column(db.Boolean(), default=False) update = db.Column(db.Boolean(), default=False) delete = db.Column(db.Boolean(), default=False)
class User(db.Model, BaseMixin, UserMixin, TimestampMixin): """User ORM ... Attributes ---------- __tablename__ : String Table Name name: String User's full name, i.e first name and father name cell_phone_num : String(13) Example : - +251966751230 """ __tablename__ = 'user' __legal_characters = '^[a-zA-Z]+$' __phone_num_patterns = '^\+[0-9]+$' __name_pattern = '^[a-zA-Z]+\s[a-zA-Z]+\s?[a-zA-Z]*$' ACTIVE = True DEACTIVE = False name = db.Column(db.String(), nullable=False) email = db.Column(db.String()) #, unique = True, nullable = True) pnum = db.Column(db.String(13)) #, unique = True, nullable = True) credential = db.relationship( 'Credential', uselist=False, backref="user", cascade='all, delete-orphan' ) #, lazy = 'select', backref=db.backref('credential', lazy='joined', uselist=False)) #role = db.relationship('UserRole', uselist=False, back_populates='user') role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) role = db.relationship('Role', back_populates='user', lazy='select') #@validates('name') #def validate_name(self, key, value): # if not re.match(self.__name_pattern, value): # raise ValueError('Name cannot contain illegal characters VALUE => %s' % value) # return value @validates('email') def validate_email(self, key, value): if not value.strip(): return "" if not '@' in value or not value.strip(): raise ValueError( 'Email Address must contain @ symbol or cannot be empty VALUE => %s' % value) else: return value @validates('pnum') def validate_phone_num(self, key, value): if not value.strip(): return None elif not re.match(self.__phone_num_patterns, value) or len(value) != 13: raise ValueError( 'Phone Number cannot contain illegal characters, or must be 13 characters VALUE => %s' % value) else: return value