|
|
@ -1,12 +1,14 @@ |
|
|
from rest_framework import generics, status |
|
|
from rest_framework import generics, status |
|
|
from rest_framework.permissions import IsAuthenticated |
|
|
from rest_framework.permissions import IsAuthenticated |
|
|
from rest_framework.response import Response |
|
|
from rest_framework.response import Response |
|
|
|
|
|
|
|
|
|
|
|
from rest_framework.views import APIView |
|
|
from apps.course.models import Participant, Course |
|
|
from apps.course.models import Participant, Course |
|
|
from apps.transaction.models import TransactionParticipant |
|
|
from apps.transaction.models import TransactionParticipant |
|
|
from apps.transaction.serializers import TransactionParticipantSerializer, TransactionListSerializer |
|
|
from apps.transaction.serializers import TransactionParticipantSerializer, TransactionListSerializer |
|
|
from utils.exceptions import AppAPIException |
|
|
from utils.exceptions import AppAPIException |
|
|
from apps.account.models import User |
|
|
from apps.account.models import User |
|
|
|
|
|
from drf_yasg.utils import swagger_auto_schema |
|
|
|
|
|
from drf_yasg import openapi |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -25,12 +27,10 @@ class TransactionParticipantCreateView(generics.CreateAPIView): |
|
|
raise AppAPIException({'message': "Course not found"}) # Handle course not found |
|
|
raise AppAPIException({'message': "Course not found"}) # Handle course not found |
|
|
|
|
|
|
|
|
participant_infos = request.data.get('participant_infos', []) |
|
|
participant_infos = request.data.get('participant_infos', []) |
|
|
print(f'1---> {participant_infos}') |
|
|
|
|
|
print(f'2---> {len(participant_infos)}') |
|
|
|
|
|
|
|
|
|
|
|
serializer = self.get_serializer(data=request.data) |
|
|
serializer = self.get_serializer(data=request.data) |
|
|
serializer.is_valid(raise_exception=True) |
|
|
serializer.is_valid(raise_exception=True) |
|
|
|
|
|
|
|
|
|
|
|
statis = TransactionParticipant.TransactionStatus.PENDING |
|
|
if len(participant_infos) == 1 and (course.final_price == 0 or course.is_free): |
|
|
if len(participant_infos) == 1 and (course.final_price == 0 or course.is_free): |
|
|
participant = participant_infos[0] |
|
|
participant = participant_infos[0] |
|
|
if participant.get('email') != user.email: |
|
|
if participant.get('email') != user.email: |
|
|
@ -43,15 +43,11 @@ class TransactionParticipantCreateView(generics.CreateAPIView): |
|
|
student=user, |
|
|
student=user, |
|
|
course=course |
|
|
course=course |
|
|
) |
|
|
) |
|
|
return Response({ |
|
|
|
|
|
'message': 'Transaction Participant created successfully.', |
|
|
|
|
|
'participant_id': participant.id, |
|
|
|
|
|
'participant_infos': serializer.data['participant_infos'] |
|
|
|
|
|
}, status=status.HTTP_201_CREATED) |
|
|
|
|
|
|
|
|
statis = TransactionParticipant.TransactionStatus.SUCCESS |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
transaction_participant = serializer.save(user=user, course=course, price=course.final_price) |
|
|
|
|
|
|
|
|
transaction_participant = serializer.save(user=user, course=course, price=course.final_price, status=statis) |
|
|
print(f'---> {type(transaction_participant)}/ {transaction_participant}') |
|
|
print(f'---> {type(transaction_participant)}/ {transaction_participant}') |
|
|
return Response({ |
|
|
return Response({ |
|
|
'message': 'Transaction Participant created successfully.', |
|
|
'message': 'Transaction Participant created successfully.', |
|
|
@ -74,4 +70,51 @@ class TransactiontListView(generics.ListAPIView): |
|
|
def get_queryset(self): |
|
|
def get_queryset(self): |
|
|
queryset = super().get_queryset() |
|
|
queryset = super().get_queryset() |
|
|
queryset = queryset.filter(user=self.request.user) |
|
|
queryset = queryset.filter(user=self.request.user) |
|
|
return queryset |
|
|
|
|
|
|
|
|
return queryset |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SoftDeleteTransactionParticipantView(APIView): |
|
|
|
|
|
permission_classes = [IsAuthenticated] |
|
|
|
|
|
|
|
|
|
|
|
@swagger_auto_schema( |
|
|
|
|
|
operation_summary="Soft delete a transaction participant", |
|
|
|
|
|
operation_description="Marks a transaction participant as deleted without removing it from the database", |
|
|
|
|
|
manual_parameters=[ |
|
|
|
|
|
openapi.Parameter( |
|
|
|
|
|
'id', |
|
|
|
|
|
openapi.IN_PATH, |
|
|
|
|
|
description="Transaction Participant ID", |
|
|
|
|
|
type=openapi.TYPE_INTEGER, |
|
|
|
|
|
required=True |
|
|
|
|
|
) |
|
|
|
|
|
], |
|
|
|
|
|
responses={ |
|
|
|
|
|
200: openapi.Response( |
|
|
|
|
|
description="Transaction participant successfully marked as deleted", |
|
|
|
|
|
examples={ |
|
|
|
|
|
"application/json": { |
|
|
|
|
|
"success": True, |
|
|
|
|
|
"message": "Transaction participant successfully marked as deleted" |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
), |
|
|
|
|
|
404: "Transaction participant not found", |
|
|
|
|
|
403: "Permission denied" |
|
|
|
|
|
} |
|
|
|
|
|
) |
|
|
|
|
|
def delete(self, request, pk): |
|
|
|
|
|
try: |
|
|
|
|
|
transaction = TransactionParticipant.objects.get(pk=pk) |
|
|
|
|
|
if transaction.user == request.user: |
|
|
|
|
|
transaction.is_deleted = True |
|
|
|
|
|
transaction.save() |
|
|
|
|
|
return Response({ |
|
|
|
|
|
"success": True, |
|
|
|
|
|
"message": "Transaction participant successfully marked as deleted" |
|
|
|
|
|
}, status=status.HTTP_200_OK) |
|
|
|
|
|
else: |
|
|
|
|
|
return AppAPIException({'message': "You don't have permission to delete this transaction"}, status_code=status.HTTP_403_FORBIDDEN) |
|
|
|
|
|
|
|
|
|
|
|
except TransactionParticipant.DoesNotExist: |
|
|
|
|
|
return AppAPIException({'message': "Transaction participant not found"}) |