class Item(Base): __tablename__ = 'items' id = Column(BigInteger, primary_key=True, index=True) code = Column(BigInteger, index=True) quantity = Column(DECIMAL(precision=10, scale=2)) unit = Column(SqlEnum(Unit)) # TODO: not as discussed, but I think it makes sense to have single unified name the same as for unit and quantity name = Column(Text, index=True) store_products = relationship('StoreProduct', backref='item', lazy='joined') def __repr__(self): return '{}: {}'.format(self.name, self.code) def __hash__(self): return hash(self.id) def __eq__(self, other): return self.id == other.id @staticmethod def from_store_product(product): return Item(code=product.code, quantity=product.quantity, unit=Unit.to_unit(product.unit), name=product.name)
class PriceFunction(Base): __tablename__ = 'price_functions' promotion_id = Column(BigInteger, ForeignKey(Promotion.id), primary_key=True) function_type = Column(SqlEnum(PriceFunctionType)) value = Column(DECIMAL(precision=10, scale=2)) def __repr__(self): return '{}{}'.format( self.value, '%' if self.function_type == PriceFunctionType.percentage else '₪')
class Restrictions(Base): __tablename__ = 'restrictions' id = Column(BigInteger, primary_key=True) #, autoincrement=True) promotion_id = Column(BigInteger, ForeignKey(Promotion.id), index=True) restriction_type = Column(SqlEnum(RestrictionType)) amount = Column(Integer, default=None) store_product_id = Column(BigInteger, ForeignKey(StoreProduct.id), nullable=True) def __repr__(self): return '{}'.format(self.restriction_type)
class Store(Base): __tablename__ = 'stores' id = Column(Integer, primary_key=True) # , autoincrement=True) store_id = Column(Integer) chain_id = Column(Integer, ForeignKey(Chain.id)) name = Column(String) city = Column(String) address = Column(String, default='') type = Column(SqlEnum(StoreType)) UniqueConstraint(store_id, chain_id) def __repr__(self): return 'id: {}. {}-{}'.format(self.id, self.name, self.address) # def __str__(self): # return '{}-{}:{}'.format(self.chain.name, self.name, self.address) def __eq__(self, other): # must be different for different chain-store combination because of the UniqueConstraint return self.id == other.id def __hash__(self): return hash(self.id)