Fakultas Ilmu Komputer UI

views.py 2.29 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
5
6
from rest_framework import generics
from rest_framework.exceptions import ValidationError
from rest_framework.permissions import IsAuthenticated
7

8
9
from donor.models import DaftarDonor, JadwalDonor
from donor.serializers import DaftarDonorSerializer, JadwalDonorSerializer
10
from main.paginations import ExtraSmallResultsSetPagination
11

12
13

class JadwalDonorView(generics.ListAPIView):
14

15
16
17
    serializer_class = JadwalDonorSerializer

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

25
26
27

class DaftarDonorView(generics.CreateAPIView):

28
29
30
31
32
33
34
35
36
37
    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)
38
39
40
41


class AgendaDonorView(generics.ListAPIView):

42
43
44
45
46
47
48
    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')
49

50
51
52

class RiwayatDonorView(generics.ListAPIView):

53
54
    permission_classes = [IsAuthenticated]
    serializer_class = JadwalDonorSerializer
55
    pagination_class = ExtraSmallResultsSetPagination
56
57
58
59

    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')
60
61
62
63


class FormulirDaftarDonorView(generics.RetrieveAPIView):
    pass