stub in new shop_location and inventory models

This commit is contained in:
Russell Ballestrini 2024-06-05 13:24:28 -04:00
parent 34ea581dab
commit 722de10485
2 changed files with 61 additions and 0 deletions

View file

@ -0,0 +1,31 @@
import uuid
from sqlalchemy import Column, Integer, BigInteger
from .meta import Base, RBase, UUIDType, now_timestamp, foreign_key
from sqlalchemy.orm import relationship, backref
class Inventory(RBase, Base):
"""This class represents the inventory of a product at a specific location."""
id = Column(UUIDType, primary_key=True, index=True)
shop_location_id = Column(UUIDType, foreign_key("ShopLocation", "id"), nullable=False)
product_id = Column(UUIDType, foreign_key("Product", "id"), nullable=False)
quantity = Column(Integer, nullable=False, default=0)
created_timestamp = Column(BigInteger, nullable=False)
updated_timestamp = Column(BigInteger, nullable=False)
shop_location = relationship("ShopLocation", backref=backref("inventories", cascade="all, delete-orphan"))
product = relationship("Product", backref=backref("inventories", cascade="all, delete-orphan"))
def __init__(self, shop_location, product, quantity=0):
if not product.is_physical:
raise ValueError("Cannot create inventory for a digital product")
self.id = uuid.uuid1()
self.shop_location = shop_location
self.product = product
self.quantity = quantity
self.created_timestamp = now_timestamp()
self.updated_timestamp = now_timestamp()
def update_quantity(self, quantity):
self.quantity = quantity
self.updated_timestamp = now_timestamp()

View file

@ -0,0 +1,30 @@
import uuid
from sqlalchemy import Column, Unicode, BigInteger
from .meta import Base, RBase, UUIDType, now_timestamp, foreign_key
from sqlalchemy.orm import relationship, backref
class ShopLocation(RBase, Base):
"""This class represents a physical location of a shop."""
id = Column(UUIDType, primary_key=True, index=True)
shop_id = Column(UUIDType, foreign_key("Shop", "id"), nullable=False)
name = Column(Unicode(64), nullable=False)
address = Column(Unicode(256), nullable=False)
city = Column(Unicode(64), nullable=False)
state = Column(Unicode(64), nullable=False)
country = Column(Unicode(64), nullable=False)
postal_code = Column(Unicode(16), nullable=False)
created_timestamp = Column(BigInteger, nullable=False)
shop = relationship("Shop", backref=backref("shop_locations", cascade="all, delete-orphan"))
def __init__(self, shop, name, address, city, state, country, postal_code):
self.id = uuid.uuid1()
self.shop = shop
self.name = name
self.address = address
self.city = city
self.state = state
self.country = country
self.postal_code = postal_code
self.created_timestamp = now_timestamp()