feat [back]: translate tmdb details to internal film format in new view

This commit is contained in:
Alice 2022-06-10 01:26:29 +02:00
parent 640644781c
commit 59ee938913

View file

@ -1,9 +1,13 @@
from datetime import timedelta
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from tmdbv3api import TMDb, Search, Movie
from myapi.models import Film
from myapi.serializers import FilmSerializer
from ulm_cine_club_api.settings.base import TMDB_API_KEY
tmdb = TMDb()
@ -27,3 +31,30 @@ class TmdbViewSet(viewsets.ViewSet):
for film in tmdb_results
]
return Response(res)
@action(methods=["GET"], detail=True)
def fill(self, request, pk=None):
tmdb_movie = Movie().details(pk, "images,credits,keywords")
internal_movie = Film(
title=tmdb_movie["title"],
imdb_id=tmdb_movie["imdb_id"],
tmdb_id=tmdb_movie["id"],
synopsis=tmdb_movie["overview"],
poster_link=tmdb_movie["poster_path"],
duration=timedelta(minutes=tmdb_movie["runtime"]),
release_year=tmdb_movie["release_date"][:4],
director=next(person["name"] for person in tmdb_movie["credits"]["crew"] if person["job"] == "Director"),
actors=[person["name"] for person in tmdb_movie["credits"]["cast"][:5]],
origin_country=", ".join(
short_country_name(country["name"]) for country in tmdb_movie["production_countries"]
), # TODO iso to French name
is_in_color=not any(keyword["id"] == 12999 for keyword in tmdb_movie["keywords"]["keywords"])
)
serializer = FilmSerializer(internal_movie)
return Response(serializer.data)
def short_country_name(country_name: str):
if country_name == "United States of America":
return "USA"
return country_name