From 7b280f8e2a901297112bc15becb1704205e3e901 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Tue, 5 Feb 2019 19:14:25 +0100 Subject: api/views: ajout de routes pour lister les cours d’une seule journée Signed-off-by: Alban Gruin --- api/views.py | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) (limited to 'api') diff --git a/api/views.py b/api/views.py index f6aa620..b7b69e3 100644 --- a/api/views.py +++ b/api/views.py @@ -83,11 +83,7 @@ class CourseListGroupSet(viewsets.ReadOnlyModelViewSet): many=True) return self.get_paginated_response(courses_json.data) - @detail_route(methods=["get"], url_path="courses/weeks/current") - def current_week(self, request, pk): - obj = self.get_object() - start, end = get_week(*get_current_or_next_week()) - + def __get_courses(self, obj, start, end): courses = Course.objects.get_courses(obj, begin__gte=start, end__lt=end) \ .prefetch_related("groups") @@ -95,6 +91,37 @@ class CourseListGroupSet(viewsets.ReadOnlyModelViewSet): many=True) return self.get_paginated_response(courses_json.data) + @detail_route(methods=["get"], url_path="courses/days/current") + def current_day(self, request, pk): + obj = self.get_object() + start = datetime.date.today() + end = start + datetime.timedelta(days=1) + return self.__get_courses(obj, start, end) + + @detail_route(methods=["get"], url_path="courses/days/(?P\d+)/(?P\d+)/(?P\d+)") + def other_day(self, request, pk, year, month, day): + obj = self.get_object() + + try: + start = datetime.date(int(year), int(month), int(day)) + except ValueError as v: + errors = {} + message = v.args[0] + if message.split(" ")[0] == "month": + errors["month"] = "Rentrez un mois invalide" + else: + errors["day"] = "Numéro de jour invalide pour le mois" + return Response(errors, status=400) + + end = start + datetime.timedelta(days=1) + return self.__get_courses(obj, start, end) + + @detail_route(methods=["get"], url_path="courses/weeks/current") + def current_week(self, request, pk): + obj = self.get_object() + start, end = get_week(*get_current_or_next_week()) + return self.__get_courses(obj, start, end) + @detail_route(methods=["get"], url_path="courses/weeks/(?P\d+)/(?P\d+)") def other_week(self, request, pk, year, week): @@ -108,14 +135,7 @@ class CourseListGroupSet(viewsets.ReadOnlyModelViewSet): if errors: return Response(errors, status=400) - start, end = get_week(int(year), int(week)) - - courses = Course.objects.get_courses(obj, - begin__gte=start, end__lt=end) \ - .prefetch_related("groups") - courses_json = CourseSerializer(self.paginate_queryset(courses), - many=True) - return self.get_paginated_response(courses_json.data) + return self.__get_courses(obj, *get_week(int(year), int(week))) class GroupViewSet(CourseListGroupSet): -- cgit v1.2.1