class CartSkus(db.Model): id = db.Column(db.Integer, primary_key=True) customer_id = db.Column(db.Integer, db.ForeignKey('customer.id'), primary_key=True) sku_id = db.Column(db.Integer, db.ForeignKey('sku.id'), primary_key=True) quantity = db.Column(db.Integer) def __init__(self, quantity): self.quantity = quantity
class OrderSkus(db.Model): __tablename__ = 'order_skus' order_id = db.Column(db.Integer, db.ForeignKey('order.id'), primary_key=True) sku_id = db.Column(db.Integer, db.ForeignKey('sku.id'), primary_key=True) quantity = db.Column(db.Integer) price = db.Column(db.Integer) subtotal = db.Column(db.Integer) def __init__(self, quantity, price, subtotal): self.quantity = quantity self.price = price self.subtotal = subtotal
class SKU(db.Model): __tablename__ = 'sku' id = db.Column(db.Integer, primary_key=True) properties = db.Column(db.String(255)) product_id = db.Column(db.Integer, db.ForeignKey('product.id'), nullable=False) in_stock = db.Column(db.Boolean, default=False) stock_qty = db.Column(db.Integer, nullable=False) price = db.Column(db.Float, nullable=False) carts = db.relationship('CartSkus', backref=db.backref('sku')) order_skus = db.relationship('OrderSkus', backref=db.backref('sku', lazy=True)) def __init__(self, properties, stock_qty, price): self.properties = properties self.price = float(price) self.stock_qty = int(stock_qty) if (self.stock_qty > 0): self.in_stock = True def get_key_values(self, skus=None): if skus is None: skus = {} sku = {} sku['price'] = self.price sku['stock quantity'] = self.stock_qty sku['In stock'] = self.in_stock sku['Properties'] = self.properties skus[self.id] = sku return skus
class Invoice(db.Model): __tablename__ = 'order' id = db.Column(db.Integer, primary_key=True) status = db.Column(db.String(50)) customer_id = db.Column(db.Integer, db.ForeignKey('customer.id')) is_paid = db.Column(db.Boolean, default=False) time = db.Column(db.DateTime) order_skus = db.relationship('OrderSkus', backref=db.backref('invoice', lazy=True)) total = db.Column(db.Integer) agent_id = db.Column(db.Integer, db.ForeignKey('agent.id')) def __init__(self, status, is_paid, time, total): self.status = status self.is_paid = is_paid self.time = time self.total = total
class FinancialLedgerEntry(db.Model): id = db.Column(db.Integer, primary_key=True) customer_id = db.Column(db.Integer, db.ForeignKey('customer.id')) transaction_date = db.Column(db.DateTime) amount = db.Column(db.Float) balance = db.Column(db.Float) def __init__(self, transaction_date, amount, balance): self.transaction_date = transaction_date self.amount = amount self.balance = balance
class Customer(User): id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True) ledgerEntries = db.relationship('FinancialLedgerEntry', backref=db.backref('customer'), lazy='dynamic') orders = db.relationship('Invoice', backref=db.backref('customer')) cart_skus = db.relationship('CartSkus', backref=db.backref('customer')) phone = db.Column(db.Integer) def __init__(self, username, password, first_name, last_name, phone=None, email=None): super().__init__(username, password, first_name, last_name, email) self.phone = -1 if phone is None else phone __mapper_args__ = {'polymorphic_identity': 'customer'}
class Agent(User): id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True) orders = db.relationship('Invoice', backref=db.backref('agent')) __mapper_args__ = {'polymorphic_identity': 'agent'}
class Admin(User): id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True) __mapper_args__ = {'polymorphic_identity': 'admin'}
from ecomm import db import enum user_roles = db.Table( 'user_roles', db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True), db.Column('role_id', db.Integer, db.ForeignKey('role.id'), primary_key=True)) ''' Parent class for Admin, Agent and Customer Users. ''' class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String) password = db.Column(db.String) email = db.Column(db.String) 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='') roles = db.relationship('Role', secondary=user_roles,