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.
77 lines
3.1 KiB
77 lines
3.1 KiB
from rest_framework import generics, status
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.response import Response
|
|
|
|
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
|
|
|
|
|
|
|
|
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', [])
|
|
print(f'1---> {participant_infos}')
|
|
print(f'2---> {len(participant_infos)}')
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
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"})
|
|
|
|
if user.user_type != User.UserType.STUDENT:
|
|
user = User.objects.change_user_type(user, User.UserType.STUDENT)
|
|
|
|
participant, created = Participant.objects.get_or_create(
|
|
student=user,
|
|
course=course
|
|
)
|
|
return Response({
|
|
'message': 'Transaction Participant created successfully.',
|
|
'participant_id': participant.id,
|
|
'participant_infos': serializer.data['participant_infos']
|
|
}, status=status.HTTP_201_CREATED)
|
|
|
|
|
|
|
|
transaction_participant = serializer.save(user=user, course=course, price=course.final_price)
|
|
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)
|
|
return queryset
|