-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
proposition of solution on Create Order route
I try to use your proposed request and response serializers for the creation of an Order. All the cases keeps the same returned response, except the error on billing_address, now it return a dict of error and not a single string, because it's a field with multiple values. All the old test past (modification of the billing_address error). The generation of openAPI also works. The usage of new request serializer add also check on billing_address and credit_card_id values that was not done before. I have also a question on the existing code, if paiement fail because we didn't find the card, we didn't cancel the order ? (see commentary in the code)
- Loading branch information
Showing
11 changed files
with
102 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 39 additions & 9 deletions
48
src/backend/joanie/core/serializers/order_create_body_serializer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,45 @@ | ||
"""Serializers for core.api.OrderViewSet.create Body""" | ||
|
||
from rest_framework import serializers | ||
from joanie.core import models | ||
from .model_serializers import OrderSerializer, AddressSerializer | ||
|
||
|
||
class OrderCreateBodySerializer(OrderSerializer): | ||
billing_address = AddressSerializer(required=False) | ||
credit_card_id = serializers.UUIDField(required=False) | ||
|
||
from .model_serializers import OrderSerializer, OrderCreateSerializer, AddressSerializer | ||
class Meta(OrderSerializer.Meta): | ||
fields = OrderSerializer.Meta.fields + ["billing_address", "credit_card_id"] | ||
read_only_fields = OrderSerializer.Meta.fields + ["billing_address", "credit_card_id"] | ||
|
||
def validate(self, data): | ||
cleaned_data = super().validate(data) | ||
# Populate organization field if it is not set and there is only one | ||
# on the product | ||
if "organization" not in cleaned_data or not cleaned_data["organization"]: | ||
try: | ||
organization = cleaned_data["product"].course_relations.get( | ||
course=cleaned_data["course"] | ||
).organizations.get() | ||
except ( | ||
models.Organization.DoesNotExist, | ||
models.Organization.MultipleObjectsReturned, | ||
): | ||
pass | ||
else: | ||
cleaned_data["organization"] = organization | ||
|
||
class OrderCreateBodySerializer: | ||
credit_card_id = serializers.CharField(required=True) | ||
course = serializers.CharField(required=True) | ||
product = serializers.CharField(required=True) | ||
billing_address = AddressSerializer(required=True) | ||
# If product is not free, we have to create a payment. | ||
# To create one, a billing address is mandatory | ||
if cleaned_data['product'].price.amount > 0 \ | ||
and ("billing_address" not in cleaned_data or not cleaned_data['billing_address']): | ||
raise serializers.ValidationError({"billing_address": "This field is required."}) | ||
return cleaned_data | ||
|
||
class Meta(OrderCreateSerializer.Meta): | ||
fields = ["billing_address"] | ||
def create(self, validated_data): | ||
order_data = validated_data.copy() | ||
if 'billing_address' in order_data: | ||
order_data.pop('billing_address') | ||
if 'credit_card_id' in order_data: | ||
order_data.pop('credit_card_id') | ||
return super().create(order_data) |
15 changes: 4 additions & 11 deletions
15
src/backend/joanie/core/serializers/order_create_response_serializer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,10 @@ | ||
"""Serializers for core.api.OrderViewSet.create Response""" | ||
from .model_serializers import OrderSerializer, PaymentSerializer | ||
|
||
from rest_framework import serializers | ||
from djmoney.contrib.django_rest_framework import MoneyField | ||
from drf_yasg.utils import swagger_serializer_method | ||
|
||
from joanie.core import models | ||
from .model_serializers import OrderSerializer, PaymentSerializer, EnrollmentSerializer | ||
|
||
|
||
class OrderCreateResponseSerializer(serializers.Serializer): | ||
order = OrderSerializer(required=True) | ||
class OrderCreateResponseSerializer(OrderSerializer): | ||
payment_info = PaymentSerializer(required=False) | ||
|
||
class Meta(OrderSerializer.Meta): | ||
fields = ["order", "payment_info"] | ||
read_only_fields = ["order", "payment_info"] | ||
fields = OrderSerializer.Meta.fields + ["payment_info"] | ||
read_only_fields = OrderSerializer.Meta.fields + ["payment_info"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters