diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/models.py | 142 |
1 files changed, 112 insertions, 30 deletions
diff --git a/app/models.py b/app/models.py index 507d135..5155ee5 100644 --- a/app/models.py +++ b/app/models.py @@ -3,16 +3,63 @@ from flask_sqlalchemy import SQLAlchemy +# from sqlalchemy.orm import declarative_base + from datetime import datetime from flask_login import UserMixin from . import db +# Base = declarative_base() + def date_time_now(): return datetime.now().strftime("%Y-%m-%d %H:%M:%S") +# module_user = db.Table( +# "ModuleUser", +# Base.metadata, +# db.Column("ModuleId", db.String, db.ForeignKey("Module.Name"), primary_key=True), +# db.Column("UserId", db.Integer, db.ForeignKey("User.UserId"), primary_key=True), +# ) + +# association_table = db.Table('association', Base.metadata, +# db.Column('left_id', db.ForeignKey('left.id'), primary_key=True), +# db.Column('right_id', db.ForeignKey('right.id'), primary_key=True), +# ) + +# class Parent(Base): +# __tablename__ = 'left' +# id = db.Column(db.Integer, primary_key=True) +# children = db.relationship( +# "Child", +# secondary=association_table, +# back_populates="parents") + +# class Child(Base): +# __tablename__ = 'right' +# id = db.Column(db.Integer, primary_key=True) +# parents = db.relationship( +# "Parent", +# secondary=association_table, +# back_populates="children") + + +class Module(db.Model): + __tablename__ = "Module" + name = db.Column("Name", db.String(20), nullable=False, primary_key=True) + description = db.Column("Description", db.String(50), nullable=False) + # users = db.relationship("User", secondary=module_user, backref="modules") + + def __repr__(self): + return f"<Module {self.name}>" + + class User(UserMixin, db.Model): + """UserMixin inheritance required for features described here: + + https://stackoverflow.com/questions/63231163/what-is-the-usermixin-in-flask""" + __tablename__ = "User" def get_id(self): @@ -32,23 +79,15 @@ class User(UserMixin, db.Model): date_time_updated = db.Column( "DateTimeUpdated", db.String, server_onupdate=date_time_now() ) + # modules = db.relationship("Module", secondary=module_user, backref="users") def __repr__(self): return f"<User {self.name_first} {self.name_last}>" -class Module(db.Model): - __tablename__ = "Module" - name = db.Column( - "Name", db.String(20), unique=True, nullable=False, primary_key=True - ) - description = db.Column("Description", db.String(50), nullable=False) - - def __repr__(self): - return f"<Module {self.name}>" - - class Customer(db.Model): + """One customer has many invoices and orders associated.""" + __tablename__ = "Customer" primary_key = db.Column("CustomerId", db.Integer, nullable=False, primary_key=True) name = db.Column("Name", db.String(20), nullable=False) @@ -65,10 +104,15 @@ class Customer(db.Model): professional_id_1 = db.Column("ProfessionalId1", db.String(20), unique=True) professional_id_2 = db.Column("ProfessionalId2", db.String(20), unique=True) tax_id = db.Column("TaxId", db.String(20), unique=True) - payment_terms = db.Column("PaymentTerms", db.String(20), unique=True) - date_time_created = db.Column("DateTimeCreated", db.String(20), nullable=False) - date_time_updated = db.Column("DateTimeUpdated", db.String(20), nullable=False) + payment_terms = db.Column("PaymentTerms", db.String(20)) + date_time_created = db.Column( + "DateTimeCreated", db.String, server_default=date_time_now() + ) + date_time_updated = db.Column( + "DateTimeUpdated", db.String, server_onupdate=date_time_now() + ) invoices = db.relationship("Invoice", back_populates="customer") + orders = db.relationship("Order", back_populates="customer") def __init__( self, @@ -109,6 +153,8 @@ class Customer(db.Model): class Invoice(db.Model): + """One invoice has many orders, one customer associated.""" + __tablename__ = "Invoice" primary_key = db.Column("InvoiceId", db.Integer, primary_key=True) invoice_id_alt = db.Column("InvoiceIdAlt", db.String) @@ -119,12 +165,15 @@ class Invoice(db.Model): amount_gross = db.Column("AmountGross", db.String) amount_tax = db.Column("AmountTax", db.String) date_time_created = db.Column( - "DateTimeCreated", db.String, default=date_time_now() + "DateTimeCreated", db.String, server_default=date_time_now() ) customer_id = db.Column( "CustomerId", db.Integer, db.ForeignKey("Customer.CustomerId") ) - customer = db.relationship("Customer", back_populates="invoices") + customer = db.relationship( + "Customer", back_populates="invoices", foreign_keys=[customer_id] + ) + orders = db.relationship("Order", back_populates="invoice") def __init__( self, @@ -150,6 +199,51 @@ class Invoice(db.Model): return f"<Invoice for {self.customer.name} at {self.updated}>" +class Order(db.Model): # TODO + """One order per line item in invoice.""" + + __tablename__ = "Order" + primary_key = db.Column("OrderId", db.Integer, primary_key=True) + order_id_alt = db.Column("OrderIdAlt", db.String) + delivery_date = db.Column("DateDelivery", db.String(20), nullable=False) + date_time_created = db.Column( + "DateTimeCreated", db.String, server_default=date_time_now() + ) + date_time_updated = db.Column( + "DateTimeUpdated", db.String, server_onupdate=date_time_now() + ) + product_id = db.Column( + "ProductId", + db.Integer, + db.ForeignKey("Product.ProductId"), + nullable=False, + ) + customer_id = db.Column( + "CustomerId", + db.Integer, + db.ForeignKey("Customer.CustomerId"), + nullable=False, + ) + invoice_id = db.Column( + "InvoiceId", + db.Integer, + db.ForeignKey("Invoice.InvoiceId"), + nullable=True, + ) + customer = db.relationship( + "Customer", back_populates="orders" + ) + product = db.relationship( + "Product", back_populates="orders" + ) + invoice = db.relationship( + "Invoice", back_populates="orders" + ) + + def __repr__(self): + return f"<Order {self.name} at {self.updated}>" + + class Product(db.Model): __tablename__ = "Product" primary_key = db.Column("ProductId", db.Integer, primary_key=True) @@ -165,6 +259,8 @@ class Product(db.Model): date_time_updated = db.Column( "DateTimeUpdated", db.String, server_onupdate=date_time_now() ) + # order_id = db.Column("ProductId", db.Integer, db.ForeignKey("Product.ProductId")) + orders = db.relationship("Order", back_populates="product") def __init__( self, @@ -244,17 +340,3 @@ class FertiTarget(FertiLog): def __repr__(self): return f"<Target ID {self.primary_key}>" - - -# Everything after here is garbage - - -class Order(db.Model): # TODO - id = db.Column(db.Integer, primary_key=True) - date = db.Column(db.String(20), nullable=False) - customer = db.Column(db.String(20), nullable=False) - content = db.Column(db.String(10), nullable=False) - updated = db.Column(db.String) - - def __repr__(self): - return f"<Order {self.name} at {self.updated}>" |