home · contact · privacy
Refactor redirects. master
authorChristian Heller <c.heller@plomlompom.de>
Wed, 27 Nov 2024 06:54:10 +0000 (07:54 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 27 Nov 2024 06:54:10 +0000 (07:54 +0100)
src/ytplom/misc.py

index dcb84d860f8281308c45fc84fb8b64ef4c3e1e23..59a9a934983f67107ad582a4a4a4cd6e2cace157 100644 (file)
@@ -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: