feat [server]: calendar view grouped by projection month

This commit is contained in:
Alice 2022-07-14 04:03:00 +02:00
parent 8df108a63e
commit c0e7ac3fd9
2 changed files with 29 additions and 4 deletions

View file

@ -1,7 +1,10 @@
# from django.shortcuts import render
from datetime import datetime
from itertools import groupby
from typing import Callable
from django.db.models import Q
from django.db.models.functions import TruncMonth
from rest_framework import viewsets, mixins
from rest_framework.decorators import action
from rest_framework.renderers import StaticHTMLRenderer
@ -50,8 +53,27 @@ class FilmViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin, GenericViewS
serializer_class = FilmSerializer
def get_queryset(self):
queryset = Film.objects.all().filter(is_confirmed=True).order_by("projection_date")
queryset = Film.objects.all().filter(is_confirmed=True)
past = self.request.query_params.get("past", False)
if past:
return queryset.filter(projection_date__date__lt=datetime.now())
return queryset.filter(projection_date__date__gte=datetime.now())
date_filter = (
Q(projection_date__date__lt=datetime.now())
if past
else Q(projection_date__date__gte=datetime.now())
)
ordering = f"{'-' if past else ''}projection_date"
return queryset.filter(date_filter).order_by(ordering)
@action(detail=False, methods=["GET"])
def calendar(self, request):
qs = self.get_queryset().annotate(
projection_month=TruncMonth("projection_date")
)
grouped = groupby(qs, lambda f: f.projection_month)
data = [
{
"projection_month": month.strftime("%B %Y"),
"films": FilmSerializer(monthly_data, many=True).data,
}
for (month, monthly_data) in grouped
]
return Response(data)

View file

@ -9,6 +9,8 @@ https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
import locale
import getconf
from pathlib import Path
@ -97,6 +99,7 @@ DATETIME_FORMAT = "%Y-%m-%dT%H:%M"
TIME_ZONE = "Europe/Paris"
USE_I18N = True
USE_L10N = True
locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
# ManifestStaticFilesStorage is recommended in production, to prevent outdated
# JavaScript / CSS assets being served from cache (e.g. after a Wagtail upgrade).