Skip to content

Commit

Permalink
Fix wrong sender, avoid double save when creating order from basket
Browse files Browse the repository at this point in the history
  • Loading branch information
dinoperovic committed Mar 22, 2022
1 parent 2c8f731 commit 91c7d5c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
15 changes: 15 additions & 0 deletions docs/releases/1.1.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#####
1.1.2
#####

*2022-03-22*

Changed
-------

- Creating an Order using ``create_from_basket`` no longer saves the order twice.

Fixed
-----

- Fixed wrong sender model being sent for status changed signal.
1 change: 1 addition & 0 deletions docs/releases/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Release notes and upgrade information. This project adheres to
:caption: v1.1
:maxdepth: 1

1.1.2
1.1.1
1.1.0

Expand Down
20 changes: 12 additions & 8 deletions salesman/orders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from django.utils.text import Truncator
from django.utils.translation import gettext_lazy as _

from salesman.basket.models import Basket, BasketItem
from salesman.basket.models import BaseBasket, BaseBasketItem
from salesman.conf import app_settings
from salesman.core.typing import Product
from salesman.core.utils import get_salesman_model
Expand Down Expand Up @@ -47,13 +47,12 @@ def create_from_request(self, request: HttpRequest, **kwargs) -> BaseOrder:
Returns:
Order: Order instance
"""
generate_ref = app_settings.SALESMAN_ORDER_REFERENCE_GENERATOR
kwargs['ref'] = generate_ref(request)
kwargs['ref'] = app_settings.SALESMAN_ORDER_REFERENCE_GENERATOR(request)
return super().create(**kwargs)

def create_from_basket(
self,
basket: Basket,
basket: BaseBasket,
request: HttpRequest,
**kwargs,
) -> BaseOrder:
Expand All @@ -63,7 +62,8 @@ def create_from_basket(
Returns:
Order: Order instance
"""
order = self.create_from_request(request, **kwargs)
kwargs['ref'] = app_settings.SALESMAN_ORDER_REFERENCE_GENERATOR(request)
order = self.model(**kwargs)
order.populate_from_basket(basket, request)
return order

Expand Down Expand Up @@ -154,7 +154,7 @@ def save(self, *args, **kwargs):
# Send signal if status changed.
if new_status != old_status:
status_changed.send(
get_salesman_model('Basket'),
get_salesman_model('Order'),
order=self,
new_status=new_status,
old_status=old_status,
Expand Down Expand Up @@ -188,7 +188,7 @@ def pay(
@transaction.atomic
def populate_from_basket(
self,
basket: Basket,
basket: BaseBasket,
request: HttpRequest,
**kwargs,
) -> None:
Expand Down Expand Up @@ -369,7 +369,11 @@ def save(self, *args, **kwargs):
kwargs['update_fields'].append('_extra')
super().save(*args, **kwargs)

def populate_from_basket_item(self, item: BasketItem, request: HttpRequest) -> None:
def populate_from_basket_item(
self,
item: BaseBasketItem,
request: HttpRequest,
) -> None:
"""
Populate order with items from basket.
Expand Down

0 comments on commit 91c7d5c

Please sign in to comment.