Use the new label handler
also, move "print one-off labels" logic into product master view
This commit is contained in:
parent
517dd4ad9e
commit
fe7612c885
2 changed files with 59 additions and 47 deletions
|
@ -49,7 +49,6 @@ from pyramid import httpexceptions
|
|||
from webhelpers2.html import tags, HTML
|
||||
|
||||
from tailbone import forms, grids
|
||||
from tailbone.db import Session
|
||||
from tailbone.views import MasterView
|
||||
from tailbone.util import raw_datetime
|
||||
|
||||
|
@ -178,7 +177,8 @@ class ProductView(MasterView):
|
|||
|
||||
def __init__(self, request):
|
||||
super(ProductView, self).__init__(request)
|
||||
self.print_labels = request.rattail_config.getbool('tailbone', 'products.print_labels', default=False)
|
||||
self.expose_label_printing = self.rattail_config.getbool(
|
||||
'tailbone', 'products.print_labels', default=False)
|
||||
|
||||
app = self.get_rattail_app()
|
||||
self.product_handler = app.get_products_handler()
|
||||
|
@ -360,7 +360,7 @@ class ProductView(MasterView):
|
|||
|
||||
g.set_sort_defaults('upc')
|
||||
|
||||
if self.print_labels and self.has_perm('print_labels'):
|
||||
if self.expose_label_printing and self.has_perm('print_labels'):
|
||||
if use_buefy:
|
||||
g.more_actions.append(self.make_action(
|
||||
'print_label', icon='print', url='#',
|
||||
|
@ -661,7 +661,7 @@ class ProductView(MasterView):
|
|||
kwargs = super(ProductView, self).template_kwargs_index(**kwargs)
|
||||
model = self.model
|
||||
|
||||
if self.print_labels:
|
||||
if self.expose_label_printing:
|
||||
|
||||
kwargs['label_profiles'] = self.Session.query(model.LabelProfile)\
|
||||
.filter(model.LabelProfile.visible == True)\
|
||||
|
@ -1704,6 +1704,37 @@ class ProductView(MasterView):
|
|||
self.request.response.body = product.image.bytes
|
||||
return self.request.response
|
||||
|
||||
def print_labels(self):
|
||||
app = self.get_rattail_app()
|
||||
label_handler = app.get_label_handler()
|
||||
model = self.model
|
||||
|
||||
profile = self.request.params.get('profile')
|
||||
profile = self.Session.query(model.LabelProfile).get(profile) if profile else None
|
||||
if not profile:
|
||||
return {'error': "Label profile not found"}
|
||||
|
||||
product = self.request.params.get('product')
|
||||
product = self.Session.query(model.Product).get(product) if product else None
|
||||
if not product:
|
||||
return {'error': "Product not found"}
|
||||
|
||||
quantity = self.request.params.get('quantity')
|
||||
if not quantity.isdigit():
|
||||
return {'error': "Quantity must be numeric"}
|
||||
quantity = int(quantity)
|
||||
|
||||
printer = label_handler.get_printer(profile)
|
||||
if not printer:
|
||||
return {'error': "Couldn't get printer from label profile"}
|
||||
|
||||
try:
|
||||
printer.print_labels([({'product': product}, quantity)])
|
||||
except Exception as error:
|
||||
log.warning("error occurred while printing labels", exc_info=True)
|
||||
return {'error': six.text_type(error)}
|
||||
return {'ok': True}
|
||||
|
||||
def search(self):
|
||||
"""
|
||||
Locate a product(s) by UPC.
|
||||
|
@ -1964,10 +1995,18 @@ class ProductView(MasterView):
|
|||
template_prefix = cls.get_template_prefix()
|
||||
permission_prefix = cls.get_permission_prefix()
|
||||
model_title = cls.get_model_title()
|
||||
model_title_plural = cls.get_model_title_plural()
|
||||
|
||||
# print labels
|
||||
config.add_tailbone_permission('products', 'products.print_labels',
|
||||
"Print labels for products")
|
||||
config.add_tailbone_permission(permission_prefix,
|
||||
'{}.print_labels'.format(permission_prefix),
|
||||
"Print labels for {}".format(model_title_plural))
|
||||
config.add_route('{}.print_labels'.format(route_prefix),
|
||||
'{}/labels'.format(url_prefix))
|
||||
config.add_view(cls, attr='print_labels',
|
||||
route_name='{}.print_labels'.format(route_prefix),
|
||||
permission='{}.print_labels'.format(permission_prefix),
|
||||
renderer='json')
|
||||
|
||||
# view deleted products
|
||||
config.add_tailbone_permission('products', 'products.view_deleted',
|
||||
|
@ -2250,39 +2289,6 @@ class PendingProductView(MasterView):
|
|||
permission='{}.resolve_product'.format(permission_prefix))
|
||||
|
||||
|
||||
def print_labels(request):
|
||||
profile = request.params.get('profile')
|
||||
profile = Session.query(model.LabelProfile).get(profile) if profile else None
|
||||
if not profile:
|
||||
return {'error': "Label profile not found"}
|
||||
|
||||
product = request.params.get('product')
|
||||
product = Session.query(model.Product).get(product) if product else None
|
||||
if not product:
|
||||
return {'error': "Product not found"}
|
||||
|
||||
quantity = request.params.get('quantity')
|
||||
if not quantity.isdigit():
|
||||
return {'error': "Quantity must be numeric"}
|
||||
quantity = int(quantity)
|
||||
|
||||
printer = profile.get_printer(request.rattail_config)
|
||||
if not printer:
|
||||
return {'error': "Couldn't get printer from label profile"}
|
||||
|
||||
try:
|
||||
printer.print_labels([(product, quantity, {})])
|
||||
except Exception as error:
|
||||
log.warning("error occurred while printing labels", exc_info=True)
|
||||
return {'error': six.text_type(error)}
|
||||
return {'ok': True}
|
||||
|
||||
|
||||
def includeme(config):
|
||||
|
||||
config.add_route('products.print_labels', '/products/labels')
|
||||
config.add_view(print_labels, route_name='products.print_labels',
|
||||
renderer='json', permission='products.print_labels')
|
||||
|
||||
ProductView.defaults(config)
|
||||
PendingProductView.defaults(config)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue