Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🩹(api schema): add course query param to product retrieve route #231

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to

- Allow on-demand page size on the order and enrollment endpoints
- Add yarn cli to generate joanie api client in TypeScript
- Add course query param to openapi schema on route products.retrieve

### Removed

Expand Down
19 changes: 18 additions & 1 deletion src/backend/joanie/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from django.http import HttpResponse
from django.utils.translation import gettext_lazy as _

from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import mixins, pagination, permissions, viewsets
from rest_framework.decorators import action
from rest_framework.exceptions import ValidationError as DRFValidationError
Expand Down Expand Up @@ -111,6 +113,14 @@ def get_serializer_context(self):

return context

@swagger_auto_schema(
manual_parameters=[
openapi.Parameter("course", in_=openapi.IN_QUERY, type=openapi.TYPE_STRING)
],
)
def retrieve(self, *args, **kwargs):
return super().retrieve(*args, **kwargs)


# pylint: disable=too-many-ancestors
class EnrollmentViewSet(
Expand Down Expand Up @@ -176,6 +186,10 @@ def perform_create(self, serializer):
owner = User.update_or_create_from_request_user(request_user=self.request.user)
serializer.save(owner=owner)

@swagger_auto_schema(
request_body=serializers.OrderCreateBodySerializer,
responses={200: serializers.OrderCreateResponseSerializer()},
)
@transaction.atomic
def create(self, request, *args, **kwargs):
"""Try to create an order and a related payment if the payment is fee."""
Expand Down Expand Up @@ -247,7 +261,10 @@ def create(self, request, *args, **kwargs):

# Return the fresh new order with payment_info
return Response(
{**serializer.data, "payment_info": payment_info}, status=201
serializers.OrderCreateResponseSerializer(
{**serializer.data, "payment_info": payment_info}
),
status=201,
)

# Else return the fresh new order
Expand Down
24 changes: 24 additions & 0 deletions src/backend/joanie/core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,27 @@ class Meta:
model = models.Certificate
fields = ["id"]
read_only_fields = ["id"]


class OrderCreateBodySerializer(OrderSerializer):
billing_address = AddressSerializer(required=False)

class Meta(OrderSerializer.Meta):
fields = OrderSerializer.Meta.fields + ["billing_address"]
read_only_fields = OrderSerializer.Meta.fields + ["billing_address"]


class PaymentSerializer(serializers.Serializer):
payment_id = serializers.CharField(required=True)
provider = serializers.CharField(required=True)
url = serializers.CharField(required=True)
is_paid = serializers.BooleanField(required=False)


class OrderCreateResponseSerializer(OrderSerializer):
id = serializers.CharField(required=True)
payment_info = PaymentSerializer(required=False)

class Meta(OrderSerializer.Meta):
fields = OrderSerializer.Meta.fields + ["payment_info"]
read_only_fields = OrderSerializer.Meta.fields + ["payment_info"]