Fakultas Ilmu Komputer UI

views.py 2.99 KB
Newer Older
1
import datetime
Muzaki Azami's avatar
Muzaki Azami committed
2
3
from rest_framework import viewsets, status
from rest_framework.response import Response
4
from django.db.models import Q
Muzaki Azami's avatar
Muzaki Azami committed
5
from authentication.utilities import get_appropriate_week_num
6
7
from .models import WeeklyReport, WeeklyReportComment
from .serializers import WeeklyReportSerializer, WeeklyReportCommentSerializer
Muzaki Azami's avatar
Muzaki Azami committed
8
9
10
11
12

class WeeklyReportViewSet(viewsets.ModelViewSet):
    serializer_class = WeeklyReportSerializer
    queryset = WeeklyReport.objects.all()

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    def get_queryset(self):
        """
        Optionally restricts the returned purchases to a given user,
        by filtering against a `name` query parameter in the URL.
        """
        groups = self.request.user.groups.values_list('name',flat = True) # QuerySet Object
        groups_list = list(groups)                                     # QuerySet to `list`
        queryset = WeeklyReport.objects.all()

        user_id = self.request.query_params.get('client_id')
        if user_id is not None:
            queryset = queryset.filter(Q(client=user_id))

        if len(groups_list)==1:
            if groups_list[0] == 'nutritionist':
                nutritionist_id = self.request.user.nutritionist.id
                queryset = queryset.filter(Q(nutritionist=nutritionist_id))
        today = datetime.date.today()
        queryset = queryset.filter(Q(client__cart__program_end_date__gte=today))

        queryset = queryset.order_by('week_num')
        return queryset

Muzaki Azami's avatar
Muzaki Azami committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    def create(self, request, *args, **kwargs):
        deadline = self.request.user.deadline
        week_number = get_appropriate_week_num(deadline)


        request.data['week_num'] = week_number
        request.data['nutritionist'] = self.request.user.nutritionist.id
        request.data['client'] = self.request.user.id
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        response = serializer.data

        return Response(response, status=status.HTTP_201_CREATED, headers=headers)
51
52
53
54


class WeeklyReportCommentViewSet(viewsets.ModelViewSet):
    serializer_class = WeeklyReportCommentSerializer
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

    def get_queryset(self):
        queryset = WeeklyReportComment.objects.all()
        weekly_report_id = self.request.query_params.get('weekly_report_id')
        if weekly_report_id is not None:
            try:
                weekly_report = WeeklyReport.objects.get(pk=weekly_report_id)
            except WeeklyReport.DoesNotExist:
                return None
            queryset = queryset.filter(Q(weekly_report=weekly_report))

        return queryset

    def list(self, request):
        queryset = self.get_queryset()

        if queryset is None:
            return Response({'message': 'Weekly Report does not exist.'}, \
                status=status.HTTP_400_BAD_REQUEST)

        serializer = WeeklyReportCommentSerializer(queryset, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)