cineclub-site/server/myapi/views/std_views.py

80 lines
2.9 KiB
Python
Raw Normal View History

2022-04-03 01:20:53 +02:00
# from django.shortcuts import render
from datetime import datetime
from itertools import groupby
2022-04-03 01:20:54 +02:00
from typing import Callable
from django.db.models import Q
from django.db.models.functions import TruncMonth
from rest_framework import viewsets, mixins
2022-04-03 01:20:54 +02:00
from rest_framework.decorators import action
2022-04-03 01:20:54 +02:00
from rest_framework.renderers import StaticHTMLRenderer
2022-04-03 01:20:54 +02:00
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet
2022-04-03 01:20:53 +02:00
2022-04-03 01:20:54 +02:00
from myapi.renderers import LatexRenderer, PlainTextRenderer
from myapi.serializers import FilmSerializer
from myapi.models import Film
2022-04-03 01:20:54 +02:00
import myapi.services.com_service as com
2022-04-03 01:20:53 +02:00
class AdminFilmViewSet(viewsets.ModelViewSet):
2022-04-03 01:20:54 +02:00
queryset = Film.objects.all().order_by("projection_date")
2022-04-03 01:20:53 +02:00
serializer_class = FilmSerializer
2022-04-03 01:20:53 +02:00
2022-04-03 01:20:54 +02:00
def general_com_view(self, com_function: Callable[[Film], str]):
film: Film = self.get_object()
preview_text = com_function(film)
return Response(preview_text)
2022-04-03 01:20:54 +02:00
# TODO confirm that latex renderer is not a problem
2022-07-14 01:42:12 +02:00
@action(detail=True, methods=["GET"], renderer_classes=[LatexRenderer])
2022-04-03 01:20:54 +02:00
def bocal(self, request, pk=None):
2022-04-03 01:20:54 +02:00
return self.general_com_view(com.bocal)
2022-04-03 01:20:54 +02:00
@action(detail=True, methods=["GET"])
def facebook(self, request, pk=None):
2022-04-03 01:20:54 +02:00
film: Film = self.get_object()
fb_text = com.facebook_text(film)
fb_title = com.facebook_title(film)
return Response(
{"title": fb_title, "body": fb_text, "banner_link": film.banner_link}
)
2022-07-14 01:42:12 +02:00
@action(detail=True, methods=["GET"], renderer_classes=[StaticHTMLRenderer])
def newsletter(self, request, pk=None):
return self.general_com_view(com.mail)
2022-04-03 01:20:54 +02:00
@action(detail=True, methods=["GET"], renderer_classes=[PlainTextRenderer])
def ics(self, request, pk=None):
return self.general_com_view(com.ics)
class FilmViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin, GenericViewSet):
serializer_class = FilmSerializer
def get_queryset(self):
queryset = Film.objects.all().filter(is_confirmed=True)
2022-07-19 02:43:02 +02:00
past = self.request.query_params.get("period", "future") == "past"
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)