You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

126 lines
5.1 KiB

from rest_framework import generics, status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from apps.course.models import Participant, Course
from apps.transaction.models import TransactionParticipant
from apps.transaction.serializers import TransactionParticipantSerializer, TransactionListSerializer
from utils.exceptions import AppAPIException
from apps.account.models import User
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
class TransactionParticipantCreateView(generics.CreateAPIView):
queryset = TransactionParticipant.objects.all()
serializer_class = TransactionParticipantSerializer
permission_classes = [IsAuthenticated]
def create(self, request, *args, **kwargs):
user = request.user
course_slug = self.kwargs.get('slug') # Get the slug from the URL
try:
course = Course.objects.get(slug=course_slug) # Retrieve the Course object
except Course.DoesNotExist:
raise AppAPIException({'message': "Course not found"}) # Handle course not found
participant_infos = request.data.get('participant_infos', [])
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
statis = TransactionParticipant.TransactionStatus.PENDING
if len(participant_infos) == 1 and (course.final_price == 0 or course.is_free):
participant = participant_infos[0]
if participant.get('email') != user.email:
raise AppAPIException({'message': "The email must be for the requesting user"})
# به جای تغییر user_type، فقط نقش student را اضافه می‌کنیم
if not user.has_role('student'):
user.add_role('student')
# فیلتر کردن برای چک کردن وجود participant
existing_participant = Participant.objects.filter(student=user, course=course).first()
if existing_participant:
participant = existing_participant
else:
participant = Participant.objects.create(student=user, course=course)
statis = TransactionParticipant.TransactionStatus.SUCCESS
transaction_participant = serializer.save(user=user, course=course, price=course.final_price, status=statis)
print(f'---> {type(transaction_participant)}/ {transaction_participant}')
return Response({
'message': 'Transaction Participant created successfully.',
'transaction_id': transaction_participant.id,
'participant_infos': serializer.data['participant_infos']
}, status=status.HTTP_201_CREATED)
class TransactiontListView(generics.ListAPIView):
queryset = TransactionParticipant.objects.all() # یا هر فیلتر که بخواهید اضافه کنید
serializer_class = TransactionListSerializer
permission_classes = [IsAuthenticated] # برای دسترسی کاربران احراز هویت شده
def get_queryset(self):
queryset = super().get_queryset()
queryset = queryset.filter(user=self.request.user, is_deleted=False)
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:
raise AppAPIException(
detail={'message': "You don't have permission to delete this transaction"},
status_code=status.HTTP_403_FORBIDDEN
)
except TransactionParticipant.DoesNotExist:
raise AppAPIException({'message': "Transaction participant not found"})