Avoid use of self.handler within batch API views

This commit is contained in:
Lance Edgar 2022-09-07 20:46:18 -05:00
parent e46f4bf01e
commit e67cde4255
4 changed files with 52 additions and 33 deletions

View file

@ -27,6 +27,7 @@ Tailbone Web API - Batch Views
from __future__ import unicode_literals, absolute_import from __future__ import unicode_literals, absolute_import
import logging import logging
import warnings
import six import six
@ -84,7 +85,14 @@ class APIBatchView(APIBatchMixin, APIMasterView):
def __init__(self, request, **kwargs): def __init__(self, request, **kwargs):
super(APIBatchView, self).__init__(request, **kwargs) super(APIBatchView, self).__init__(request, **kwargs)
self.handler = self.get_handler() self.batch_handler = self.get_handler()
@property
def handler(self):
warnings.warn("the `handler` property is deprecated; "
"please use `batch_handler` instead",
DeprecationWarning, stacklevel=2)
return self.batch_handler
def normalize(self, batch): def normalize(self, batch):
app = self.get_rattail_app() app = self.get_rattail_app()
@ -115,7 +123,7 @@ class APIBatchView(APIBatchMixin, APIMasterView):
'executed_display': self.pretty_datetime(executed) if executed else None, 'executed_display': self.pretty_datetime(executed) if executed else None,
'executed_by_uuid': batch.executed_by_uuid, 'executed_by_uuid': batch.executed_by_uuid,
'executed_by_display': six.text_type(batch.executed_by or ''), 'executed_by_display': six.text_type(batch.executed_by or ''),
'mutable': self.handler.is_mutable(batch), 'mutable': self.batch_handler.is_mutable(batch),
} }
def create_object(self, data): def create_object(self, data):
@ -128,9 +136,9 @@ class APIBatchView(APIBatchMixin, APIMasterView):
user = self.request.user user = self.request.user
kwargs = dict(data) kwargs = dict(data)
kwargs['user'] = user kwargs['user'] = user
batch = self.handler.make_batch(self.Session(), **kwargs) batch = self.batch_handler.make_batch(self.Session(), **kwargs)
if self.handler.should_populate(batch): if self.batch_handler.should_populate(batch):
self.handler.do_populate(batch, user) self.batch_handler.do_populate(batch, user)
return batch return batch
def update_object(self, batch, data): def update_object(self, batch, data):
@ -198,7 +206,7 @@ class APIBatchView(APIBatchMixin, APIMasterView):
kwargs = dict(self.request.json_body) kwargs = dict(self.request.json_body)
kwargs.pop('user', None) kwargs.pop('user', None)
kwargs.pop('progress', None) kwargs.pop('progress', None)
result = self.handler.do_execute(batch, self.request.user, **kwargs) result = self.batch_handler.do_execute(batch, self.request.user, **kwargs)
return {'ok': bool(result), 'batch': self.normalize(batch)} return {'ok': bool(result), 'batch': self.normalize(batch)}
@classmethod @classmethod
@ -252,7 +260,14 @@ class APIBatchRowView(APIBatchMixin, APIMasterView):
def __init__(self, request, **kwargs): def __init__(self, request, **kwargs):
super(APIBatchRowView, self).__init__(request, **kwargs) super(APIBatchRowView, self).__init__(request, **kwargs)
self.handler = self.get_handler() self.batch_handler = self.get_handler()
@property
def handler(self):
warnings.warn("the `handler` property is deprecated; "
"please use `batch_handler` instead",
DeprecationWarning, stacklevel=2)
return self.batch_handler
def normalize(self, row): def normalize(self, row):
batch = row.batch batch = row.batch
@ -267,7 +282,7 @@ class APIBatchRowView(APIBatchMixin, APIMasterView):
'batch_description': batch.description, 'batch_description': batch.description,
'batch_complete': batch.complete, 'batch_complete': batch.complete,
'batch_executed': bool(batch.executed), 'batch_executed': bool(batch.executed),
'batch_mutable': self.handler.is_mutable(batch), 'batch_mutable': self.batch_handler.is_mutable(batch),
'sequence': row.sequence, 'sequence': row.sequence,
'status_code': row.status_code, 'status_code': row.status_code,
'status_display': row.STATUS.get(row.status_code, six.text_type(row.status_code)), 'status_display': row.STATUS.get(row.status_code, six.text_type(row.status_code)),
@ -280,14 +295,14 @@ class APIBatchRowView(APIBatchMixin, APIMasterView):
Invokes the batch handler's ``refresh_row()`` method after updating the Invokes the batch handler's ``refresh_row()`` method after updating the
row's field data per usual. row's field data per usual.
""" """
if not self.handler.is_mutable(row.batch): if not self.batch_handler.is_mutable(row.batch):
return {'error': "Batch is not mutable"} return {'error': "Batch is not mutable"}
# update row per usual # update row per usual
row = super(APIBatchRowView, self).update_object(row, data) row = super(APIBatchRowView, self).update_object(row, data)
# okay now we apply handler refresh logic # okay now we apply handler refresh logic
self.handler.refresh_row(row) self.batch_handler.refresh_row(row)
return row return row
def delete_object(self, row): def delete_object(self, row):
@ -296,7 +311,7 @@ class APIBatchRowView(APIBatchMixin, APIMasterView):
Delegates deletion of the row to the batch handler. Delegates deletion of the row to the batch handler.
""" """
self.handler.do_remove_row(row) self.batch_handler.do_remove_row(row)
def quick_entry(self): def quick_entry(self):
""" """
@ -312,10 +327,10 @@ class APIBatchRowView(APIBatchMixin, APIMasterView):
entry = data['quick_entry'] entry = data['quick_entry']
try: try:
row = self.handler.quick_entry(self.Session(), batch, entry) row = self.batch_handler.quick_entry(self.Session(), batch, entry)
except Exception as error: except Exception as error:
log.warning("quick entry failed for '%s' batch %s: %s", log.warning("quick entry failed for '%s' batch %s: %s",
self.handler.batch_key, batch.id_str, entry, self.batch_handler.batch_key, batch.id_str, entry,
exc_info=True) exc_info=True)
msg = six.text_type(error) msg = six.text_type(error)
if not msg and isinstance(error, NotImplementedError): if not msg and isinstance(error, NotImplementedError):

View file

@ -67,9 +67,9 @@ class InventoryBatchViews(APIBatchView):
""" """
permission_prefix = self.get_permission_prefix() permission_prefix = self.get_permission_prefix()
if self.request.is_root: if self.request.is_root:
modes = self.handler.get_count_modes() modes = self.batch_handler.get_count_modes()
else: else:
modes = self.handler.get_allowed_count_modes( modes = self.batch_handler.get_allowed_count_modes(
self.Session(), self.request.user, self.Session(), self.request.user,
permission_prefix=permission_prefix) permission_prefix=permission_prefix)
return modes return modes
@ -79,7 +79,7 @@ class InventoryBatchViews(APIBatchView):
Retrieve info about the available "reasons" for inventory adjustment Retrieve info about the available "reasons" for inventory adjustment
batches. batches.
""" """
raw_reasons = self.handler.get_adjustment_reasons(self.Session()) raw_reasons = self.batch_handler.get_adjustment_reasons(self.Session())
reasons = [] reasons = []
for reason in raw_reasons: for reason in raw_reasons:
reasons.append({ reasons.append({
@ -149,7 +149,7 @@ class InventoryBatchRowViews(APIBatchRowView):
pretty_quantity(row.cases or row.units), pretty_quantity(row.cases or row.units),
'CS' if row.cases else data['unit_uom']) 'CS' if row.cases else data['unit_uom'])
data['allow_cases'] = self.handler.allow_cases(batch) data['allow_cases'] = self.batch_handler.allow_cases(batch)
return data return data

View file

@ -104,10 +104,10 @@ class OrderingBatchViews(APIBatchView):
# organize vendor catalog costs by dept / subdept # organize vendor catalog costs by dept / subdept
departments = {} departments = {}
costs = self.handler.get_order_form_costs(self.Session(), batch.vendor) costs = self.batch_handler.get_order_form_costs(self.Session(), batch.vendor)
costs = self.handler.sort_order_form_costs(costs) costs = self.batch_handler.sort_order_form_costs(costs)
costs = list(costs) # we must have a stable list for the rest of this costs = list(costs) # we must have a stable list for the rest of this
self.handler.decorate_order_form_costs(batch, costs) self.batch_handler.decorate_order_form_costs(batch, costs)
for cost in costs: for cost in costs:
department = cost.product.department department = cost.product.department
@ -175,7 +175,7 @@ class OrderingBatchViews(APIBatchView):
sorted_departments.append(dept) sorted_departments.append(dept)
# fetch recent purchase history, sort/pad for template convenience # fetch recent purchase history, sort/pad for template convenience
history = self.handler.get_order_form_history(batch, costs, 6) history = self.batch_handler.get_order_form_history(batch, costs, 6)
for i in range(6 - len(history)): for i in range(6 - len(history)):
history.append(None) history.append(None)
history = list(reversed(history)) history = list(reversed(history))
@ -266,10 +266,10 @@ class OrderingBatchRowViews(APIBatchRowView):
Note that the "normal" logic for this method is not invoked at all. Note that the "normal" logic for this method is not invoked at all.
""" """
if not self.handler.is_mutable(row.batch): if not self.batch_handler.is_mutable(row.batch):
return {'error': "Batch is not mutable"} return {'error': "Batch is not mutable"}
self.handler.update_row_quantity(row, **data) self.batch_handler.update_row_quantity(row, **data)
return row return row

View file

@ -73,7 +73,7 @@ class ReceivingBatchViews(APIBatchView):
data['invoice_total'] = batch.invoice_total data['invoice_total'] = batch.invoice_total
data['invoice_total_calculated'] = batch.invoice_total_calculated data['invoice_total_calculated'] = batch.invoice_total_calculated
data['can_auto_receive'] = self.handler.can_auto_receive(batch) data['can_auto_receive'] = self.batch_handler.can_auto_receive(batch)
return data return data
@ -89,7 +89,7 @@ class ReceivingBatchViews(APIBatchView):
a pending batch. a pending batch.
""" """
batch = self.get_object() batch = self.get_object()
self.handler.auto_receive_all_items(batch) self.batch_handler.auto_receive_all_items(batch)
return self._get(obj=batch) return self._get(obj=batch)
def mark_receiving_complete(self): def mark_receiving_complete(self):
@ -119,7 +119,7 @@ class ReceivingBatchViews(APIBatchView):
if not vendor: if not vendor:
return {'error': "Vendor not found"} return {'error': "Vendor not found"}
purchases = self.handler.get_eligible_purchases( purchases = self.batch_handler.get_eligible_purchases(
vendor, self.enum.PURCHASE_BATCH_MODE_RECEIVING) vendor, self.enum.PURCHASE_BATCH_MODE_RECEIVING)
purchases = [self.normalize_eligible_purchase(p) purchases = [self.normalize_eligible_purchase(p)
@ -128,10 +128,10 @@ class ReceivingBatchViews(APIBatchView):
return {'purchases': purchases} return {'purchases': purchases}
def normalize_eligible_purchase(self, purchase): def normalize_eligible_purchase(self, purchase):
return self.handler.normalize_eligible_purchase(purchase) return self.batch_handler.normalize_eligible_purchase(purchase)
def render_eligible_purchase(self, purchase): def render_eligible_purchase(self, purchase):
return self.handler.render_eligible_purchase(purchase) return self.batch_handler.render_eligible_purchase(purchase)
@classmethod @classmethod
def defaults(cls, config): def defaults(cls, config):
@ -321,6 +321,10 @@ class ReceivingBatchRowViews(APIBatchRowView):
data['cases_expired'] = row.cases_expired data['cases_expired'] = row.cases_expired
data['units_expired'] = row.units_expired data['units_expired'] = row.units_expired
cases, units = self.batch_handler.get_unconfirmed_counts(row)
data['cases_unconfirmed'] = cases
data['units_unconfirmed'] = units
data['po_unit_cost'] = row.po_unit_cost data['po_unit_cost'] = row.po_unit_cost
data['po_total'] = row.po_total data['po_total'] = row.po_total
@ -328,7 +332,7 @@ class ReceivingBatchRowViews(APIBatchRowView):
data['invoice_total'] = row.invoice_total data['invoice_total'] = row.invoice_total
data['invoice_total_calculated'] = row.invoice_total_calculated data['invoice_total_calculated'] = row.invoice_total_calculated
data['allow_cases'] = self.handler.allow_cases() data['allow_cases'] = self.batch_handler.allow_cases()
data['quick_receive'] = self.rattail_config.getbool( data['quick_receive'] = self.rattail_config.getbool(
'rattail.batch', 'purchase.mobile_quick_receive', 'rattail.batch', 'purchase.mobile_quick_receive',
@ -346,8 +350,8 @@ class ReceivingBatchRowViews(APIBatchRowView):
raise NotImplementedError("TODO: add CS support for quick_receive_all") raise NotImplementedError("TODO: add CS support for quick_receive_all")
else: else:
data['quick_receive_uom'] = data['unit_uom'] data['quick_receive_uom'] = data['unit_uom']
accounted_for = self.handler.get_units_accounted_for(row) accounted_for = self.batch_handler.get_units_accounted_for(row)
remainder = self.handler.get_units_ordered(row) - accounted_for remainder = self.batch_handler.get_units_ordered(row) - accounted_for
if accounted_for: if accounted_for:
# some product accounted for; button should receive "remainder" only # some product accounted for; button should receive "remainder" only
@ -389,7 +393,7 @@ class ReceivingBatchRowViews(APIBatchRowView):
default=False) default=False)
if alert_received: if alert_received:
data['received_alert'] = None data['received_alert'] = None
if self.handler.get_units_confirmed(row): if self.batch_handler.get_units_confirmed(row):
msg = "You have already received some of this product; last update was {}.".format( msg = "You have already received some of this product; last update was {}.".format(
humanize.naturaltime(app.make_utc() - row.modified)) humanize.naturaltime(app.make_utc() - row.modified))
data['received_alert'] = msg data['received_alert'] = msg
@ -418,7 +422,7 @@ class ReceivingBatchRowViews(APIBatchRowView):
# handler takes care of the row receiving logic for us # handler takes care of the row receiving logic for us
kwargs = dict(form.validated) kwargs = dict(form.validated)
del kwargs['row'] del kwargs['row']
self.handler.receive_row(row, **kwargs) self.batch_handler.receive_row(row, **kwargs)
self.Session.flush() self.Session.flush()
return self._get(obj=row) return self._get(obj=row)