From 8fe00e2152103ddf0724f5627baab1e80bda3a08 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 5 Feb 2022 17:46:14 +0700 Subject: [PATCH] process api return values for frontend use --- tubearchivist/api/views.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index 7ab8b47..f0923aa 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -26,13 +26,13 @@ class ApiBaseView(APIView): self.response = {"data": False} self.status_code = False self.context = False + self.default_conf = AppConfig().config def config_builder(self): """build confic context""" - default_conf = AppConfig().config self.context = { - "es_url": default_conf["application"]["es_url"], - "es_auth": default_conf["application"]["es_auth"], + "es_url": self.default_conf["application"]["es_url"], + "es_auth": self.default_conf["application"]["es_auth"], } def get_document(self, document_id): @@ -48,6 +48,19 @@ class ApiBaseView(APIView): self.response["data"] = False self.status_code = response.status_code + def process_keys(self): + """process keys for frontend""" + all_keys = self.response["data"].keys() + if "media_url" in all_keys: + media_url = self.response["data"]["media_url"] + self.response["data"]["media_url"] = f"/media/{media_url}" + if "vid_thumb_url" in all_keys: + youtube_id = self.response["data"]["youtube_id"] + vid_thumb_url = ThumbManager().vid_thumb_path(youtube_id) + cache_dir = self.default_conf["application"]["cache_dir"] + new_thumb = f"{cache_dir}/{vid_thumb_url}" + self.response["data"]["vid_thumb_url"] = new_thumb + def get_paginate(self): """add pagination detail to response""" self.response["paginate"] = False @@ -75,6 +88,7 @@ class VideoApiView(ApiBaseView): """get request""" self.config_builder() self.get_document(video_id) + self.process_keys() return Response(self.response, status=self.status_code)