refactor [api]: generalise com views
This commit is contained in:
parent
3d1ae17114
commit
761dcb728b
|
@ -1,6 +1,9 @@
|
||||||
# from django.shortcuts import render
|
# from django.shortcuts import render
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
|
from rest_framework.renderers import StaticHTMLRenderer
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
from myapi.renderers import LatexRenderer, PlainTextRenderer
|
from myapi.renderers import LatexRenderer, PlainTextRenderer
|
||||||
|
@ -13,15 +16,16 @@ class FilmViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Film.objects.all().order_by("projection_date")
|
queryset = Film.objects.all().order_by("projection_date")
|
||||||
serializer_class = FilmSerializer
|
serializer_class = FilmSerializer
|
||||||
|
|
||||||
|
def general_com_view(self, com_function: Callable[[Film], str]):
|
||||||
|
film: Film = self.get_object()
|
||||||
|
preview_text = com_function(film)
|
||||||
|
return Response(preview_text)
|
||||||
|
|
||||||
# TODO confirm that latex renderer is not a problem
|
# TODO confirm that latex renderer is not a problem
|
||||||
@action(detail=True, renderer_classes=[LatexRenderer], methods=["GET"])
|
@action(detail=True, renderer_classes=[LatexRenderer], methods=["GET"])
|
||||||
def bocal(self, request, pk=None):
|
def bocal(self, request, pk=None):
|
||||||
film: Film = self.get_object()
|
return self.general_com_view(com.bocal)
|
||||||
bocal_text = com.bocal(film)
|
|
||||||
return Response(bocal_text)
|
|
||||||
|
|
||||||
@action(detail=True, renderer_classes=[PlainTextRenderer], methods=["GET"])
|
@action(detail=True, renderer_classes=[PlainTextRenderer], methods=["GET"])
|
||||||
def facebook(self, request, pk=None):
|
def facebook(self, request, pk=None):
|
||||||
film: Film = self.get_object()
|
return self.general_com_view(com.facebook)
|
||||||
fb_text = com.facebook(film)
|
|
||||||
return Response(fb_text)
|
|
||||||
|
|
Loading…
Reference in a new issue