Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Implement customer note added event
NyanKiyoshida79acd67709
diff --git a/saleor/account/events.py b/saleor/account/events.pyindex 0aa038c4d7..93b3b9ed8e 100644--- a/saleor/account/events.py+++ b/saleor/account/events.py@@ -20,3 +20,12 @@ def customer_placed_order_event(return Nonereturn _new_customer_event(user=user, order=order, type=CustomerEvents.PLACED_ORDER)+++def customer_added_to_note_order_event(*, user: UserType, order: Order, message: str):+ return _new_customer_event(+ user=user,+ order=order,+ type=CustomerEvents.NOTE_ADDED_TO_ORDER,+ parameters={"message": message},+ )diff --git a/saleor/order/events.py b/saleor/order/events.pyindex ed282fb5a6..31ef8de93a 100644--- a/saleor/order/events.py+++ b/saleor/order/events.py@@ -1,16 +1,14 @@from decimal import Decimalfrom typing import Dict, List, Optional, Tuple, Union-from django.contrib.auth.base_user import AbstractBaseUser--from ..account.events import customer_placed_order_event-from ..account.models import Address+from ..account import events as customer_events+from ..account.models import Address, Userfrom ..order.models import Fulfillment, FulfillmentLine, Order, OrderLinefrom ..payment.models import Paymentfrom . import OrderEvents, OrderEventsEmailsfrom .models import OrderEvent-UserType = AbstractBaseUser+UserType = Userdef _lines_per_quantity_to_line_object_list(quantities_per_order_line):@@ -94,7 +92,7 @@ def order_created_event(*, order: Order, user: UserType, from_draft=False):event_type = OrderEvents.PLACED_FROM_DRAFTelse:event_type = OrderEvents.PLACED- customer_placed_order_event(user=user, order=order)+ customer_events.customer_placed_order_event(user=user, order=order)if user.is_anonymous:user = None@@ -218,6 +216,11 @@ def fulfillment_tracking_updated_event(def order_note_added_event(*, order: Order, user: UserType, message: str):+ if not user.is_staff:+ customer_events.customer_added_to_note_order_event(+ user=user, order=order, message=message+ )+return _new_event(order=order,type=OrderEvents.NOTE_ADDED,diff --git a/saleor/order/forms.py b/saleor/order/forms.pyindex 89fd9bbc72..0e7f08c889 100644--- a/saleor/order/forms.py+++ b/saleor/order/forms.py@@ -5,6 +5,7 @@ from django.utils.translation import pgettext_lazyfrom ..account.forms import SignupFormfrom ..payment.models import Paymentfrom ..payment.utils import gateway_void+from . import eventsfrom .models import OrderPAYMENT_CHOICES = [(k, v) for k, v in settings.CHECKOUT_PAYMENT_GATEWAYS.items()]@@ -69,3 +70,9 @@ class CustomerNoteForm(forms.ModelForm):class Meta:model = Orderfields = ["customer_note"]++ def save(self, *, user, commit=True):+ events.order_note_added_event(+ order=self.instance, user=user, message=self.instance.customer_note+ )+ return super().save(commit)diff --git a/saleor/order/views.py b/saleor/order/views.pyindex f23121b89e..47c975bef0 100644--- a/saleor/order/views.py+++ b/saleor/order/views.py@@ -42,7 +42,7 @@ def details(request, token):note_form = CustomerNoteForm(request.POST or None, instance=order)if request.method == "POST":if note_form.is_valid():- note_form.save()+ note_form.save(user=request.user)return redirect("order:details", token=order.token)fulfillments = order.fulfillments.exclude(status=FulfillmentStatus.CANCELED)ctx = {"order": order, "fulfillments": fulfillments, "note_form": note_form}diff --git a/tests/api/test_order.py b/tests/api/test_order.pyindex 6490c4cbd8..3b8c096460 100644--- a/tests/api/test_order.py+++ b/tests/api/test_order.py@@ -7,6 +7,7 @@ import pytestfrom django.core.exceptions import ValidationErrorfrom freezegun import freeze_time+from saleor.account.models import CustomerEventfrom saleor.core.utils.taxes import ZERO_TAXED_MONEYfrom saleor.graphql.core.enums import ReportingPeriodfrom saleor.graphql.order.mutations.orders import (@@ -1221,9 +1222,11 @@ def test_order_update_user_email_existing_user(assert order.user == customer_user-def test_order_add_note(+def test_order_add_note_as_staff_user(staff_api_client, permission_manage_orders, order_with_lines, staff_user):+ """We are testing that adding a note to an order as a staff user is doing the+ expected behaviors."""order = order_with_linesquery = """mutation addNote($id: ID!, $message: String) {@@ -1258,11 +1261,15 @@ def test_order_add_note(assert data["event"]["user"]["email"] == staff_user.emailassert data["event"]["message"] == message+ # Ensure the correct order event was createdevent = order.events.get()assert event.type == order_events.OrderEvents.NOTE_ADDEDassert event.user == staff_userassert event.parameters == {"message": message}+ # Ensure not customer events were created as it was a staff action+ assert not CustomerEvent.objects.exists()+CANCEL_ORDER_QUERY = """mutation cancelOrder($id: ID!, $restock: Boolean!) {diff --git a/tests/test_order.py b/tests/test_order.pyindex 233202d0cf..716dec5b0c 100644--- a/tests/test_order.py+++ b/tests/test_order.py@@ -6,6 +6,7 @@ from django.urls import reversefrom django_countries.fields import Countryfrom prices import Money, TaxedMoney+from saleor.account import events as customer_eventsfrom saleor.account.models import Userfrom saleor.checkout.utils import create_orderfrom saleor.core.exceptions import InsufficientStock@@ -15,7 +16,7 @@ from saleor.core.utils.taxes import (get_taxes_for_country,)from saleor.core.weight import zero_weight-from saleor.order import FulfillmentStatus, OrderStatus, models+from saleor.order import FulfillmentStatus, OrderStatus, events as order_events, modelsfrom saleor.order.models import Fulfillment, Orderfrom saleor.order.utils import (add_variant_to_order,@@ -534,6 +535,20 @@ def test_add_order_note_view(order, authorized_client, customer_user):order.refresh_from_db()assert order.customer_note == customer_note+ # Ensure a order event was triggered+ note_event = order_events.OrderEvent.objects.get() # type: order_events.OrderEvent+ assert note_event.type == order_events.OrderEvents.NOTE_ADDED+ assert note_event.user == customer_user+ assert note_event.order == order+ assert note_event.parameters == {"message": customer_note}++ # Ensure a customer event was triggered+ note_event = customer_events.CustomerEvent.objects.get()+ assert note_event.type == customer_events.CustomerEvents.NOTE_ADDED_TO_ORDER+ assert note_event.user == customer_user+ assert note_event.order == order+ assert note_event.parameters == {"message": customer_note}+def _calculate_order_weight_from_lines(order):weight = zero_weight()