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 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 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 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, backref=db.backref('users', lazy='dynamic')) type = db.Column(db.String(50)) __mapper_args__ = { 'polymorphic_identity': 'user', 'polymorphic_on': 'type' } def __init__(self, username, password, first_name, last_name, email=None): self.username = username self.password = password self.email = "" if email is None else email self.first_name = first_name self.last_name = last_name def get_key_values(self, users=None): if users is None: users = {} users[user.id] = {'name': self.username, 'mail': self.email} return users
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 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 Product(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), nullable=False) description = db.Column(db.String(255), default="") skus = db.relationship('SKU', backref='product', lazy='dynamic') def __init__(self, name, description=None): self.name = str(name) if (description is not None): self.description = description def get_key_values(self, products=None): if products is None: products = {} products[self.id] = { 'name': self.name, 'description': self.description, } skus = {} for sku in self.skus.all(): sku.get_key_values(skus) products[self.id]['skus'] = skus return products
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 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 Role(db.Model): id = db.Column(db.Integer, primary_key=True) type = db.Column(db.Enum(RoleValues)) def __init__(self, type): self.type = type
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,