diff --git a/insalan/payment/models.py b/insalan/payment/models.py index 70972551..d38764ad 100644 --- a/insalan/payment/models.py +++ b/insalan/payment/models.py @@ -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) diff --git a/insalan/payment/serializers.py b/insalan/payment/serializers.py index ab700944..eb6e4e86 100644 --- a/insalan/payment/serializers.py +++ b/insalan/payment/serializers.py @@ -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__" + + + diff --git a/insalan/payment/urls.py b/insalan/payment/urls.py index 6ba4229d..cc776e2f 100644 --- a/insalan/payment/urls.py +++ b/insalan/payment/urls.py @@ -8,7 +8,7 @@ path("pay/", views.PayView.as_view(), name="pay"), path("product/", views.ProductList.as_view(), name="list-product"), path("product//", 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/", views.TransactionPerId.as_view(), name="transactions/id"), ] diff --git a/insalan/payment/views.py b/insalan/payment/views.py index 93cb1fad..e16cf55e 100644 --- a/insalan/payment/views.py +++ b/insalan/payment/views.py @@ -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 @@ -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