From 912005bf998fb114e249dd17348e5d15bf243279 Mon Sep 17 00:00:00 2001 From: Tarun Bhardwaj Date: Fri, 13 Feb 2015 10:53:47 +0530 Subject: [PATCH] Refactored sale._add_or_update to return unsaved active record #6889 --- cart.py | 26 +++++++------------------- sale.py | 56 +++++++++++++++++++++++--------------------------------- 2 files changed, 30 insertions(+), 52 deletions(-) diff --git a/cart.py b/cart.py index 0f8b344..90440d4 100644 --- a/cart.py +++ b/cart.py @@ -7,7 +7,6 @@ :copyright: (c) 2010-2014 by Openlabs Technologies & Consulting (P) LTD :license: GPLv3, see LICENSE for more details ''' -import warnings from decimal import Decimal from functools import partial @@ -347,9 +346,11 @@ def add_to_cart(cls): flash(_("This product is not for sale")) return redirect(request.referrer) - cart.sale._add_or_update( + sale_line = cart.sale._add_or_update( form.product.data, form.quantity.data, action ) + sale_line.save() + if action == 'add': flash(_('The product has been added to your cart'), 'info') else: @@ -359,22 +360,6 @@ def add_to_cart(cls): return redirect(url_for('nereid.cart.view_cart')) - def _add_or_update(self, product_id, quantity, action='set'): - '''Add item as a line or if a line with item exists - update it for the quantity - - :param product: ID of the product - :param quantity: Quantity - :param action: set - set the quantity to the given quantity - add - add quantity to existing quantity - ''' - warnings.warn( - "cart._add_or_update will be deprecated. " - "Use cart.sale._add_or_update instead", - DeprecationWarning, stacklevel=2 - ) - return self.sale._add_or_update(product_id, quantity, action) - @classmethod @route('/cart/delete/', methods=['DELETE', 'POST']) def delete_from_cart(cls, line): @@ -454,7 +439,10 @@ def _login_event_handler(cls, user=None): to_cart = cls.open_cart(True) # Transfer lines from one cart to another for from_line in guest_cart.sale.lines: - to_cart._add_or_update(from_line.product.id, from_line.quantity) + sale_line = to_cart.sale._add_or_update( + from_line.product.id, from_line.quantity + ) + sale_line.save() # Clear and delete the old cart guest_cart._clear_cart() diff --git a/sale.py b/sale.py index 687cd8d..23ebfbd 100644 --- a/sale.py +++ b/sale.py @@ -99,50 +99,40 @@ def _add_or_update(self, product_id, quantity, action='set'): SaleLine = Pool().get('sale.line') order_line = self.find_existing_line(product_id) + + values = { + 'product': product_id, + '_parent_sale.currency': self.currency.id, + '_parent_sale.party': self.party.id, + '_parent_sale.price_list': ( + self.price_list.id if self.price_list else None + ), + 'type': 'line', + } + if order_line: - values = { - 'product': product_id, - '_parent_sale.currency': self.currency.id, - '_parent_sale.party': self.party.id, - '_parent_sale.price_list': ( - self.price_list.id if self.price_list else None - ), + values.update({ 'unit': order_line.unit.id, 'quantity': quantity if action == 'set' else quantity + order_line.quantity, - 'type': 'line', - } - values.update(SaleLine(**values).on_change_quantity()) - - new_values = {} - for key, value in values.iteritems(): - if '.' not in key: - new_values[key] = value - SaleLine.write([order_line], new_values) - return order_line + }) else: - values = { - 'product': product_id, - '_parent_sale.currency': self.currency.id, - '_parent_sale.party': self.party.id, + order_line = SaleLine() + values.update({ 'sale': self.id, - 'type': 'line', 'sequence': 10, 'quantity': quantity, 'unit': None, 'description': None, - } - if self.price_list: - values['_parent_sale.price_list'] = self.price_list.id + }) values.update(SaleLine(**values).on_change_product()) - values.update(SaleLine(**values).on_change_quantity()) - new_values = {} - for key, value in values.iteritems(): - if '.' not in key: - new_values[key] = value - if key == 'taxes' and value: - new_values[key] = [('add', value)] - return SaleLine.create([new_values])[0] + + values.update(SaleLine(**values).on_change_quantity()) + + for key, value in values.iteritems(): + if '.' not in key: + setattr(order_line, key, value) + return order_line class SaleLine: