feat [api]: complete facebook response

This commit is contained in:
Alice 2022-04-03 01:20:54 +02:00
parent b26c31c187
commit 5721930803
2 changed files with 16 additions and 4 deletions

View file

@ -1,3 +1,4 @@
from django.template import Template, Context
from django.template.loader import render_to_string from django.template.loader import render_to_string
from myapi.models import Film from myapi.models import Film
@ -6,16 +7,22 @@ prices = {"one_cof": 4, "one_exte": 5, "card_cof": 30, "card_exte": 35}
def render_com(template_name, film: Film) -> str: def render_com(template_name, film: Film) -> str:
return render_to_string(template_name, {"film": film, "prices": prices}) return render_to_string(template_name, {"film": film, "prices": prices}).strip()
def bocal(film: Film) -> str: def bocal(film: Film) -> str:
return render_com("bocal.tex", film) return render_com("bocal.tex", film)
def facebook(film: Film) -> str: def facebook_text(film: Film) -> str:
return render_com("facebook.txt", film) return render_com("facebook.txt", film)
def facebook_title(film: Film) -> str:
template = Template("{{ film.title }} - {{ film.director }}")
ctx = Context({"film": film, "prices": prices})
return template.render(ctx)
def mail(film: Film) -> str: def mail(film: Film) -> str:
return render_com("mail.html", film) return render_com("mail.html", film)

View file

@ -26,9 +26,14 @@ class FilmViewSet(viewsets.ModelViewSet):
def bocal(self, request, pk=None): def bocal(self, request, pk=None):
return self.general_com_view(com.bocal) return self.general_com_view(com.bocal)
@action(detail=True, renderer_classes=[PlainTextRenderer], methods=["GET"]) @action(detail=True, methods=["GET"])
def facebook(self, request, pk=None): def facebook(self, request, pk=None):
return self.general_com_view(com.facebook) 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}
)
@action(detail=True, renderer_classes=[StaticHTMLRenderer], methods=["GET"]) @action(detail=True, renderer_classes=[StaticHTMLRenderer], methods=["GET"])
def newsletter(self, request, pk=None): def newsletter(self, request, pk=None):