Add support for generic "product" batch type

This commit is contained in:
Lance Edgar 2019-04-19 13:23:21 -05:00
parent 4a5f1ce19a
commit 2c1985bef3
5 changed files with 221 additions and 9 deletions

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
# Copyright © 2010-2019 Lance Edgar
#
# This file is part of Rattail.
#
@ -74,3 +74,4 @@ def includeme(config):
# batch views
config.include('tailbone.views.batch.newproduct')
config.include('tailbone.views.batch.pricing')
config.include('tailbone.views.batch.product')

View file

@ -111,6 +111,7 @@ class BatchMasterView(MasterView):
row_labels = {
'upc': "UPC",
'item_id': "Item ID",
'status_code': "Status",
}
@ -236,9 +237,6 @@ class BatchMasterView(MasterView):
kwargs['execute_form'] = self.make_execute_form(action_url=url)
return kwargs
def get_exec_options_kwargs(self, **kwargs):
return kwargs
def get_instance_title(self, batch):
if batch.description:
return "{} {}".format(batch.id_str, batch.description)

View file

@ -0,0 +1,208 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2019 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Views for generic product batches
"""
from __future__ import unicode_literals, absolute_import
from rattail.db import model
from rattail.util import OrderedDict
import colander
from tailbone import forms
from tailbone.views.batch import BatchMasterView
ACTION_OPTIONS = OrderedDict([
('make_label_batch', "Make a new Label Batch"),
('make_pricing_batch', "Make a new Pricing Batch"),
])
class ExecutionOptions(colander.Schema):
action = colander.SchemaNode(
colander.String(),
validator=colander.OneOf(ACTION_OPTIONS),
widget=forms.widgets.PlainSelectWidget(values=ACTION_OPTIONS.items()))
class ProductBatchView(BatchMasterView):
"""
Master view for product batches.
"""
model_class = model.ProductBatch
model_row_class = model.ProductBatchRow
default_handler_spec = 'rattail.batch.product:ProductBatchHandler'
route_prefix = 'batch.product'
url_prefix = '/batches/product'
template_prefix = '/batch/product'
downloadable = True
cloneable = True
execution_options_schema = ExecutionOptions
form_fields = [
'id',
'input_filename',
'description',
'notes',
'created',
'created_by',
'rowcount',
'executed',
'executed_by',
]
row_grid_columns = [
'sequence',
'upc',
'brand_name',
'description',
'size',
'department',
'subdepartment',
'vendor',
'regular_cost',
'current_cost',
'regular_price',
'current_price',
'status_code',
]
row_form_fields = [
'sequence',
'item_entry',
'product',
'item_id',
'upc',
'brand_name',
'description',
'size',
'department_number',
'department',
'subdepartment_number',
'subdepartment',
'vendor',
'vendor_item_code',
'regular_cost',
'current_cost',
'current_cost_starts',
'current_cost_ends',
'regular_price',
'current_price',
'current_price_starts',
'current_price_ends',
'suggested_price',
'status_code',
'status_text',
]
def configure_form(self, f):
super(ProductBatchView, self).configure_form(f)
# input_filename
if self.creating:
f.set_type('input_filename', 'file')
else:
f.set_readonly('input_filename')
f.set_renderer('input_filename', self.render_downloadable_file)
def configure_row_grid(self, g):
super(ProductBatchView, self).configure_row_grid(g)
g.set_joiner('vendor', lambda q: q.outerjoin(model.Vendor))
g.set_sorter('vendor', model.Vendor.name)
g.set_joiner('department', lambda q: q.outerjoin(model.Department))
g.set_sorter('department', model.Department.name)
g.set_joiner('subdepartment', lambda q: q.outerjoin(model.Subdepartment))
g.set_sorter('subdepartment', model.Subdepartment.name)
g.set_type('regular_cost', 'currency')
g.set_type('current_cost', 'currency')
g.set_type('regular_price', 'currency')
g.set_type('current_price', 'currency')
g.set_type('suggested_price', 'currency')
def row_grid_extra_class(self, row, i):
if row.status_code in (row.STATUS_MISSING_KEY,
row.STATUS_PRODUCT_NOT_FOUND):
return 'warning'
def configure_row_form(self, f):
super(ProductBatchView, self).configure_row_form(f)
f.set_type('upc', 'gpc')
f.set_renderer('product', self.render_product)
f.set_renderer('vendor', self.render_vendor)
f.set_renderer('department', self.render_department)
f.set_renderer('subdepartment', self.render_subdepartment)
def get_execute_success_url(self, batch, result, **kwargs):
if kwargs['action'] == 'make_label_batch':
return self.request.route_url('labels.batch.view', uuid=result.uuid)
elif kwargs['action'] == 'make_pricing_batch':
return self.request.route_url('batch.pricing.view', uuid=result.uuid)
return super(ProductBatchView, self).get_execute_success_url(batch)
def get_row_csv_fields(self):
fields = super(ProductBatchView, self).get_row_csv_fields()
if 'vendor_uuid' in fields:
i = fields.index('vendor_uuid')
fields.insert(i + 1, 'vendor_id')
fields.insert(i + 2, 'vendor_abbreviation')
fields.insert(i + 3, 'vendor_name')
else:
fields.append('vendor_id')
fields.append('vendor_abbreviation')
fields.append('vendor_name')
return fields
def supplement_row_data(self, row, fields, data):
vendor = row.vendor
if 'vendor_id' in fields:
data['vendor_id'] = (vendor.id or '') if vendor else ''
if 'vendor_abbreviation' in fields:
data['vendor_abbreviation'] = (vendor.abbreviation or '') if vendor else ''
if 'vendor_name' in fields:
data['vendor_name'] = (vendor.name or '') if vendor else ''
def get_row_csv_row(self, row, fields):
csvrow = super(ProductBatchView, self).get_row_csv_row(row, fields)
self.supplement_row_data(row, fields, csvrow)
return csvrow
def get_row_xlsx_row(self, row, fields):
xlrow = super(ProductBatchView, self).get_row_xlsx_row(row, fields)
self.supplement_row_data(row, fields, xlrow)
return xlrow
def includeme(config):
ProductBatchView.defaults(config)

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
# Copyright © 2010-2019 Lance Edgar
#
# This file is part of Rattail.
#
@ -198,10 +198,6 @@ class HandheldBatchView(FileBatchMasterView):
return tags.link_to(text, url)
return text
def get_exec_options_kwargs(self, **kwargs):
kwargs['ACTION_OPTIONS'] = list(ACTION_OPTIONS.items())
return kwargs
def get_execute_success_url(self, batch, result, **kwargs):
if kwargs['action'] == 'make_inventory_batch':
return self.request.route_url('batch.inventory.view', uuid=result.uuid)

View file

@ -2507,6 +2507,15 @@ class MasterView(View):
if isinstance(value, GPC):
value = six.text_type(value)
elif isinstance(value, datetime.datetime):
# datetime values we provide to Excel must *not* have time zone info,
# but we should make sure they're in "local" time zone effectively.
# note however, this assumes a "naive" time value is in UTC zone!
if value.tzinfo:
value = localtime(self.rattail_config, value, tzinfo=False)
else:
value = localtime(self.rattail_config, value, from_utc=True, tzinfo=False)
xlrow[field] = value
return xlrow