# coding=UTF-8 from datetime import datetime from api.extensions import db from flask_sqlalchemy import BaseQuery from sqlalchemy import or_ class Product(db.Model): __tablename__ = "product-products" id = db.Column("productid", db.Integer, primary_key=True) designerid = db.Column(db.Integer, db.ForeignKey("designers.designerid")) designer = db.relationship("Designer", backref="products") class ProductField(db.Model): __tablename__ = "product-fields" id = db.Column("fieldid", db.Integer, primary_key=True) field = db.Column(db.String()) inserted = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) updated = db.Column(db.DateTime) class ProductProductFields(db.Model): __tablename__ = "product-products_fields" productid = db.Column( db.Integer, db.ForeignKey("product-products.productid"), nullable=False ) fieldid = db.Column( db.Integer, db.ForeignKey("product-fields.fieldid"), nullable=False ) value = db.Column(db.String()) inserted = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) updated = db.Column(db.DateTime) product = db.relationship("Product", backref="fields") field = db.relationship("ProductField") __table_args__ = (db.PrimaryKeyConstraint(productid, fieldid),) class Currencies(db.Model): __tablename__ = "product-currencies" iso3char = db.Column(db.String(3), primary_key=True) exchange_rate = db.Column(db.Float, default=1) class MaterialQuery(BaseQuery): def wallpapers(self): return self.filter(Material.name.in_(Material.WALLPAPER_MATERIALS)) def canvas(self): return self.filter(Material.name.in_(Material.CANVAS_MATERIALS)) class Material(db.Model): __tablename__ = "product-materials" query_class = MaterialQuery WALLPAPER_MATERIALS = [ "standard-wallpaper", # 'self-adhesive-wallpaper', // isn't sold now "premium-wallpaper", ] CANVAS_MATERIALS = ["standard-canvas"] id = db.Column("materialid", db.Integer, primary_key=True) name = db.Column("material", db.String(255), nullable=False) price = db.Column("price", db.Integer) def to_json(self): return {"id": self.id, "name": self.name, "price": float(self.price)} def __repr__(self): return self.name class PrintProductQuery(BaseQuery): def wallpapers(self): return self.filter(or_(PrintProduct.groupid == 1, PrintProduct.groupid == 3)) def canvases(self): return self.filter(PrintProduct.groupid == 2) def posters(self): return self.filter(PrintProduct.groupid == 7) def framed_prints(self): return self.filter(PrintProduct.groupid == 8) class PrintProduct(db.Model): __tablename__ = "product-printproducts" query_class = PrintProductQuery id = db.Column("printid", db.Integer, primary_key=True) productid = db.Column( db.Integer, db.ForeignKey("product-products.productid"), nullable=False ) groupid = db.Column(db.Integer, nullable=False) product = db.relationship("Product", backref="printproducts", lazy="joined")