Fakultas Ilmu Komputer UI

views.py 2.72 KB
Newer Older
1
from django.shortcuts import get_object_or_404
2
from django.utils import timezone
3
from django.utils.timezone import now
4
from rest_framework import generics, views
5
6
from rest_framework.exceptions import ValidationError
from rest_framework.permissions import IsAuthenticated
7
from rest_framework.response import Response
8

9
10
from donor.models import DaftarDonor, JadwalDonor
from donor.serializers import DaftarDonorSerializer, JadwalDonorSerializer
11
from main.paginations import ExtraSmallResultsSetPagination
12
from donor.services import formulir_daftar_donor_service
13

14
15

class JadwalDonorView(generics.ListAPIView):
16

17
18
19
    serializer_class = JadwalDonorSerializer

    def get_queryset(self):
20
        queryset = JadwalDonor.objects.all()
21
        date = self.request.query_params.get('date', None)
22
23
24
        if date is not None:
            queryset = queryset.filter(time_start__date=date)
        queryset = queryset.order_by('time_start')
25
        return queryset
26

27
28
29

class DaftarDonorView(generics.CreateAPIView):

30
31
32
33
34
35
36
37
38
39
    permission_classes = [IsAuthenticated]
    serializer_class = DaftarDonorSerializer

    def perform_create(self, serializer):
        jadwal_donor = get_object_or_404(JadwalDonor, pk=self.kwargs.get("pk", None))
        if jadwal_donor.time_end < now():
            raise ValidationError("Can not register for a past event")
        if DaftarDonor.objects.filter(user=self.request.user, jadwal_donor=jadwal_donor):
            raise ValidationError("User has already registered")
        serializer.save(user=self.request.user, jadwal_donor=jadwal_donor)
40
41
42
43


class AgendaDonorView(generics.ListAPIView):

44
45
46
47
48
49
50
    permission_classes = [IsAuthenticated]
    serializer_class = JadwalDonorSerializer

    def get_queryset(self):
        daftar_donors = DaftarDonor.objects.filter(user=self.request.user)
        return JadwalDonor.objects.filter(time_end__gte=timezone.now(),
                                          daftar_donors__in=daftar_donors).order_by('time_start')
51

52
53
54

class RiwayatDonorView(generics.ListAPIView):

55
56
    permission_classes = [IsAuthenticated]
    serializer_class = JadwalDonorSerializer
57
    pagination_class = ExtraSmallResultsSetPagination
58
59
60
61

    def get_queryset(self):
        daftar_donors = DaftarDonor.objects.filter(user=self.request.user, has_attended=True)
        return JadwalDonor.objects.filter(daftar_donors__in=daftar_donors).order_by('-time_start')
62
63


64
65
66
67
68
69
70
71
72
class FormulirDaftarDonorView(views.APIView):

    permission_classes = [IsAuthenticated]

    def get(self, request, pk):
        daftar_donor = get_object_or_404(DaftarDonor, user=request.user, jadwal_donor__pk=pk)
        file_path = formulir_daftar_donor_service.generate_formulir(daftar_donor)
        file_url = request.build_absolute_uri(file_path)
        return Response({"url": file_url})