class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(42), unique=True) password = db.Column(db.String(256)) def __init__(self, **kwargs): super().__init__(**kwargs) self.password = User.hash_password(self.password) def validate(self, password: str) -> bool: return User.verify_password(self.password, password) def hash_password(password): """Hash a password for storing.""" salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii') pwdhash = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'), salt, 100000) pwdhash = binascii.hexlify(pwdhash) return (salt + pwdhash).decode('ascii') def verify_password(stored_password, provided_password): """Verify a stored password against one provided by user""" salt = stored_password[:64] stored_password = stored_password[64:] pwdhash = hashlib.pbkdf2_hmac('sha512', provided_password.encode('utf-8'), salt.encode('ascii'), 100000) pwdhash = binascii.hexlify(pwdhash).decode('ascii') return pwdhash == stored_password
class Products(db.Model, SerializerMixin): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(140)) price = db.Column(db.Numeric()) description = db.Column(db.Text) supliers = db.Column(db.String(140)) sku = db.Column(db.String(150)) def __init__(self, **kwargs): super(Products, self).__init__(**kwargs) self.name = kwargs['name'] self.price = kwargs['price'] self.description = kwargs['description'] self.supliers = kwargs['supliers'] self.sku = kwargs['sku']
class Employee(db.Model): __tablename__ = 'employee' id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) created_at = db.Column(db.DateTime, nullable=False, default=db.func.now()) updated_at = db.Column(db.DateTime, nullable=False, default=db.func.now(), onupdate=db.func.now()) full_name = db.Column(db.String(64), nullable=False, index=True) departament_id = db.Column(UUID(as_uuid=True), db.ForeignKey('departament.id'), nullable=False) have_dependents = db.Column(db.Boolean(), nullable=False) dependent = db.relationship(Dependent, backref='employee', lazy='dynamic', cascade='all, delete-orphan') def __init__(self, data): self.full_name = data['full_name'] self.departament_id = data['departament_id'] self.have_dependents = data['have_dependents'] def __repr__(self): return f'<Employee {self.id}>'
class User(UserMixin, db.Model): __tablename__ = 'User' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) password_hash = db.Column(db.String(128)) about_me = db.Column(db.String(140)) last_seen = db.Column(db.DateTime, default=datetime.utcnow) token = db.Column(db.String(32), index=True, unique=True) token_expiration = db.Column(db.DateTime) def __repr__(self) -> str: return f'<User | username: {self.username}, id: {self.id}, \ email: {self.email}>' def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password)
class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) # User Authentication fields email = db.Column(db.String(255), nullable=False, unique=True) email_confirmed_at = db.Column(db.DateTime()) username = db.Column(db.String(50), nullable=False, unique=True) password = db.Column(db.String(255), nullable=False) # User information first_name = db.Column(db.String(100, collation='NOCASE'), nullable=False, server_default='') last_name = db.Column(db.String(100, collation='NOCASE'), nullable=False, server_default='') # Define the relationship to Role via UserRoles roles = db.relationship('Role', secondary='user_roles') def is_enabled(self): return True
class Departament(db.Model): __tablename__ = 'departament' id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) created_at = db.Column(db.DateTime, nullable=False, default=db.func.now()) updated_at = db.Column(db.DateTime, nullable=False, default=db.func.now(), onupdate=db.func.now()) name = db.Column(db.String(64), nullable=False, unique=True, index=True) employee = db.relationship(Employee, backref='departament', lazy='dynamic') def __init__(self, data): self.name = data['name'] def __repr__(self): return f'<Departament {self.id}>'
class Link(db.Model): __tablename__ = 'links' id = db.Column(db.Integer, primary_key=True) url = db.Column(db.String(512)) slug = db.Column(db.String(5), unique=True) visits = db.Column(db.Integer, default=0) date_created = db.Column(db.DateTime, default=datetime.now) def __init__(self, **kwargs): super().__init__(**kwargs) self.slug = self.generate_slug() def generate_slug(self): """Generate a random 5 characters slug""" chars = string.digits + string.ascii_letters slug = ''.join(choices(chars, k=5)) link = self.query.filter_by(slug=slug).first() if link: return self.generate_slug() return slug
class UserModels(db.Model): __tablename__ = "user" id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(50), unique=True, nullable=False) password = db.Column(db.Text(), nullable=False) created_at = db.Column(db.DateTime, default=datetime.utcnow) updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) def to_entity(self): return User( id=self.id, username=self.username, password=self.password, created_at=self.created_at, )
class Dependent(db.Model): __tablename__ = 'dependent' id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) created_at = db.Column(db.DateTime, nullable=False, default=db.func.now()) updated_at = db.Column(db.DateTime, nullable=False, default=db.func.now(), onupdate=db.func.now()) full_name = db.Column(db.String(64), nullable=False, index=True) employee_id = db.Column(UUID(as_uuid=True), db.ForeignKey('employee.id'), nullable=False) def __init__(self, data): self.full_name = data['full_name'] self.employee_id = data['employee_id'] def __repr__(self): return f'<Dependent {self.id}>'
class User(db.Model, SerializerMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(140)) password = db.Column(db.String(512))
class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(50), unique=True)