diff options
author | Alban Gruin | 2019-02-05 19:14:25 +0100 |
---|---|---|
committer | Alban Gruin | 2019-02-05 19:14:25 +0100 |
commit | 7b280f8e2a901297112bc15becb1704205e3e901 (patch) | |
tree | aebd4899d7ed38ee29fdb577fbd3fa59edda8d6e | |
parent | 61eb694cd445fd5f17c4dc6ded0b1d1f5eccdf57 (diff) |
api/views: ajout de routes pour lister les cours d’une seule journée
Signed-off-by: Alban Gruin <alban at pa1ch dot fr>
-rw-r--r-- | api/views.py | 46 |
1 files changed, 33 insertions, 13 deletions
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<year>\d+)/(?P<month>\d+)/(?P<day>\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<year>\d+)/(?P<week>\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): |