class Product(db.Model): __tablename__ = "product" barcode = db.Column(db.String(100), primary_key=True) price = db.Column(db.Float) name = db.Column(db.String(100)) description = db.Column(db.String(300)) date = db.Column(db.String(100)) in_stock = db.Column(db.Integer) discontinued = db.Column(db.Boolean) selling_price = db.Column(db.Float) category_name = db.Column(db.String(100), db.ForeignKey("category.name"), nullable=False)
class Category(db.Model): __tablename__ = "category" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True) products = db.relationship("Product", backref="categories", lazy=True, cascade="all, delete") def insert(self): """Save category to the database""" db.session.add(self) db.session.commit() def update(self): """Update category""" db.session.commit() def delete(self): """delete category""" db.session.delete(self) db.session.commit() @classmethod def category_exists(cls, name): return db.session.query(exists().where(cls.name == name)).scalar()
class User(UserMixin, db.Model): """ User model """ __tablename__ = "users" id = db.Column(db.String(10), primary_key=True) username = db.Column(db.String(100), unique=True) password = db.Column(db.String(128)) admin_user = db.Column(db.Boolean, default=False) roles = db.relationship("Role", secondary=role_helpers, cascade="all, delete") def __init__(self, **kwargs): super(User, self).__init__(**kwargs) self.id = str(uuid4()) def set_hash(self, password): self.password = generate_password_hash(password, method="sha256") def check_hash(self, password): return check_password_hash(self.password, password) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit() def __repr__(self): return f'User({self.username!r})'
class Role(db.Model): __tablename__ = "roles" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Settings(db.Model): __tablename__ = "settings" setting = db.Column(db.String(100), primary_key=True) value = db.Column(db.String(100))