ref: master
core/types/product.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
from core.database import Base from core.types.balance import Balance from datetime import datetime as dt from sqlalchemy import Integer, Column, DateTime, Numeric, String, ForeignKey, Text from sqlalchemy.orm import relationship class Product(Base): __tablename__ = 'products' id = Column(Integer, primary_key=True) title = Column(String(255), nullable=False) description = Column(Text) group_id = Column(Integer, ForeignKey('groups.id'), nullable=False) category_id = Column(Integer, ForeignKey('product_categories.id'), nullable=False) price = Column(Numeric(10, 2), nullable=False, default=0.0) created_at = Column(DateTime, nullable=False) updated_at = Column(DateTime, nullable=False) balance = relationship("Balance", backref="product", lazy=True, uselist=False) def __init__(self, title, description, group_id, category_id, price): self.title = title self.description = description self.group_id = group_id self.category_id = category_id self.price = price def price_on(self, cycle): margin = (cycle.price_margin * self.price) / 100 return self.price + margin def to_json(self): return { "title": self.title, "description": self.description, "group_id": self.group_id, "category_id": self.category_id, "price": self.price, "created_at": self.created_at.__str__(), "updated_at": self.updated_at.__str__(), "balance": (self.balance.to_json() if self.balance else None) } |