Add basic API support for printing product labels

This commit is contained in:
Lance Edgar 2023-01-29 18:46:49 -06:00
parent c880065da8
commit b7f3a67cd0
2 changed files with 104 additions and 0 deletions

51
tailbone/api/labels.py Normal file
View file

@ -0,0 +1,51 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2023 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/>.
#
################################################################################
"""
Tailbone Web API - Label Views
"""
from __future__ import unicode_literals, absolute_import
from rattail.db.model import LabelProfile
from tailbone.api import APIMasterView
class LabelProfileView(APIMasterView):
"""
API views for Label Profile data
"""
model_class = LabelProfile
collection_url_prefix = '/label-profiles'
object_url_prefix = '/label-profile'
def defaults(config, **kwargs):
base = globals()
LabelProfileView = kwargs.get('LabelProfileView', base['LabelProfileView'])
LabelProfileView.defaults(config)
def includeme(config):
defaults(config)

View file

@ -26,6 +26,8 @@ Tailbone Web API - Product Views
from __future__ import unicode_literals, absolute_import
import logging
import six
import sqlalchemy as sa
from sqlalchemy import orm
@ -37,6 +39,9 @@ from rattail.db import model
from tailbone.api import APIMasterView
log = logging.getLogger(__name__)
class ProductView(APIMasterView):
"""
API views for Product data
@ -63,6 +68,14 @@ class ProductView(APIMasterView):
'sale_price_display',
'sale_ends',
'sale_ends_display',
'tpr_price',
'tpr_price_display',
'tpr_ends',
'tpr_ends_display',
'current_price',
'current_price_display',
'current_ends',
'current_ends_display',
'vendor_name',
'costs',
'image_url',
@ -115,6 +128,39 @@ class ProductView(APIMasterView):
return {'ok': True,
'product': self.normalize(product)}
def print_labels(self):
app = self.get_rattail_app()
label_handler = app.get_label_handler()
model = self.model
data = self.request.json_body
uuid = data.get('label_profile_uuid')
profile = self.Session.query(model.LabelProfile).get(uuid) if uuid else None
if not profile:
return {'error': "Label profile not found"}
uuid = data.get('product_uuid')
product = self.Session.query(model.Product).get(uuid) if uuid else None
if not product:
return {'error': "Product not found"}
try:
quantity = int(data.get('quantity'))
except:
return {'error': "Quantity must be integer"}
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}
@classmethod
def defaults(cls, config):
cls._defaults(config)
@ -133,6 +179,13 @@ class ProductView(APIMasterView):
permission='{}.list'.format(permission_prefix))
config.add_cornice_service(quick_lookup)
# print labels
print_labels = Service(name='{}.print_labels'.format(route_prefix),
path='{}/print-labels'.format(collection_url_prefix))
print_labels.add_view('POST', 'print_labels', klass=cls,
permission='{}.print_labels'.format(permission_prefix))
config.add_cornice_service(print_labels)
def defaults(config, **kwargs):
base = globals()