aboutsummaryrefslogtreecommitdiff
path: root/api/views.py
diff options
context:
space:
mode:
authorAlban Gruin2019-02-05 19:16:20 +0100
committerAlban Gruin2019-02-05 19:16:20 +0100
commit2cb2110470deb79c42c640816f5d31b5749abc86 (patch)
treeb5aa6c5fd1e2feab95691f767ab3abb92da62045 /api/views.py
parentaa29ed0143dc8b7181092bab8c46a9b073e1c96c (diff)
parent09a5b8cb97769853ac8a3982bcb340c4c1422498 (diff)
Merge branch 'ag/api' into prod/pa1ch/0.14.z
Diffstat (limited to 'api/views.py')
-rw-r--r--api/views.py46
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):