Add new base classes for customer order/item models

so that we can batch things out next
This commit is contained in:
Lance Edgar 2020-08-01 23:29:19 -05:00
parent e1f58209d4
commit b44f45b8fd
2 changed files with 95 additions and 44 deletions

View file

@ -49,7 +49,8 @@ from .products import (Brand, Tax, Product, ProductImage, ProductCode,
from .purchase import (PurchaseBase, PurchaseItemBase, PurchaseCreditBase,
Purchase, PurchaseItem, PurchaseCredit)
from .custorders import CustomerOrder, CustomerOrderItem, CustomerOrderItemEvent
from .custorders import (CustomerOrderBase, CustomerOrderItemBase,
CustomerOrder, CustomerOrderItem, CustomerOrderItemEvent)
from .messages import Message, MessageRecipient

View file

@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2017 Lance Edgar
# Copyright © 2010-2020 Lance Edgar
#
# This file is part of Rattail.
#
@ -32,21 +32,63 @@ import six
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.orderinglist import ordering_list
from sqlalchemy.ext.declarative import declared_attr
from rattail.db.model import Base, uuid_column
from rattail.db.model import Customer, Person, Product, User
@six.python_2_unicode_compatible
class CustomerOrder(Base):
class CustomerOrderBase(object):
"""
Base class for customer orders; defines common fields.
"""
@declared_attr
def __table_args__(cls):
return cls.__customer_order_table_args__()
@classmethod
def __customer_order_table_args__(cls):
table_name = cls.__tablename__
return (
sa.ForeignKeyConstraint(['customer_uuid'], ['customer.uuid'],
name='{}_fk_customer'.format(table_name)),
sa.ForeignKeyConstraint(['person_uuid'], ['person.uuid'],
name='{}_fk_person'.format(table_name)),
)
customer_uuid = sa.Column(sa.String(length=32), nullable=True)
@declared_attr
def customer(cls):
return orm.relationship(
Customer,
doc="""
Reference to the customer account for which the order exists.
""")
person_uuid = sa.Column(sa.String(length=32), nullable=True)
@declared_attr
def person(cls):
return orm.relationship(
Person,
doc="""
Reference to the person to whom the order applies.
""")
created = sa.Column(sa.DateTime(), nullable=False, default=datetime.datetime.utcnow, doc="""
Date and time when the order/batch was first created.
""")
@six.python_2_unicode_compatible
class CustomerOrder(CustomerOrderBase, Base):
"""
Represents an order placed by the customer.
"""
__tablename__ = 'custorder'
__table_args__ = (
sa.ForeignKeyConstraint(['customer_uuid'], ['customer.uuid'], name='custorder_fk_customer'),
sa.ForeignKeyConstraint(['person_uuid'], ['person.uuid'], name='custorder_fk_person'),
)
uuid = uuid_column()
@ -54,22 +96,6 @@ class CustomerOrder(Base):
Numeric, auto-increment ID for the order.
""")
customer_uuid = sa.Column(sa.String(length=32), nullable=True)
customer = orm.relationship(Customer, doc="""
Reference to the :class:`Customer` instance for whom the order exists.
""")
person_uuid = sa.Column(sa.String(length=32), nullable=True)
person = orm.relationship(Person, doc="""
Reference to the :class:`Person` instance for whom the order exists.
""")
created = sa.Column(sa.DateTime(), nullable=False, default=datetime.datetime.utcnow, doc="""
Date and time when the order was first created.
""")
status_code = sa.Column(sa.Integer(), nullable=False)
items = orm.relationship('CustomerOrderItem', back_populates='order',
@ -84,24 +110,23 @@ class CustomerOrder(Base):
@six.python_2_unicode_compatible
class CustomerOrderItem(Base):
class CustomerOrderItemBase(object):
"""
Represents a particular line item (product) within a customer order.
Base class for customer order line items.
"""
__tablename__ = 'custorder_item'
__table_args__ = (
sa.ForeignKeyConstraint(['order_uuid'], ['custorder.uuid'], name='custorder_item_fk_order'),
sa.ForeignKeyConstraint(['product_uuid'], ['product.uuid'], name='custorder_item_fk_product'),
@declared_attr
def __table_args__(cls):
return cls.__customer_order_item_table_args__()
@classmethod
def __customer_order_item_table_args__(cls):
table_name = cls.__tablename__
return (
sa.ForeignKeyConstraint(['product_uuid'], ['product.uuid'],
name='{}_fk_product'.format(table_name)),
)
uuid = uuid_column()
order_uuid = sa.Column(sa.String(length=32), nullable=False)
order = orm.relationship(CustomerOrder, back_populates='items', doc="""
Reference to the :class:`CustomerOrder` instance to which the item belongs.
""")
sequence = sa.Column(sa.Integer(), nullable=False, doc="""
Numeric sequence for the item, i.e. its "line number". These values should
obviously increment in sequence and be unique within the context of a
@ -110,8 +135,12 @@ class CustomerOrderItem(Base):
product_uuid = sa.Column(sa.String(length=32), nullable=True)
product = orm.relationship(Product, doc="""
Reference to the :class:`Product` instance for the item.
@declared_attr
def product(cls):
return orm.relationship(
Product,
doc="""
Reference to the master product record for the line item.
""")
product_brand = sa.Column(sa.String(length=100), nullable=True, doc="""
@ -184,11 +213,32 @@ class CustomerOrderItem(Base):
Transaction number in which payment for the order was taken, if applicable.
""")
status_code = sa.Column(sa.Integer(), nullable=False)
# TODO
def __str__(self):
return str(self.product)
return str(self.product or "(no product)")
@six.python_2_unicode_compatible
class CustomerOrderItem(CustomerOrderItemBase, Base):
"""
Represents a particular line item (product) within a customer order.
"""
__tablename__ = 'custorder_item'
@declared_attr
def __table_args__(cls):
return cls.__customer_order_item_table_args__() + (
sa.ForeignKeyConstraint(['order_uuid'], ['custorder.uuid'],
name='custorder_item_fk_order'),
)
uuid = uuid_column()
order_uuid = sa.Column(sa.String(length=32), nullable=False)
order = orm.relationship(CustomerOrder, back_populates='items', doc="""
Reference to the :class:`CustomerOrder` instance to which the item belongs.
""")
status_code = sa.Column(sa.Integer(), nullable=False)
class CustomerOrderItemEvent(Base):