Changeset b44f45b in rattail


Ignore:
Timestamp:
08/01/2020 11:29:19 PM (3 years ago)
Author:
Lance Edgar <lance@…>
Branches:
master
Children:
42d20a7
Parents:
e1f58209
Message:

Add new base classes for customer order/item models

so that we can batch things out next

Location:
rattail/db/model
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • rattail/db/model/__init__.py

    re1f58209 rb44f45b  
    5050                       Purchase, PurchaseItem, PurchaseCredit)
    5151
    52 from .custorders import CustomerOrder, CustomerOrderItem, CustomerOrderItemEvent
     52from .custorders import (CustomerOrderBase, CustomerOrderItemBase,
     53                         CustomerOrder, CustomerOrderItem, CustomerOrderItemEvent)
    5354
    5455from .messages import Message, MessageRecipient
  • rattail/db/model/custorders.py

    re1f58209 rb44f45b  
    1 # -*- coding: utf-8 -*-
     1# -*- coding: utf-8; -*-
    22################################################################################
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2017 Lance Edgar
     5#  Copyright © 2010-2020 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    3333from sqlalchemy import orm
    3434from sqlalchemy.ext.orderinglist import ordering_list
     35from sqlalchemy.ext.declarative import declared_attr
    3536
    3637from rattail.db.model import Base, uuid_column
     
    3940
    4041@six.python_2_unicode_compatible
    41 class CustomerOrder(Base):
     42class CustomerOrderBase(object):
     43    """
     44    Base class for customer orders; defines common fields.
     45    """
     46
     47    @declared_attr
     48    def __table_args__(cls):
     49        return cls.__customer_order_table_args__()
     50
     51    @classmethod
     52    def __customer_order_table_args__(cls):
     53        table_name = cls.__tablename__
     54        return (
     55            sa.ForeignKeyConstraint(['customer_uuid'], ['customer.uuid'],
     56                                    name='{}_fk_customer'.format(table_name)),
     57            sa.ForeignKeyConstraint(['person_uuid'], ['person.uuid'],
     58                                    name='{}_fk_person'.format(table_name)),
     59        )
     60
     61    customer_uuid = sa.Column(sa.String(length=32), nullable=True)
     62
     63    @declared_attr
     64    def customer(cls):
     65        return orm.relationship(
     66            Customer,
     67            doc="""
     68            Reference to the customer account for which the order exists.
     69            """)
     70
     71    person_uuid = sa.Column(sa.String(length=32), nullable=True)
     72
     73    @declared_attr
     74    def person(cls):
     75        return orm.relationship(
     76            Person,
     77            doc="""
     78            Reference to the person to whom the order applies.
     79            """)
     80
     81    created = sa.Column(sa.DateTime(), nullable=False, default=datetime.datetime.utcnow, doc="""
     82    Date and time when the order/batch was first created.
     83    """)
     84
     85
     86@six.python_2_unicode_compatible
     87class CustomerOrder(CustomerOrderBase, Base):
    4288    """
    4389    Represents an order placed by the customer.
    4490    """
    4591    __tablename__ = 'custorder'
    46     __table_args__ = (
    47         sa.ForeignKeyConstraint(['customer_uuid'], ['customer.uuid'], name='custorder_fk_customer'),
    48         sa.ForeignKeyConstraint(['person_uuid'], ['person.uuid'], name='custorder_fk_person'),
    49     )
    5092
    5193    uuid = uuid_column()
     
    5395    id = sa.Column(sa.Integer(), doc="""
    5496    Numeric, auto-increment ID for the order.
    55     """)
    56 
    57     customer_uuid = sa.Column(sa.String(length=32), nullable=True)
    58 
    59     customer = orm.relationship(Customer, doc="""
    60     Reference to the :class:`Customer` instance for whom the order exists.
    61     """)
    62 
    63     person_uuid = sa.Column(sa.String(length=32), nullable=True)
    64 
    65     person = orm.relationship(Person, doc="""
    66     Reference to the :class:`Person` instance for whom the order exists.
    67     """)
    68 
    69     created = sa.Column(sa.DateTime(), nullable=False, default=datetime.datetime.utcnow, doc="""
    70     Date and time when the order was first created.
    7197    """)
    7298
     
    85111
    86112@six.python_2_unicode_compatible
    87 class CustomerOrderItem(Base):
    88     """
    89     Represents a particular line item (product) within a customer order.
    90     """
    91     __tablename__ = 'custorder_item'
    92     __table_args__ = (
    93         sa.ForeignKeyConstraint(['order_uuid'], ['custorder.uuid'], name='custorder_item_fk_order'),
    94         sa.ForeignKeyConstraint(['product_uuid'], ['product.uuid'], name='custorder_item_fk_product'),
    95     )
    96 
    97     uuid = uuid_column()
    98 
    99     order_uuid = sa.Column(sa.String(length=32), nullable=False)
    100 
    101     order = orm.relationship(CustomerOrder, back_populates='items', doc="""
    102     Reference to the :class:`CustomerOrder` instance to which the item belongs.
    103     """)
     113class CustomerOrderItemBase(object):
     114    """
     115    Base class for customer order line items.
     116    """
     117
     118    @declared_attr
     119    def __table_args__(cls):
     120        return cls.__customer_order_item_table_args__()
     121
     122    @classmethod
     123    def __customer_order_item_table_args__(cls):
     124        table_name = cls.__tablename__
     125        return (
     126            sa.ForeignKeyConstraint(['product_uuid'], ['product.uuid'],
     127                                    name='{}_fk_product'.format(table_name)),
     128        )
    104129
    105130    sequence = sa.Column(sa.Integer(), nullable=False, doc="""
     
    111136    product_uuid = sa.Column(sa.String(length=32), nullable=True)
    112137
    113     product = orm.relationship(Product, doc="""
    114     Reference to the :class:`Product` instance for the item.
    115     """)
     138    @declared_attr
     139    def product(cls):
     140        return orm.relationship(
     141            Product,
     142            doc="""
     143            Reference to the master product record for the line item.
     144            """)
    116145
    117146    product_brand = sa.Column(sa.String(length=100), nullable=True, doc="""
     
    185214    """)
    186215
     216    def __str__(self):
     217        return str(self.product or "(no product)")
     218
     219
     220@six.python_2_unicode_compatible
     221class CustomerOrderItem(CustomerOrderItemBase, Base):
     222    """
     223    Represents a particular line item (product) within a customer order.
     224    """
     225    __tablename__ = 'custorder_item'
     226
     227    @declared_attr
     228    def __table_args__(cls):
     229        return cls.__customer_order_item_table_args__() + (
     230            sa.ForeignKeyConstraint(['order_uuid'], ['custorder.uuid'],
     231                                    name='custorder_item_fk_order'),
     232        )
     233
     234    uuid = uuid_column()
     235
     236    order_uuid = sa.Column(sa.String(length=32), nullable=False)
     237    order = orm.relationship(CustomerOrder, back_populates='items', doc="""
     238    Reference to the :class:`CustomerOrder` instance to which the item belongs.
     239    """)
     240
    187241    status_code = sa.Column(sa.Integer(), nullable=False)
    188 
    189     # TODO
    190     def __str__(self):
    191         return str(self.product)
    192242
    193243
Note: See TracChangeset for help on using the changeset viewer.