class Device(db.Model, CRUDMixin): """ Generic Device Can be a Smartphone, Tablet or anything else """ __tablename__ = 'devices' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True, index=True) manufacturer_id = db.Column(db.Integer, db.ForeignKey('manufacturers.id')) manufacturer = relationship('Manufacturer', back_populates='devices') repairs = relationship('Repair', secondary=repair_association_table, back_populates='devices', lazy='dynamic') picture_id = db.Column(db.String, db.ForeignKey('pictures.name')) colors = relationship("Color", secondary=color_association_table) enquiries = relationship("Enquiry", back_populates="device") def __init__(self, **kwargs): super(Device, self).__init__(**kwargs) if len(self.colors) == 0: default_color = Color.query.filter_by(default=True).first() if default_color is not None: self.colors.append(default_color) def __repr__(self): return f"<Device: {self.manufacturer.name} - {self.name}>" @property def picture_file(self): """ Get the picture file or if none is provided use the manufacturers default and if no picture is defined at all it uses the default picture. :return: template path of associated html render """ if self.picture is None: return self.manufacturer.picture_file return self.picture.file @classmethod def query_factory_all(cls): """ Query Factory for use in sqlalchemy.wtforms """ return cls.query.order_by(cls.name) @classmethod def _check_if_paths_are_valid(cls): """ private function to ensure every device points to a html render :return: True if everything is valid else False """ for d in cls.query.all(): if d.picture is None or d.picture_file is None: return False return True
class Enquiry(db.Model, CRUDMixin): __tablename__ = "enquiries" id = db.Column(db.Integer, primary_key=True) timestamp = db.Column(db.DateTime, default=dt.datetime.now) name = db.Column(db.String(128)) customer_first_name = db.Column(db.String(128)) customer_last_name = db.Column(db.String(128)) customer_email = db.Column(db.String(128)) customer_phone = db.Column(db.String(128)) customer_street = db.Column(db.String(128)) customer_city = db.Column(db.String(128)) customer_postal_code = db.Column(db.String(32)) sale = db.Column(db.Integer, default=0) imei = db.Column(db.String(64)) color = db.Column(db.String(64)) shop = db.Column(db.String(128)) device_id = db.Column(db.Integer, db.ForeignKey('devices.id')) device = relationship("Device", back_populates="enquiries") repairs = relationship("Repair", secondary=repairs) done = db.Column(db.Boolean, default=False) def __repr__(self): return f"<Enquiry ({self.name}) from {self.timestamp.strftime('%d.%m.%Y at %H:%M')}>" @property def address(self): return f"{self.customer_street or '-'}, {self.customer_postal_code or '-'}, {self.customer_street or '-'}"
class Manufacturer(db.Model, CRUDMixin): __tablename__ = 'manufacturers' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True, index=True) devices = relationship('Device', back_populates='manufacturer', cascade="all, delete-orphan") picture_id = db.Column(db.String, db.ForeignKey('pictures.name')) def __init__(self, **kwargs): super(Manufacturer, self).__init__(**kwargs) if self.picture is None: self.picture = Picture.default_picture() @property def picture_file(self): if self.picture is None: return Picture.default_picture().file return self.picture.file @classmethod def query_factory_all(cls): """ Query Factory for use in sqlalchemy.wtforms """ return cls.query.order_by(cls.name)
@property def picture_file(self): if self.picture is None: return Picture.default_picture().file return self.picture.file @classmethod def query_factory_all(cls): """ Query Factory for use in sqlalchemy.wtforms """ return cls.query.order_by(cls.name) repair_association_table = db.Table('repair_association', db.Column('device_id', db.Integer, db.ForeignKey('devices.id', ondelete="cascade")), db.Column('repair_id', db.Integer, db.ForeignKey('repair.id', ondelete="cascade")) ) color_association_table = db.Table('color_association', db.Column('device_id', db.Integer, db.ForeignKey('devices.id', ondelete="cascade")), db.Column('color_name', db.String, db.ForeignKey('color.name', ondelete="cascade")) ) class Device(db.Model, CRUDMixin): """ Generic Device Can be a Smartphone, Tablet or anything else """ __tablename__ = 'devices'
from price_picker import db import datetime as dt from sqlalchemy.orm import relationship from price_picker.common.database import CRUDMixin repairs = db.Table( 'repairs', db.Column('repair_id', db.Integer, db.ForeignKey('repair.id', ondelete="cascade")), db.Column('enquiry_id', db.Integer, db.ForeignKey('enquiries.id', ondelete="cascade"))) class Enquiry(db.Model, CRUDMixin): __tablename__ = "enquiries" id = db.Column(db.Integer, primary_key=True) timestamp = db.Column(db.DateTime, default=dt.datetime.now) name = db.Column(db.String(128)) customer_first_name = db.Column(db.String(128)) customer_last_name = db.Column(db.String(128)) customer_email = db.Column(db.String(128)) customer_phone = db.Column(db.String(128)) customer_street = db.Column(db.String(128)) customer_city = db.Column(db.String(128)) customer_postal_code = db.Column(db.String(32)) sale = db.Column(db.Integer, default=0) imei = db.Column(db.String(64)) color = db.Column(db.String(64)) shop = db.Column(db.String(128)) device_id = db.Column(db.Integer, db.ForeignKey('devices.id')) device = relationship("Device", back_populates="enquiries")