From d677f9579ea4df5be44bf24395b1cd7f3a620cf4 Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 1 Nov 2023 22:49:33 +0700 Subject: [PATCH] replace old process view, use user conf api --- tubearchivist/home/src/frontend/api_calls.py | 83 ------------------- .../home/templates/home/channel_id.html | 4 +- tubearchivist/home/templates/home/home.html | 4 +- tubearchivist/home/urls.py | 1 - tubearchivist/home/views.py | 17 +--- tubearchivist/static/script.js | 40 ++++----- 6 files changed, 23 insertions(+), 126 deletions(-) delete mode 100644 tubearchivist/home/src/frontend/api_calls.py diff --git a/tubearchivist/home/src/frontend/api_calls.py b/tubearchivist/home/src/frontend/api_calls.py deleted file mode 100644 index 0d9dc6c..0000000 --- a/tubearchivist/home/src/frontend/api_calls.py +++ /dev/null @@ -1,83 +0,0 @@ -""" -Functionality: -- collection of functions and tasks from frontend -- called via user input -""" - -from home.src.ta.users import UserConfig - - -class PostData: - """ - map frontend http post values to backend funcs - handover long running tasks to celery - """ - - def __init__(self, post_dict, current_user): - self.post_dict = post_dict - self.to_exec, self.exec_val = list(post_dict.items())[0] - self.current_user = current_user - - def run_task(self): - """execute and return task result""" - to_exec = self.exec_map() - task_result = to_exec() - return task_result - - def exec_map(self): - """map dict key and return function to execute""" - exec_map = { - "change_view": self._change_view, - "change_grid": self._change_grid, - "sort_order": self._sort_order, - "hide_watched": self._hide_watched, - "show_subed_only": self._show_subed_only, - "show_ignored_only": self._show_ignored_only, - } - - return exec_map[self.to_exec] - - def _change_view(self): - """process view changes in home, channel, and downloads""" - view, setting = self.exec_val.split(":") - UserConfig(self.current_user).set_value(f"view_style_{view}", setting) - return {"success": True} - - def _change_grid(self): - """process change items in grid""" - grid_items = int(self.exec_val) - grid_items = max(grid_items, 3) - grid_items = min(grid_items, 7) - UserConfig(self.current_user).set_value("grid_items", grid_items) - return {"success": True} - - def _sort_order(self): - """change the sort between published to downloaded""" - if self.exec_val in ["asc", "desc"]: - UserConfig(self.current_user).set_value( - "sort_order", self.exec_val - ) - else: - UserConfig(self.current_user).set_value("sort_by", self.exec_val) - return {"success": True} - - def _hide_watched(self): - """toggle if to show watched vids or not""" - UserConfig(self.current_user).set_value( - "hide_watched", bool(int(self.exec_val)) - ) - return {"success": True} - - def _show_subed_only(self): - """show or hide subscribed channels only on channels page""" - UserConfig(self.current_user).set_value( - "show_subed_only", bool(int(self.exec_val)) - ) - return {"success": True} - - def _show_ignored_only(self): - """switch view on /downloads/ to show ignored only""" - UserConfig(self.current_user).set_value( - "show_ignored_only", bool(int(self.exec_val)) - ) - return {"success": True} diff --git a/tubearchivist/home/templates/home/channel_id.html b/tubearchivist/home/templates/home/channel_id.html index 0d45bd8..c51fbd2 100644 --- a/tubearchivist/home/templates/home/channel_id.html +++ b/tubearchivist/home/templates/home/channel_id.html @@ -77,13 +77,13 @@
Sort by: - - diff --git a/tubearchivist/home/templates/home/home.html b/tubearchivist/home/templates/home/home.html index 59a3478..0724381 100644 --- a/tubearchivist/home/templates/home/home.html +++ b/tubearchivist/home/templates/home/home.html @@ -60,13 +60,13 @@
Sort by: - - diff --git a/tubearchivist/home/urls.py b/tubearchivist/home/urls.py index 2c2388c..363f505 100644 --- a/tubearchivist/home/urls.py +++ b/tubearchivist/home/urls.py @@ -58,7 +58,6 @@ urlpatterns = [ login_required(views.SettingsActionsView.as_view()), name="settings_actions", ), - path("process/", login_required(views.process), name="process"), path( "channel/", login_required(views.ChannelView.as_view()), diff --git a/tubearchivist/home/views.py b/tubearchivist/home/views.py index dae3d17..bc3a97b 100644 --- a/tubearchivist/home/views.py +++ b/tubearchivist/home/views.py @@ -4,7 +4,6 @@ Functionality: - holds base classes to inherit from """ import enum -import json import urllib.parse from time import sleep @@ -14,7 +13,7 @@ from django.conf import settings from django.contrib.auth import login from django.contrib.auth.decorators import user_passes_test from django.contrib.auth.forms import AuthenticationForm -from django.http import Http404, JsonResponse +from django.http import Http404 from django.shortcuts import redirect, render from django.utils.decorators import method_decorator from django.views import View @@ -23,7 +22,6 @@ from home.src.download.yt_dlp_base import CookieHandler from home.src.es.backup import ElasticBackup from home.src.es.connect import ElasticWrap from home.src.es.snapshot import ElasticSnapshot -from home.src.frontend.api_calls import PostData from home.src.frontend.forms import ( AddToQueueForm, ApplicationSettingsForm, @@ -1133,16 +1131,3 @@ class SettingsActionsView(MinView): ) return render(request, "home/settings_actions.html", context) - - -def process(request): - """handle all the buttons calls via POST ajax""" - if request.method == "POST": - current_user = request.user.id - post_dict = json.loads(request.body.decode()) - post_handler = PostData(post_dict, current_user) - if post_handler.to_exec: - task_result = post_handler.run_task() - return JsonResponse(task_result) - - return JsonResponse({"success": False}) diff --git a/tubearchivist/static/script.js b/tubearchivist/static/script.js index 07b4f20..0bcfac1 100644 --- a/tubearchivist/static/script.js +++ b/tubearchivist/static/script.js @@ -2,9 +2,11 @@ /* globals checkMessages */ -function sortChange(sortValue) { - let payload = JSON.stringify({ sort_order: sortValue }); - sendPost(payload); +function sortChange(button) { + let apiEndpoint = '/api/config/user/'; + let data = {}; + data[button.name] = button.value; + apiRequest(apiEndpoint, 'POST', data); setTimeout(function () { location.reload(); }, 500); @@ -105,17 +107,21 @@ function subscribeStatus(subscribeButton) { function changeView(image) { let sourcePage = image.getAttribute('data-origin'); let newView = image.getAttribute('data-value'); - let payload = JSON.stringify({ change_view: sourcePage + ':' + newView }); - sendPost(payload); + let apiEndpoint = '/api/config/user/'; + let data = {}; + data[`view_style_${sourcePage}`] = newView; + console.log(data); + apiRequest(apiEndpoint, 'POST', data); setTimeout(function () { location.reload(); }, 500); } function changeGridItems(image) { - let operator = image.getAttribute('data-value'); - let payload = JSON.stringify({ change_grid: operator }); - sendPost(payload); + let newGridItems = Number(image.getAttribute('data-value')); + let apiEndpoint = '/api/config/user/'; + let data = { grid_items: newGridItems }; + apiRequest(apiEndpoint, 'POST', data); setTimeout(function () { location.reload(); }, 500); @@ -123,12 +129,10 @@ function changeGridItems(image) { function toggleCheckbox(checkbox) { // pass checkbox id as key and checkbox.checked as value - let toggleId = checkbox.id; - let toggleVal = checkbox.checked; - let payloadDict = {}; - payloadDict[toggleId] = toggleVal; - let payload = JSON.stringify(payloadDict); - sendPost(payload); + let apiEndpoint = '/api/config/user/'; + let data = {}; + data[checkbox.id] = checkbox.checked; + apiRequest(apiEndpoint, 'POST', data); setTimeout(function () { let currPage = window.location.pathname; window.location.replace(currPage); @@ -1304,14 +1308,6 @@ function populateEmpty() { // generic -function sendPost(payload) { - let http = new XMLHttpRequest(); - http.open('POST', '/process/', true); - http.setRequestHeader('X-CSRFToken', getCookie('csrftoken')); - http.setRequestHeader('Content-type', 'application/json'); - http.send(payload); -} - function getCookie(c_name) { if (document.cookie.length > 0) { let c_start = document.cookie.indexOf(c_name + '=');