From: Christian Heller Date: Wed, 27 Nov 2024 06:54:10 +0000 (+0100) Subject: Refactor redirects. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%22https:/validator.w3.org/pick_tasks?a=commitdiff_plain;p=ytplom Refactor redirects. --- diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py index dcb84d8..59a9a93 100644 --- a/src/ytplom/misc.py +++ b/src/ytplom/misc.py @@ -701,6 +701,9 @@ class TaskHandler(BaseHTTPRequestHandler): if content: self.wfile.write(content) + def _redirect(self, target: PathStr) -> None: + self._send_http(headers=[('Location', target)], code=302) + def do_POST(self) -> None: # pylint:disable=invalid-name """Map POST requests to handlers for various paths.""" url = urlparse(self.path) @@ -728,7 +731,7 @@ class TaskHandler(BaseHTTPRequestHandler): elif 'reload' == command: self.server.player.reload() sleep(0.5) # avoid redir happening before current_file update - self._send_http(headers=[('Location', '/')], code=302) + self._redirect(PathStr('/')) def _receive_video_flag(self, rel_path_b64: B64Str, @@ -743,9 +746,7 @@ class TaskHandler(BaseHTTPRequestHandler): file.save(conn) conn.commit_close() file.ensure_absence_if_deleted() - self._send_http(headers=[('Location', - f'/{PAGE_NAMES["file"]}/{rel_path_b64}')], - code=302) + self._redirect(PathStr(f'/{PAGE_NAMES["file"]}/{rel_path_b64}')) def _receive_yt_query(self, query_txt: QueryText) -> None: conn = DbConnection() @@ -797,10 +798,7 @@ class TaskHandler(BaseHTTPRequestHandler): result.save(conn) result.save_to_query(conn, query_data.id_) conn.commit_close() - self._send_http( - headers=[('Location', - f'/{PAGE_NAMES["yt_query"]}/{query_data.id_}')], - code=302) + self._redirect(PathStr(f'/{PAGE_NAMES["yt_query"]}/{query_data.id_}')) def do_GET(self) -> None: # pylint:disable=invalid-name """Map GET requests to handlers for various paths.""" @@ -863,9 +861,7 @@ class TaskHandler(BaseHTTPRequestHandler): except NotFoundException: conn.commit_close() self.server.downloads.queue_download(video_id) - self._send_http(headers=[('Location', - f'/{PAGE_NAMES["yt_result"]}/{video_id}')], - code=302) + self._redirect(PathStr(f'/{PAGE_NAMES["yt_result"]}/{video_id}')) return conn.commit_close() with open(file_data.full_path, 'rb') as video_file: