2022-04-03 01:20:53 +02:00
|
|
|
# from django.shortcuts import render
|
2022-04-03 01:20:54 +02:00
|
|
|
from typing import Callable
|
|
|
|
|
2022-04-03 01:20:53 +02:00
|
|
|
from rest_framework import viewsets
|
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
|
2022-04-03 01:20:53 +02:00
|
|
|
|
2022-04-03 01:20:54 +02:00
|
|
|
from myapi.renderers import LatexRenderer, PlainTextRenderer
|
2022-04-03 01:20:54 +02:00
|
|
|
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:54 +02:00
|
|
|
|
2022-04-03 01:20:53 +02:00
|
|
|
|
|
|
|
class FilmViewSet(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-04-03 01:20:54 +02:00
|
|
|
@action(detail=True, renderer_classes=[LatexRenderer], methods=["GET"])
|
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
|
|
|
|
2022-04-03 01:20:54 +02:00
|
|
|
@action(detail=True, methods=["GET"])
|
2022-04-03 01:20:54 +02:00
|
|
|
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-04-03 01:20:54 +02:00
|
|
|
|
|
|
|
@action(detail=True, renderer_classes=[StaticHTMLRenderer], methods=["GET"])
|
|
|
|
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)
|