Skip to content

Commit

Permalink
Clean up TransactionSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
timonegk committed Dec 8, 2023
1 parent c9f963d commit 1f31aaa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 38 deletions.
10 changes: 9 additions & 1 deletion src/vinywaji/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
61 changes: 24 additions & 37 deletions src/vinywaji/api/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)

0 comments on commit 1f31aaa

Please sign in to comment.