Fakultas Ilmu Komputer UI

views.py 3.6 KB
Newer Older
Ryan Karyadiputera's avatar
test3    
Ryan Karyadiputera committed
1
from rest_framework.decorators import api_view, permission_classes
Ryan Karyadiputera's avatar
test  
Ryan Karyadiputera committed
2
from rest_framework import generics
Ryan Karyadiputera's avatar
test3    
Ryan Karyadiputera committed
3
4
from lembaga.models import Lembaga, Institusi, Tema
from lembaga.serializers import LembagaSerializer, InstitusiSerializer, TemaSerializer
Ryan Karyadiputera's avatar
test  
Ryan Karyadiputera committed
5
from rest_framework.permissions import AllowAny
Ryan Karyadiputera's avatar
test3    
Ryan Karyadiputera committed
6
from rest_framework.response import Response
Ryan Karyadiputera's avatar
test  
Ryan Karyadiputera committed
7
8
9
from rest_framework import viewsets
from django_filters.rest_framework import DjangoFilterBackend

Ryan Karyadiputera's avatar
test3    
Ryan Karyadiputera committed
10
11
12
13

@api_view(["GET"])  # pragma: no cover
@permission_classes([AllowAny])
def get_lembaga(request, time):
Ryan Karyadiputera's avatar
test3    
Ryan Karyadiputera committed
14
15
16
17
18
19
20
21
    current_year = 2021

    if (time == "newer"):
        supervisor_lembaga1 = Lembaga.objects.filter(last_insertion__range = (current_year - 5, current_year))
        supervisor_lembaga2 = Lembaga.objects.filter(last_insertion = None)
        supervisor_lembaga = supervisor_lembaga1 | supervisor_lembaga2
    elif (time == "older"):
        supervisor_lembaga = Lembaga.objects.filter(last_insertion__range = (0, current_year - 6))
Ryan Karyadiputera's avatar
test3    
Ryan Karyadiputera committed
22
23
24
25
26
27

    serializer = LembagaSerializer(supervisor_lembaga, context={'request': request}, many=True)
    response = serializer.data

    return Response(response)

Ryan Karyadiputera's avatar
test  
Ryan Karyadiputera committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

class LembagaView(viewsets.ModelViewSet):
    permission_classes = (AllowAny,)
    serializer_class = LembagaSerializer

    def get_queryset(self, *args, **kwargs):
        """
        Optionally restricts the returned purchases to a given user,
        by filtering against a query parameter in the URL.
        """
        try:
            queryset = Lembaga.objects.all()
            param_institusi = self.request.GET.get('institusi')
            param_tema = self.request.GET.get('tema')
            param_praktikum = self.request.GET.get('praktikum')
            param_nama = self.request.GET.get('nama')

            if param_praktikum:
                temp = param_praktikum.split(',')
                param_praktikum = []
                for i in temp:
                    param_praktikum.append(int(i[-1]))
                queryset = queryset.filter(praktikum_ke__in=param_praktikum)
            if (param_institusi and param_tema):
                param_institusi = param_institusi.split(',')
                param_tema = param_tema.split(',')
                queryset = queryset.filter(institusi__nama__in=param_institusi).filter(tema__nama__in=param_tema)
            else:
                if (param_institusi is not None):
                    param_institusi = param_institusi.split(',')
                    queryset = queryset.filter(institusi__nama__in=param_institusi)
                if (param_tema is not None):
                    param_tema = param_tema.split(',')
                    queryset = queryset.filter(tema__nama__in=param_tema)

            # Find lembaga by Name
            # by performing substring checking
            if param_nama:
                tmp_queryset = queryset
                queryset = []
                for i in tmp_queryset:
                    if param_nama.lower() in i.nama.lower():
                        queryset.append(i)

            return queryset

        # Kasus Error Jika Parameter, Variable, dan Data tidak di set saat melakukan query
        except (ValueError, RuntimeError, TypeError, NameError):
            return {"status": "Harap isi kriteria pencarian"}


class InstitusiView(viewsets.ModelViewSet):
    """
    Provides a get method handler.
    """
    permission_classes = (AllowAny, )
    queryset = Institusi.objects.all()
    serializer_class = InstitusiSerializer


class TemaView(viewsets.ModelViewSet):
    """
    Provides a get method handler.
    """
    permission_classes = (AllowAny, )
    queryset = Tema.objects.all()
    serializer_class = TemaSerializer