From 1f31aaafc60cc9f36fd6b96215b1c2b714ba7af6 Mon Sep 17 00:00:00 2001 From: Timon Engelke Date: Fri, 8 Dec 2023 12:10:06 +0100 Subject: [PATCH] Clean up TransactionSerializer --- src/vinywaji/api/serializers.py | 10 +++++- src/vinywaji/api/views.py | 61 +++++++++++++-------------------- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/vinywaji/api/serializers.py b/src/vinywaji/api/serializers.py index 9c8732b..184c5ae 100644 --- a/src/vinywaji/api/serializers.py +++ b/src/vinywaji/api/serializers.py @@ -19,4 +19,12 @@ class Meta: user = serializers.PrimaryKeyRelatedField( default=serializers.CurrentUserDefault(), queryset=models.User.objects.all() ) - amount = serializers.DecimalField(max_digits=5, decimal_places=2) + amount = serializers.FloatField() + + def create(self, validated_data): + if self.context["request"].query_params.get("currency") == "euro": + validated_data["amount"] = validated_data["amount"] * 100 + if self.context["request"].query_params.get("type") == "purchase": + validated_data["amount"] = validated_data["amount"] * -1 + validated_data["amount"] = int(validated_data["amount"]) + return models.Transaction.objects.create(**validated_data) diff --git a/src/vinywaji/api/views.py b/src/vinywaji/api/views.py index 03781b7..bd9c14f 100644 --- a/src/vinywaji/api/views.py +++ b/src/vinywaji/api/views.py @@ -1,5 +1,10 @@ from django.conf import settings -from drf_spectacular.utils import OpenApiParameter, extend_schema, inline_serializer +from drf_spectacular.utils import ( + OpenApiParameter, + extend_schema, + extend_schema_view, + inline_serializer, +) from rest_framework import status, views, viewsets from rest_framework.decorators import action from rest_framework.permissions import IsAdminUser, IsAuthenticated @@ -29,6 +34,24 @@ def me(self, request): return Response(serializer.data) +@extend_schema_view( + create=extend_schema( + parameters=[ + OpenApiParameter( + "currency", + str, + enum=["ct", "euro"], + description="Currency in which the provided transaction amount is. If euro, it will be converted to cents automatically.", + ), + OpenApiParameter( + "type", + str, + enum=["purchase", "deposit"], + description="Type of transaction. If not given, negative transactions count as purchases and positive ones as deposits.", + ), + ] + ) +) class TransactionViewSet( viewsets.mixins.CreateModelMixin, viewsets.mixins.RetrieveModelMixin, @@ -49,39 +72,3 @@ def get_permissions(self): return [IsAuthenticated()] else: return [(IsAdminUser | permissions.IsRelatedToRequester)()] - - @extend_schema( - parameters=[ - OpenApiParameter( - "currency", - str, - enum=["ct", "euro"], - description="Currency in which the provided transaction amount is. If euro, it will be converted to cents automatically.", - ), - OpenApiParameter( - "type", - str, - enum=["purchase", "deposit"], - description="Type of transaction. If not given, negative transactions count as purchases ans positive ones as deposits.", - ), - ] - ) - def create(self, request: Request, *args, **kwargs): - # perform preliminary validation to make sure that later data modification does not fail unexpectedly - self.get_serializer(data=request.data).is_valid(raise_exception=True) - - # force QueryDict to be mutable - data = request.data.copy() - - # adjust requested transaction object according to query parameters - if "currency" in request.query_params and request.query_params["currency"] == "euro": - data["amount"] = int(float(data["amount"]) * 100) - if "type" in request.query_params and request.query_params["type"] == "purchase": - data["amount"] = int(data["amount"]) * -1 - - # create the transaction object - serializer = self.get_serializer(data=data) - serializer.is_valid(raise_exception=True) - self.perform_create(serializer) - headers = self.get_success_headers(serializer.data) - return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)