Skip to content

Commit

Permalink
Refactored sale._add_or_update to return unsaved active record #6889
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunbhardwaj committed Feb 13, 2015
1 parent 6f6a996 commit 912005b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 52 deletions.
26 changes: 7 additions & 19 deletions cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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/<int:line>', methods=['DELETE', 'POST'])
def delete_from_cart(cls, line):
Expand Down Expand Up @@ -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()
56 changes: 23 additions & 33 deletions sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 912005b

Please sign in to comment.