From 722de10485f19c88de002a0e1f5002accb81a53c Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Wed, 5 Jun 2024 13:24:28 -0400 Subject: [PATCH] stub in new shop_location and inventory models --- make_post_sell/models/inventory.py | 31 ++++++++++++++++++++++++++ make_post_sell/models/shop_location.py | 30 +++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 make_post_sell/models/inventory.py create mode 100644 make_post_sell/models/shop_location.py diff --git a/make_post_sell/models/inventory.py b/make_post_sell/models/inventory.py new file mode 100644 index 0000000..737d994 --- /dev/null +++ b/make_post_sell/models/inventory.py @@ -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() diff --git a/make_post_sell/models/shop_location.py b/make_post_sell/models/shop_location.py new file mode 100644 index 0000000..4885af8 --- /dev/null +++ b/make_post_sell/models/shop_location.py @@ -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()