fix: add Product.default_vendor_item convenience property

This commit is contained in:
Lance Edgar 2025-02-20 09:30:24 -06:00
parent 8359e5692e
commit 9b9260ba4b

View file

@ -583,6 +583,32 @@ class Product(common.ProductBase, Base):
creator=lambda lc: ProductLikeCode(like_code=lc),
)
vendor_items = orm.relationship(
'VendorItem',
back_populates='product',
primaryjoin='VendorItem.upc == Product.upc',
foreign_keys='VendorItem.upc',
order_by='VendorItem.vendor_item_id',
doc="""
List of :class:`VendorItem` records for this product.
""")
@property
def default_vendor_item(self):
"""
Returns the "default" vendor item record. This will
correspond to the :attr:`default_vendor` if possible.
:rtype: :class:`VendorItem` or ``None``
"""
if self.default_vendor:
for item in self.vendor_items:
if item.vendor_id == self.default_vendor.id:
return item
if self.vendor_items:
return self.vendor_items[0]
@property
def full_description(self):
fields = ['brand', 'description', 'size']
@ -732,17 +758,12 @@ class VendorItem(Base):
upc = sa.Column(sa.String(length=13), nullable=False)
product = orm.relationship(
Product,
back_populates='vendor_items',
primaryjoin=Product.upc == upc,
foreign_keys=[upc],
doc="""
Reference to the :class:`Product` to which this record applies.
""",
backref=orm.backref(
'vendor_items',
order_by=vendor_item_id,
doc="""
List of :class:`VendorItem` records for this product.
"""))
""")
brand = sa.Column(sa.String(length=50), nullable=True)