aboutsummaryrefslogtreecommitdiff
path: root/user_profile
diff options
context:
space:
mode:
authorBobby <[email protected]>2024-08-26 20:34:24 -0400
committerBobby <[email protected]>2024-08-26 20:34:24 -0400
commit187867f8740cd8cca55fbc196d7a07a35d49d274 (patch)
tree8de4178dad31e55b40140dc828e5c43c03a120e0 /user_profile
parent54e21f54cf5b36406ffcea82ee49be175b5a2fbe (diff)
downloadyugen-187867f8740cd8cca55fbc196d7a07a35d49d274.tar.xz
yugen-187867f8740cd8cca55fbc196d7a07a35d49d274.zip
Update MAL from site
Diffstat (limited to 'user_profile')
-rw-r--r--user_profile/urls.py1
-rw-r--r--user_profile/views.py71
2 files changed, 62 insertions, 10 deletions
diff --git a/user_profile/urls.py b/user_profile/urls.py
index 6afb7ef..d014c7b 100644
--- a/user_profile/urls.py
+++ b/user_profile/urls.py
@@ -6,4 +6,5 @@ app_name = "user_profile"
urlpatterns = [
path("", views.user_profile, name="user_profile"),
path("save_user_preferences", views.save_user_preferences, name="save_user_preferences"),
+ path("update_user_mal_list", views.update_user_mal_list, name="update_user_mal_list"),
] \ No newline at end of file
diff --git a/user_profile/views.py b/user_profile/views.py
index dad696e..57efc10 100644
--- a/user_profile/views.py
+++ b/user_profile/views.py
@@ -1,13 +1,14 @@
import json
from django.shortcuts import render
from django.http import JsonResponse
+import requests
from user_profile.models import UserPreferences
-from authentication.utils import get_mal_redirect_uri, get_user_mal_list
+from authentication.utils import get_mal_redirect_uri, get_single_anime_mal, get_user_mal_list
def user_profile(request):
category = request.GET.get("category", "preferences")
- supported_categories = ["preferences", "anime_list"]
+ supported_categories = ["preferences", "anime_list", "update"]
if category not in supported_categories:
category = "preferences"
@@ -16,21 +17,71 @@ def user_profile(request):
"req_category": category
}
- if category == "anime_list" and (not request.user.mal_access_token):
+ if not request.user.mal_access_token:
mal_auth_uri, code_challenge = get_mal_redirect_uri()
context["mal_auth_uri"] = mal_auth_uri
else:
- offset = request.GET.get("offset", 0)
- mal_list, prev, next = get_user_mal_list(request.user.mal_access_token, limit=24, offset=offset)
- context["mal_list"] = mal_list
- if prev:
- context["prev_offset"] = prev.split("offset=")[1].split("&")[0]
- if next:
- context["next_offset"] = next.split("offset=")[1].split("&")[0]
+ if category == "anime_list":
+ offset = request.GET.get("offset", 0)
+ filters_supported = ["watching", "completed", "on_hold", "dropped", "plan_to_watch"]
+ filter = request.GET.get("filter", "")
+ if filter not in filters_supported:
+ filter = ""
+ mal_list, prev, next = get_user_mal_list(request.user.mal_access_token, limit=24, offset=offset, filter=filter)
+ context["mal_list"] = mal_list
+ if prev:
+ context["prev_offset"] = prev.split("offset=")[1].split("&")[0]
+ if next:
+ context["next_offset"] = next.split("offset=")[1].split("&")[0]
+ if filter:
+ context["filter"] = filter
+
+ if category == "update":
+ mal_id = request.GET.get("mal_id")
+ if mal_id:
+ mal_data = get_single_anime_mal(request.user.mal_access_token, mal_id)
+ if mal_data:
+ mal_data["average_episode_duration"] = mal_data["average_episode_duration"] // 60 + 1
+ context["mal_data"] = mal_data
+ context["mal_episode_range"] = range(1, mal_data["num_episodes"] + 1)
return render(request, "user_profile/user_profile.html", context)
+def update_user_mal_list(request):
+ if request.method != "POST":
+ return JsonResponse({"error": "Invalid request method"}, status=400)
+
+ user = request.user
+ data = json.loads(request.body)
+ mal_id = data.get("mal_id")
+ status = data.get("status")
+ score = data.get("score")
+ num_watched_episodes = data.get("episodes")
+
+ if status not in ["watching", "completed", "on_hold", "dropped", "plan_to_watch", "add_to_list"]:
+ return JsonResponse({"error": "Invalid status"}, status=400)
+
+ if not user.mal_access_token:
+ return JsonResponse({"error": "User has not connected their MAL account"}, status=400)
+
+ base_url = f"https://api.myanimelist.net/v2/anime/{mal_id}/my_list_status"
+ headers = {"Authorization": f"Bearer {user.mal_access_token}", "Content-Type": "application/x-www-form-urlencoded"}
+ formdata = {
+ "score": int(score),
+ "num_watched_episodes": num_watched_episodes
+ }
+
+ if status != "add_to_list":
+ formdata["status"] = status
+
+ response = requests.put(base_url, headers=headers, data=formdata)
+ if response.status_code == 200:
+ return JsonResponse({"success": "MAL list updated"}, status=200)
+ else:
+ return JsonResponse({"error": "Failed to update MAL list"}, status=500)
+
+
def save_user_preferences(request):
if request.method != "POST":
return JsonResponse({"error": "Invalid request method"}, status=400)