diff --git a/tailbone/views/labels/profiles.py b/tailbone/views/labels/profiles.py index 3dfe07ab..a91cdfb2 100644 --- a/tailbone/views/labels/profiles.py +++ b/tailbone/views/labels/profiles.py @@ -2,7 +2,7 @@ ################################################################################ # # Rattail -- Retail Software Framework -# Copyright © 2010-2021 Lance Edgar +# Copyright © 2010-2022 Lance Edgar # # This file is part of Rattail. # @@ -62,6 +62,11 @@ class LabelProfileView(MasterView): 'sync_me', ] + def __init__(self, request): + super(LabelProfileView, self).__init__(request) + app = self.get_rattail_app() + self.label_handler = app.get_label_handler() + def configure_grid(self, g): super(LabelProfileView, self).configure_grid(g) g.set_sort_defaults('ordinal') @@ -80,7 +85,7 @@ class LabelProfileView(MasterView): def after_edit(self, profile): if not profile.format: - formatter = profile.get_formatter(self.rattail_config) + formatter = self.label_handler.get_formatter(profile) if formatter: try: profile.format = formatter.default_format @@ -122,17 +127,17 @@ class LabelProfileView(MasterView): View for editing extended Printer Settings, for a given Label Profile. """ profile = self.get_instance() - read_profile = self.redirect(self.get_action_url('view', profile)) + redirect = self.redirect(self.get_action_url('view', profile)) - printer = profile.get_printer(self.rattail_config) + printer = self.label_handler.get_printer(profile) if not printer: msg = "Label profile \"{}\" does not have a functional printer spec.".format(profile) self.request.session.flash(msg) - return read_profile + return redirect if not printer.required_settings: msg = "Printer class for label profile \"{}\" does not require any settings.".format(profile) self.request.session.flash(msg) - return read_profile + return redirect form = self.make_printer_settings_form(profile, printer) @@ -140,8 +145,9 @@ class LabelProfileView(MasterView): if self.request.method == 'POST': for setting in printer.required_settings: if setting in self.request.POST: - profile.save_printer_setting(setting, self.request.POST[setting]) - return read_profile + self.label_handler.save_printer_setting( + profile, setting, self.request.POST[setting]) + return redirect return self.render_to_response('printer', { 'form': form, diff --git a/tailbone/views/products.py b/tailbone/views/products.py index cf7be401..57aeb8c7 100644 --- a/tailbone/views/products.py +++ b/tailbone/views/products.py @@ -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)