class Products(db.Model): """Products table""" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), nullable=False) price = db.Column(db.Integer, nullable=False) discount = db.Column(db.Integer) stock = db.Column(db.Integer, nullable=False) image = db.Column(db.String(128), unique=True, nullable=False) category_id = db.Column(db.Integer, db.ForeignKey('categories.id'), nullable=False) sub_category_id = db.Column(db.Integer, db.ForeignKey('subcategories.id'), nullable=False) cart = db.relationship('Cart', backref='product', lazy=True) def __repr__(self): return self.name @property def image_filepath(self): """To access the product image filepath""" return images.path(self.image) def get_percent_saved(self): amount_saved = self.price - self.discount return round(amount_saved / self.price * 100) def get_humanize_price(self): return humanize.intcomma(self.price) def get_humanize_discount(self): return humanize.intcomma(self.discount)
class Calendar(db.Model): id = db.Column(db.Integer, primary_key=True) dag = db.relationship("Dag", backref="calendar", lazy=True) user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False) def __repr__(self): return str(self.user_id)
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') password = db.Column(db.String(60), nullable=False) sell = db.relationship('Sell', backref='author', lazy=True) def get_reset_token(self, expires_sec=1800): s = Serializer(app.config['SECRET_KEY'], expires_sec) return s.dumps({'user_id': self.id}).decode('utf-8') @staticmethod def verify_reset_token(token): s = Serializer(app.config['SECRET_KEY']) try: user_id = s.loads(token)['user_id'] except: return None return User.query.get(user_id) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class Category(db.Model): id= db.Column(db.Integer, primary_key=True) name= db.Column(db.String(20), nullable=False) products= db.relationship('Product', backref='category', lazy=True) def __repr__(self): return f"Category('{self.name}')"
class Subcategories(db.Model): """Sub Categories table""" id = db.Column(db.Integer, primary_key=True) sub_category = db.Column(db.String(50), unique=True, nullable=False) products = db.relationship('Products', backref='subcategory', lazy=True) def __repr__(self): return self.sub_category
class Roles(db.Model): """Roles table""" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True, nullable=False) users = db.relationship('Users', backref='role', lazy=True) def __repr__(self): return self.name
class Users(UserMixin, db.Model): """Users table""" id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(50), nullable=False) last_name = db.Column(db.String(50), nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) email_confirmed = db.Column(db.Boolean(), nullable=False, default=0) email_confirmed_at = db.Column(db.DateTime()) is_active = db.Column(db.Boolean(), nullable=False, default=1) password = db.Column(db.String(300), nullable=False) role_id = db.Column(db.Integer, db.ForeignKey('roles.id'), nullable=False, default=1) cart = db.relationship('Cart', backref='user', lazy=True) orders = db.relationship('Orders', backref='user', lazy=True) def __repr__(self): return f'{self.first_name} {self.last_name}'
class Dag(db.Model): id = db.Column(db.Integer, primary_key=True) date = db.Column(db.Integer, default=datetime.now().isoweekday()) begivenhed = db.relationship("Begivenhed", backref="dag", lazy=True) calendar_id = db.Column(db.Integer, db.ForeignKey("calendar.id"), nullable=False) def __repr__(self): return str(self.calendar_id)
class Order(db.Model): id= db.Column(db.Integer, primary_key=True) user_id= db.Column(db.Integer, db.ForeignKey('user.id')) order_number= db.Column(db.Integer, nullable=False) items= db.relationship('OrderItem', backref='order', lazy=True) paid= db.Column(db.Boolean, default=False) fulfilled= db.Column(db.Boolean, default=False) cancelled= db.Column(db.Boolean, default=False) err_msg= db.Column(db.String(60), nullable=True) err_log= db.Column(db.PickleType, nullable=True) date_placed = db.Column(db.DateTime, index=True, default=datetime.utcnow)
class Orders(db.Model): id = db.Column(db.Integer, primary_key=True) order_code = db.Column(db.Integer, nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) complete = db.Column(db.Boolean, default=False) ordered_date = db.Column(db.DateTime, nullable=True) products = db.relationship('Cart', secondary=cart_orders, lazy='subquery', backref=db.backref('cart', lazy=True)) def __repr__(self): return f'Product: {self.product_id}'
class Main_item(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) company = db.Column(db.String(100), nullable=False) country = db.Column(db.String(100), nullable=False) distributer = db.Column(db.String(100), nullable=False) family = db.Column(db.String(100), nullable=False) # buying_price = db.Column(db.Float, nullable=False) selling_price = db.Column(db.Float, nullable=False) # total_quantity = db.Column(db.Integer, nullable=False) current_quantity = db.Column(db.Integer, nullable=False) risk_quantity = db.Column(db.Integer, nullable=False) ran_out = db.Column(db.Boolean, default=False) place = db.Column(db.String(50), nullable=False) bought_items = db.relationship('BoughtItem', backref='item', lazy=True) sold_items = db.relationship('SoldItem', backref='item', lazy=True) def __repr__(self): return f"{self.name}"
class Users(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String, unique=True) password = db.Column(db.String) username = db.Column(db.String, unique=True) admin = db.Column(db.Boolean, default=False) calendar = db.relationship("Calendar", backref="user", lazy=True) def __repr__(self): return self.email def getPassword(self): return self.password def isAdmin(self): return self.admin