Skip to content

Commit

Permalink
struggling against the serializer (not working yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiroUsagi-san committed Oct 9, 2023
1 parent 118d8da commit 85bcf4c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
4 changes: 2 additions & 2 deletions insalan/payment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ class Transaction(models.Model):
choices=TransactionStatus.choices,
null=False,
verbose_name=_("Transaction status"),
)
)
date = models.DateField()

amount = models.DecimalField(null=False, max_digits=5, decimal_places=2)
6 changes: 4 additions & 2 deletions insalan/payment/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
from .models import Transaction, TransactionStatus, Product

class TransactionSerializer(serializers.ModelSerializer):
products = serializers.ListField(required=True)
class Meta:
model=Transaction
fields = ['payer', 'amount', 'payment_status', 'date']
fields = ['payer', 'amount', 'payment_status', 'date', 'products']

class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = "__all__"



2 changes: 1 addition & 1 deletion insalan/payment/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
path("pay/", views.PayView.as_view(), name="pay"),
path("product/", views.ProductList.as_view(), name="list-product"),
path("product/<int:pk>/", views.ProductDetails.as_view(), name="product-details"),
path("new/", views.CreateProduct.as_view(), name="new-product"),
path("product/new/", views.CreateProduct.as_view(), name="new-product"),
path("transaction/", views.TransactionList.as_view(), name="transactions"),
path("transaction/<int:pk>", views.TransactionPerId.as_view(), name="transactions/id"),
]
21 changes: 16 additions & 5 deletions insalan/payment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
from .serializers import TransactionSerializer
from datetime import date
from .tokens import tokens
from rest_framework import generics, permissions

from rest_framework import generics, permissions, status
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.authentication import SessionAuthentication
from django.shortcuts import render
import insalan.payment.serializers as serializers
from .models import Product, Transaction
Expand Down Expand Up @@ -40,12 +42,21 @@ class TransactionPerId(generics.RetrieveAPIView):
queryset = Transaction.objects.all().order_by('date')
permission_classes = [permissions.IsAdminUser]


class CreateProduct(generics.CreateAPIView):
pass
serializer_class = serializers.ProductSerializer
queryset = Product.objects.all()
permission_classes = [permissions.IsAdminUser]

class PayView(generics.CreateAPIView):
pass
permission_classes = [permissions.IsAuthenticated]
authentication_classes = [SessionAuthentication]
serializer_class = serializers.TransactionSerializer

def create(self, request):
product_list = serializers.ProductSerializer(request.data['products'], many=True)
transaction = Transaction(payer=request.user)
transaction.products.set(product_list)
return Response(TransactionSerializer(transaction), status=status.HTTP_200_OK)
"""
# lets parse the request
user=request.user
Expand Down

0 comments on commit 85bcf4c

Please sign in to comment.