-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathserializers.py
246 lines (202 loc) · 7.87 KB
/
serializers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from __future__ import annotations
from typing import Any
from django.core.exceptions import ValidationError as DjangoValidationError
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from rest_framework.settings import api_settings
from salesman.checkout.payment import PaymentMethod, payment_methods_pool
from salesman.checkout.serializers import PaymentMethodSerializer
from salesman.conf import app_settings
from salesman.core.serializers import PriceField
from salesman.core.utils import get_salesman_model
from salesman.orders.models import BaseOrder
Order = get_salesman_model("Order")
OrderItem = get_salesman_model("OrderItem")
OrderPayment = get_salesman_model("OrderPayment")
OrderNote = get_salesman_model("OrderNote")
class OrderItemSerializer(serializers.ModelSerializer):
"""
Serializer for order item.
"""
product = serializers.JSONField(source="product_data", read_only=True)
unit_price = PriceField(read_only=True)
subtotal = PriceField(read_only=True)
total = PriceField(read_only=True)
extra = serializers.JSONField(read_only=True)
class Meta:
model = OrderItem
fields = [
"id",
"product_type",
"product_id",
"product",
"unit_price",
"quantity",
"subtotal",
"extra_rows",
"total",
"extra",
]
read_only_fields = fields
class OrderPaymentSerializer(serializers.ModelSerializer):
"""
Serializer for order payment.
"""
amount = PriceField(read_only=True)
class Meta:
model = OrderPayment
fields = ["amount", "transaction_id", "payment_method", "date_created"]
read_only_fields = fields
class OrderNoteSerializer(serializers.ModelSerializer):
"""
Serializer for order note.
"""
class Meta:
model = OrderNote
fields = ["message", "date_created"]
read_only_fields = ["date_created"]
class OrderSerializer(serializers.ModelSerializer):
"""
Serializer for order.
"""
url = serializers.SerializerMethodField()
subtotal = PriceField(read_only=True)
total = PriceField(read_only=True)
amount_paid = PriceField(read_only=True)
amount_outstanding = PriceField(read_only=True)
extra = serializers.JSONField(read_only=True)
items = OrderItemSerializer(many=True, read_only=True)
payments = OrderPaymentSerializer(many=True, read_only=True)
notes = serializers.SerializerMethodField()
class Meta:
model = Order
fields = [
"id",
"url",
"ref",
"token",
"status",
"status_display",
"date_created",
"date_updated",
"is_paid",
"user",
"email",
"billing_address",
"shipping_address",
"subtotal",
"extra_rows",
"total",
"amount_paid",
"amount_outstanding",
"extra",
"items",
"payments",
"notes",
]
read_only_fields = fields
prefetch_related_fields = ["items", "payments", "notes"]
select_related_fields = ["user"]
def get_url(self, obj: BaseOrder) -> str:
request = self.context.get("request", None)
url = reverse("salesman-order-detail", args=[obj.ref])
return str(request.build_absolute_uri(url)) if request else url
def get_notes(self, obj: BaseOrder) -> dict[str, Any]:
notes = [x for x in obj.notes.all() if x.public]
return OrderNoteSerializer(notes, many=True).data
class StatusTransitionSerializer(serializers.Serializer):
"""
Serializer to display order status with error.
"""
value = serializers.CharField(read_only=True)
label = serializers.CharField(read_only=True)
error = serializers.CharField(allow_null=True, read_only=True)
def to_representation(self, status: str) -> Any:
data = super().to_representation(status)
order = self.context["order"]
try:
app_settings.SALESMAN_ORDER_STATUS.validate_transition(status, order)
except (ValidationError, DjangoValidationError) as e:
error = serializers.as_serializer_error(e)
data["error"] = error[api_settings.NON_FIELD_ERRORS_KEY][0]
return data
class OrderStatusSerializer(serializers.ModelSerializer):
"""
Serializer used to change order status.
"""
status = serializers.ChoiceField(choices=app_settings.SALESMAN_ORDER_STATUS.choices)
# Show status transitions with error on GET.
status_transitions = StatusTransitionSerializer(
source="Status",
many=True,
read_only=True,
)
class Meta:
model = Order
fields = ["status", "status_display", "status_transitions"]
def validate_status(self, status: str) -> str:
order = self.context["order"]
return app_settings.SALESMAN_ORDER_STATUS.validate_transition(status, order)
def to_representation(self, instance: BaseOrder) -> Any:
data = super().to_representation(instance)
if self.context["request"].method == "PUT" and "status_transitions" in data:
del data["status_transitions"]
return data
class OrderPaySerializer(serializers.Serializer):
"""
Serializer used to pay for existing order via payment method.
"""
payment_method = serializers.ChoiceField(choices=[], write_only=True)
# Show payment methods with error on GET.
payment_methods = PaymentMethodSerializer(many=True, read_only=True)
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.fields["payment_method"].choices = payment_methods_pool.get_choices(
"order", self.context["request"]
)
def validate_payment_method(self, value: str) -> PaymentMethod | None:
order, request = self.context["order"], self.context["request"]
payment = payment_methods_pool.get_payment(value)
if payment:
payment.validate_order(order, request)
return payment
return None
def save(self, **kwargs: Any) -> None:
# Process the payment.
order, request = self.context["order"], self.context["request"]
payment = self.validated_data["payment_method"]
data = payment.order_payment(order, request)
# Returning string in payments converts to a URL data value.
if isinstance(data, str):
data = {"url": data}
# Override the serializer data with the payment data.
self._data = data
class OrderRefundSerializer(serializers.Serializer):
"""
Serializer used to issue an order refund.
"""
refunded = serializers.ListField(read_only=True)
failed = serializers.ListField(read_only=True)
def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
order = self.context["order"]
if order.status == order.Status.REFUNDED:
raise serializers.ValidationError(_("Order is already marked as Refunded."))
return attrs
def save(self, **kwargs: Any) -> None:
# Process the refund.
order = self.context["order"]
refunded, failed = [], []
for item in order.payments.all():
payment = payment_methods_pool.get_payment(item.payment_method)
serializer = OrderPaymentSerializer(item)
if payment and payment.refund_payment(item):
refunded.append(serializer.data)
else:
failed.append(serializer.data)
# Set data and change order status.
self.validated_data.update({"refunded": refunded, "failed": failed})
if not failed:
order.status = order.Status.REFUNDED
order.save(update_fields=["status"])