Add basic ProductImage data model with importer

Plus some importer tweaks to accommodate a variation on batch mode
This commit is contained in:
Lance Edgar 2017-02-23 13:19:14 -06:00
parent f9c45c42f5
commit 4e50178519
6 changed files with 86 additions and 5 deletions

View file

@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
"""add product_image
Revision ID: 234c79420312
Revises: 7ba105af22a7
Create Date: 2017-02-22 22:43:58.290180
"""
from __future__ import unicode_literals
# revision identifiers, used by Alembic.
revision = '234c79420312'
down_revision = u'7ba105af22a7'
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
import rattail.db.types
from sqlalchemy.dialects import postgresql
def upgrade():
# product_image
op.create_table('product_image',
sa.Column('uuid', sa.String(length=32), nullable=False),
sa.Column('product_uuid', sa.String(length=32), nullable=False),
sa.Column('bytes', sa.LargeBinary(), nullable=False),
sa.ForeignKeyConstraint(['product_uuid'], [u'product.uuid'], name=u'product_image_fk_product'),
sa.PrimaryKeyConstraint('uuid')
)
def downgrade():
# product_image
op.drop_table('product_image')

View file

@ -41,7 +41,7 @@ from .employees import (Employee, EmployeePhoneNumber, EmployeeEmailAddress,
from .shifts import ScheduledShift, WorkedShift
from .vendors import Vendor, VendorPhoneNumber, VendorEmailAddress, VendorContact
from .products import Brand, Tax, Product, ProductCode, ProductCost, ProductPrice
from .products import Brand, Tax, Product, ProductImage, ProductCode, ProductCost, ProductPrice
from .purchase import (PurchaseBase, PurchaseItemBase, PurchaseCreditBase,
Purchase, PurchaseItem, PurchaseCredit)

View file

@ -340,6 +340,33 @@ class Product(Base):
return cost
class ProductImage(Base):
"""
Contains an image for a product.
"""
__tablename__ = 'product_image'
__table_args__ = (
sa.ForeignKeyConstraint(['product_uuid'], ['product.uuid'], name='product_image_fk_product'),
)
uuid = uuid_column()
product_uuid = sa.Column(sa.String(length=32), nullable=False)
product = orm.relationship(
Product,
doc="""
Reference to the product which is shown by the image.
""",
backref=orm.backref(
'image',
uselist=False,
doc="""
Reference to the product's image, if any.
"""))
bytes = sa.Column(sa.LargeBinary(), nullable=False)
@six.python_2_unicode_compatible
class ProductCode(Base):
"""

View file

@ -54,7 +54,10 @@ class QuerySequence(object):
self.query = query
def __len__(self):
return self.query.count()
try:
return len(self.query)
except ValueError:
return self.query.count()
def __iter__(self):
return iter(self.query)

View file

@ -136,7 +136,9 @@ class Importer(object):
if kwargs:
self._setup(**kwargs)
self.setup()
created = updated = deleted = []
created = []
updated = []
deleted = []
# Get complete set of normalized host data.
if host_data is None:
@ -173,11 +175,14 @@ class Importer(object):
self.teardown()
return created, updated, deleted
def _import_create_update(self, data):
def _import_create_update(self, data, created=None, updated=None):
"""
Import the given data; create and/or update records as needed.
"""
created, updated = [], []
if created is None:
created = []
if updated is None:
updated = []
count = len(data)
if not count:
return created, updated

View file

@ -1549,6 +1549,13 @@ class ProductImporter(ToRattail):
return product
class ProductImageImporter(ToRattail):
"""
Data importer for ProductImage model
"""
model_class = model.ProductImage
class ProductCodeImporter(ToRattail):
"""
Data importer for :class:`rattail.db.model.ProductCode`.