class OrderItem(Base): __tablename__ = 'order_items' quantity = db.Column(db.Integer(), default=1) product_id = db.Column(db.Integer(), db.ForeignKey('products.id'), nullable=False) product = db.relationship("Product") price = db.Column(db.Float()) order_id = db.Column(db.Integer(), db.ForeignKey('orders.id'), nullable=False) order = db.relationship("Order", backref=db.backref('order', lazy=True)) def total(self): return self.quantity * self.price def sku(self): return self.product.sku def __repr__(self): return '{} ({})'.format(self.product.name, self.product.sku) #one--many relationship #order--orderitem
class Product(Base): __tablename__ = 'products' name = db.Column(db.String(50), unique=True, nullable=False) slug = db.Column(db.String(50), unique=True, nullable=False) description = db.Column(db.Text()) meta_description = db.Column(db.String(500)) meta_keywords = db.Column(db.String(500)) sku = db.Column(db.String(100)) model = db.Column(db.String(200)) price = db.Column(db.Float()) old_price = db.Column(db.Float()) image_url = db.Column(db.String(250)) is_bestseller = db.Column(db.Boolean()) is_featured = db.Column(db.Boolean()) quantity = db.Column(db.Integer()) categories = db.relationship('Category',secondary=product_category,lazy='subquery') brands = db.relationship('Brand',secondary=product_brand,lazy='subquery') product_status = db.Column('product_status', Enum(StatusType)) def sale_price(self): if self.old_price > self.price: return self.price else: return None def __repr__(self): return self.name
class CartItem(Base): __tablename__ = 'cart_items' quantity = db.Column(db.Integer(), default=1) product_id = db.Column(db.Integer(), db.ForeignKey('products.id'), nullable=False) product = db.relationship("Product") cart_id = db.Column(db.Integer(), db.ForeignKey('carts.id'), nullable=False) cart = db.relationship("Cart", backref=db.backref('cart', cascade='all, delete-orphan', lazy=True)) def cart_item_total(self): return self.quantity * self.product.price def name(self): return self.product.name def price(self): return self.product.price def image_url(self): return self.product.image_url def increase_quantity(self, quantity): self.quantity = self.quantity + int(quantity) #The relationship between cart and cartitem is many-to-one
class Customer(Base): __tablename__ = 'customers' id = db.Column(db.Integer, primary_key=True) person_id = db.Column(db.Integer(), db.ForeignKey('people.id'), nullable=False) person = db.relationship('Person', backref=db.backref('person', cascade='all, delete-orphan', lazy=True)) addresses = db.relationship('Address', secondary=customer_address, lazy='subquery')
class Order(Base): __tablename__ = 'orders' order_total = db.Column(db.Float()) order_item_total = db.Column(db.Float()) shipping_charge = db.Column(db.Float()) delivery_address_id = db.Column(db.Integer(), db.ForeignKey('addresses.id'), nullable=False) delivery_address = db.relationship("Address", backref=db.backref('delivery_address', lazy=True)) order_status = db.Column('order_status', Enum(OrderStatus)) customer_id = db.Column(db.Integer(), db.ForeignKey('customers.id'), nullable=False) customer = db.relationship("Customer", backref=db.backref('customer', lazy=True))
class Cart(Base): __tablename__ = 'carts' unique_cart_id = db.Column(db.String(500), unique=False) cart_items = db.relationship('CartItem', backref='cart_items', lazy=True) cart_status = db.Column('cart_status', Enum(CartStatus))