class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(30), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(255), nullable=False) admin = db.Column(db.Boolean, nullable=False, default=False) addresses = db.relationship("Address", backref="user", lazy=True) forget_tokens = db.relationship("ResetPasswordToken", backref="user", lazy=True) cards = db.relationship("Card", backref="user", lazy=True) product_rating = db.relationship("ProductRating", backref="user", lazy=True) active = db.Column(db.Boolean, nullable=False, default=True) cart = db.relationship("UserCart", uselist=False, back_populates="user") def __init__(self, username, email, password, admin=False): self.username = username self.email = email sha_1 = hashlib.sha1() sha_1.update(password.encode()) self.password = sha_1.hexdigest() self.admin = admin def __repr__(self): return f"User('{self.username}', '{self.email}')"
class UserCart(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) user = db.relationship("User", back_populates="cart") items = db.relationship("CartItem", backref="cart", lazy=True) def __repr__(self): return f"UserCart('{self.id}','{self.total_price}','{self.user_id}'')"
class Product(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) retail_price = db.Column(db.Integer, nullable=False) description = db.Column(db.Text(500), nullable=False) stock = db.Column(db.Integer, nullable=False) pic_filename = db.Column(db.String(255), nullable=False) cart_item = db.relationship("CartItem", backref="product", lazy=True) product_rating = db.relationship("ProductRating", backref="product", lazy=True) ordered_product = db.relationship("OrderedProduct", backref="product", lazy=True) def __repr__(self): return f"Product('{self.name}', '{self.retail_price}', '{self.stock}')"
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(30), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(255), nullable=False) admin = db.Column(db.Boolean, nullable=False, default=False) enabled_2fa = db.Column(db.Boolean, nullable=False, default=False) email_verified = db.Column(db.Boolean, nullable=False, default=False) addresses = db.relationship("Address", backref="user", lazy=True) cards = db.relationship("Card", backref="user", lazy=True) product_rating = db.relationship("ProductRating", backref="user", lazy=True) active = db.Column(db.Boolean, nullable=False, default=True) locked = db.Column(db.Boolean, nullable=False, default=False) orders = db.relationship("Order", backref="user", lazy=True) user2fa = db.relationship("User2FA", backref="user", lazy=True) login_attempts = db.relationship("LoginAttempts", backref="user", lazy=True) failed_login_attempts_count = db.Column(db.Integer, nullable=False, default=0) cart = db.relationship("UserCart", uselist=False, back_populates="user") def __init__(self, username, email, password, admin=False, email_verified=False): self.username = username self.email = email self.password = bcrypt.generate_password_hash(password).decode("utf-8") self.admin = admin self.email_verified = email_verified def __repr__(self): return f"User('{self.username}', '{self.email}')"
class Order(db.Model): id = db.Column(db.Integer, primary_key=True) order_date = db.Column(db.String(100), nullable=False) total_price = db.Column(db.DECIMAL, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) items = db.relationship("OrderedProduct", backref="order", lazy=True) tracking_no = db.Column(db.String(30), nullable=True, default=None) def __repr__(self): return f"Order('{self.id}', '{self.order_date}','{self.total_price}','{self.user_id}'')"
class Address(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=False, nullable=False) contact = db.Column(db.String(1000), unique=False, nullable=False) description = db.Column(db.String(20), unique=False, nullable=False) address_1 = db.Column(db.String(1000), nullable=False) address_2 = db.Column(db.String(1000), nullable=True) postal_code = db.Column(db.String(1000), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) default = db.Column(db.Boolean, nullable=False, default=False) orders = db.relationship("Order", backref="address", lazy=True) def __repr__(self): return f"Address('{self.description}', '{self.address_1} {self.address_2} {self.postal_code}')"