Avoid uncaught error when updating order batch row quantities
This commit is contained in:
parent
e0dc858451
commit
a1b05540be
|
@ -2,7 +2,7 @@
|
|||
################################################################################
|
||||
#
|
||||
# Rattail -- Retail Software Framework
|
||||
# Copyright © 2010-2023 Lance Edgar
|
||||
# Copyright © 2010-2024 Lance Edgar
|
||||
#
|
||||
# This file is part of Rattail.
|
||||
#
|
||||
|
@ -28,18 +28,23 @@ API.
|
|||
"""
|
||||
|
||||
import datetime
|
||||
import logging
|
||||
|
||||
from rattail.db import model
|
||||
from rattail.util import pretty_quantity
|
||||
import sqlalchemy as sa
|
||||
|
||||
from rattail.db.model import PurchaseBatch, PurchaseBatchRow
|
||||
|
||||
from cornice import Service
|
||||
|
||||
from tailbone.api.batch import APIBatchView, APIBatchRowView
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class OrderingBatchViews(APIBatchView):
|
||||
|
||||
model_class = model.PurchaseBatch
|
||||
model_class = PurchaseBatch
|
||||
default_handler_spec = 'rattail.batch.purchase:PurchaseBatchHandler'
|
||||
route_prefix = 'orderingbatchviews'
|
||||
permission_prefix = 'ordering'
|
||||
|
@ -55,12 +60,13 @@ class OrderingBatchViews(APIBatchView):
|
|||
Adds a condition to the query, to ensure only purchase batches with
|
||||
"ordering" mode are returned.
|
||||
"""
|
||||
query = super(OrderingBatchViews, self).base_query()
|
||||
model = self.model
|
||||
query = super().base_query()
|
||||
query = query.filter(model.PurchaseBatch.mode == self.enum.PURCHASE_BATCH_MODE_ORDERING)
|
||||
return query
|
||||
|
||||
def normalize(self, batch):
|
||||
data = super(OrderingBatchViews, self).normalize(batch)
|
||||
data = super().normalize(batch)
|
||||
|
||||
data['vendor_uuid'] = batch.vendor.uuid
|
||||
data['vendor_display'] = str(batch.vendor)
|
||||
|
@ -81,7 +87,7 @@ class OrderingBatchViews(APIBatchView):
|
|||
"""
|
||||
data = dict(data)
|
||||
data['mode'] = self.enum.PURCHASE_BATCH_MODE_ORDERING
|
||||
batch = super(OrderingBatchViews, self).create_object(data)
|
||||
batch = super().create_object(data)
|
||||
return batch
|
||||
|
||||
def worksheet(self):
|
||||
|
@ -221,7 +227,7 @@ class OrderingBatchViews(APIBatchView):
|
|||
|
||||
class OrderingBatchRowViews(APIBatchRowView):
|
||||
|
||||
model_class = model.PurchaseBatchRow
|
||||
model_class = PurchaseBatchRow
|
||||
default_handler_spec = 'rattail.batch.purchase:PurchaseBatchHandler'
|
||||
route_prefix = 'ordering.rows'
|
||||
permission_prefix = 'ordering'
|
||||
|
@ -231,8 +237,9 @@ class OrderingBatchRowViews(APIBatchRowView):
|
|||
editable = True
|
||||
|
||||
def normalize(self, row):
|
||||
data = super().normalize(row)
|
||||
app = self.get_rattail_app()
|
||||
batch = row.batch
|
||||
data = super(OrderingBatchRowViews, self).normalize(row)
|
||||
|
||||
data['item_id'] = row.item_id
|
||||
data['upc'] = str(row.upc)
|
||||
|
@ -252,8 +259,8 @@ class OrderingBatchRowViews(APIBatchRowView):
|
|||
data['case_quantity'] = row.case_quantity
|
||||
data['cases_ordered'] = row.cases_ordered
|
||||
data['units_ordered'] = row.units_ordered
|
||||
data['cases_ordered_display'] = pretty_quantity(row.cases_ordered or 0, empty_zero=False)
|
||||
data['units_ordered_display'] = pretty_quantity(row.units_ordered or 0, empty_zero=False)
|
||||
data['cases_ordered_display'] = app.render_quantity(row.cases_ordered or 0, empty_zero=False)
|
||||
data['units_ordered_display'] = app.render_quantity(row.units_ordered or 0, empty_zero=False)
|
||||
|
||||
data['po_unit_cost'] = row.po_unit_cost
|
||||
data['po_unit_cost_display'] = "${:0.2f}".format(row.po_unit_cost) if row.po_unit_cost is not None else None
|
||||
|
@ -281,7 +288,17 @@ class OrderingBatchRowViews(APIBatchRowView):
|
|||
if not self.batch_handler.is_mutable(row.batch):
|
||||
return {'error': "Batch is not mutable"}
|
||||
|
||||
self.batch_handler.update_row_quantity(row, **data)
|
||||
try:
|
||||
self.batch_handler.update_row_quantity(row, **data)
|
||||
self.Session.flush()
|
||||
except Exception as error:
|
||||
log.warning("update_row_quantity failed", exc_info=True)
|
||||
if isinstance(error, sa.exc.DataError) and hasattr(error, 'orig'):
|
||||
error = str(error.orig)
|
||||
else:
|
||||
error = str(error)
|
||||
return {'error': error}
|
||||
|
||||
return row
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue