feat [server]: calendar view grouped by projection month
This commit is contained in:
parent
8df108a63e
commit
c0e7ac3fd9
2 changed files with 29 additions and 4 deletions
|
@ -1,7 +1,10 @@
|
||||||
# from django.shortcuts import render
|
# from django.shortcuts import render
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from itertools import groupby
|
||||||
from typing import Callable
|
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 import viewsets, mixins
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework.renderers import StaticHTMLRenderer
|
from rest_framework.renderers import StaticHTMLRenderer
|
||||||
|
@ -50,8 +53,27 @@ class FilmViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin, GenericViewS
|
||||||
serializer_class = FilmSerializer
|
serializer_class = FilmSerializer
|
||||||
|
|
||||||
def get_queryset(self):
|
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)
|
past = self.request.query_params.get("past", False)
|
||||||
if past:
|
date_filter = (
|
||||||
return queryset.filter(projection_date__date__lt=datetime.now())
|
Q(projection_date__date__lt=datetime.now())
|
||||||
return queryset.filter(projection_date__date__gte=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)
|
||||||
|
|
|
@ -9,6 +9,8 @@ https://docs.djangoproject.com/en/3.2/topics/settings/
|
||||||
For the full list of settings and their values, see
|
For the full list of settings and their values, see
|
||||||
https://docs.djangoproject.com/en/3.2/ref/settings/
|
https://docs.djangoproject.com/en/3.2/ref/settings/
|
||||||
"""
|
"""
|
||||||
|
import locale
|
||||||
|
|
||||||
import getconf
|
import getconf
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
@ -97,6 +99,7 @@ DATETIME_FORMAT = "%Y-%m-%dT%H:%M"
|
||||||
TIME_ZONE = "Europe/Paris"
|
TIME_ZONE = "Europe/Paris"
|
||||||
USE_I18N = True
|
USE_I18N = True
|
||||||
USE_L10N = True
|
USE_L10N = True
|
||||||
|
locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
|
||||||
|
|
||||||
# ManifestStaticFilesStorage is recommended in production, to prevent outdated
|
# ManifestStaticFilesStorage is recommended in production, to prevent outdated
|
||||||
# JavaScript / CSS assets being served from cache (e.g. after a Wagtail upgrade).
|
# JavaScript / CSS assets being served from cache (e.g. after a Wagtail upgrade).
|
||||||
|
|
Loading…
Reference in a new issue