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

32 lines
1.1 KiB
Python
Raw Normal View History

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
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 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)
@action(detail=True, renderer_classes=[PlainTextRenderer], methods=["GET"])
def facebook(self, request, pk=None):
2022-04-03 01:20:54 +02:00
return self.general_com_view(com.facebook)