From 39e035b4607f8e125c656094c9e0eaad8ce96e37 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Tue, 21 Feb 2023 00:17:31 -0500 Subject: [PATCH] Clean up URLs and templates --- vrobbler/apps/scrobbles/urls.py | 15 ++++++ vrobbler/apps/scrobbles/views.py | 46 +++++++++++-------- vrobbler/settings.py | 2 + vrobbler/templates/base.html | 12 +++-- .../templates/scrobbles/scrobble_list.html | 4 +- vrobbler/templates/scrobbles/upload_form.html | 7 +-- vrobbler/urls.py | 16 +------ 7 files changed, 61 insertions(+), 41 deletions(-) diff --git a/vrobbler/apps/scrobbles/urls.py b/vrobbler/apps/scrobbles/urls.py index aa793b4..1acff90 100644 --- a/vrobbler/apps/scrobbles/urls.py +++ b/vrobbler/apps/scrobbles/urls.py @@ -4,6 +4,21 @@ from scrobbles import views app_name = 'scrobbles' urlpatterns = [ + path( + 'manual/imdb/', + views.ManualScrobbleView.as_view(), + name='imdb-manual-scrobble', + ), + path( + 'manual/audioscrobbler/', + views.AudioScrobblerImportCreateView.as_view(), + name='audioscrobbler-file-upload', + ), + path( + 'manual/koreader/', + views.KoReaderImportCreateView.as_view(), + name='koreader-file-upload', + ), path('finish/', views.scrobble_finish, name='finish'), path('cancel/', views.scrobble_cancel, name='cancel'), path( diff --git a/vrobbler/apps/scrobbles/views.py b/vrobbler/apps/scrobbles/views.py index 423efa6..b584e84 100644 --- a/vrobbler/apps/scrobbles/views.py +++ b/vrobbler/apps/scrobbles/views.py @@ -4,6 +4,7 @@ from datetime import datetime import pytz from django.conf import settings +from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.db.models.fields import timezone from django.http import FileResponse, HttpResponseRedirect, JsonResponse @@ -28,6 +29,7 @@ from rest_framework.decorators import ( from rest_framework.parsers import MultiPartParser from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response +from scrobbles.api import serializers from scrobbles.constants import ( JELLYFIN_AUDIO_ITEM_TYPES, JELLYFIN_VIDEO_ITEM_TYPES, @@ -49,7 +51,6 @@ from scrobbles.scrobblers import ( mopidy_scrobble_podcast, mopidy_scrobble_track, ) -from scrobbles.api import serializers from scrobbles.tasks import ( process_koreader_import, process_lastfm_import, @@ -295,39 +296,48 @@ def import_audioscrobbler_file(request): ) -@csrf_exempt @permission_classes([IsAuthenticated]) @api_view(['GET']) def scrobble_finish(request, uuid): user = request.user + success_url = reverse_lazy('vrobbler-home') + if not user.is_authenticated: - return Response({}, status=status.HTTP_403_FORBIDDEN) + return HttpResponseRedirect(success_url) scrobble = Scrobble.objects.filter(user=user, uuid=uuid).first() - if not scrobble: - return Response({}, status=status.HTTP_404_NOT_FOUND) - scrobble.stop(force_finish=True) - return Response( - {'id': scrobble.id, 'status': scrobble.status}, - status=status.HTTP_200_OK, - ) + if scrobble: + scrobble.stop(force_finish=True) + messages.add_message( + request, + messages.SUCCESS, + f"Scrobble of {scrobble.media_obj} finished.", + ) + else: + messages.add_message(request, messages.ERROR, "Scrobble not found.") + return HttpResponseRedirect(success_url) -@csrf_exempt @permission_classes([IsAuthenticated]) @api_view(['GET']) def scrobble_cancel(request, uuid): user = request.user + success_url = reverse_lazy('vrobbler-home') + if not user.is_authenticated: - return Response({}, status=status.HTTP_403_FORBIDDEN) + return HttpResponseRedirect(success_url) scrobble = Scrobble.objects.filter(user=user, uuid=uuid).first() - if not scrobble: - return Response({}, status=status.HTTP_404_NOT_FOUND) - scrobble.cancel() - return Response( - {'id': scrobble.id, 'status': 'cancelled'}, status=status.HTTP_200_OK - ) + if scrobble: + scrobble.cancel() + messages.add_message( + request, + messages.SUCCESS, + f"Scrobble of {scrobble.media_obj} cancelled.", + ) + else: + messages.add_message(request, messages.ERROR, "Scrobble not found.") + return HttpResponseRedirect(success_url) @permission_classes([IsAuthenticated]) diff --git a/vrobbler/settings.py b/vrobbler/settings.py index 0777da7..07c8a07 100644 --- a/vrobbler/settings.py +++ b/vrobbler/settings.py @@ -140,6 +140,8 @@ TEMPLATES = [ }, ] +MESSAGE_STORAGE = "django.contrib.messages.storage.session.SessionStorage" + WSGI_APPLICATION = "vrobbler.wsgi.application" DATABASES = { diff --git a/vrobbler/templates/base.html b/vrobbler/templates/base.html index 0981798..d05abc6 100644 --- a/vrobbler/templates/base.html +++ b/vrobbler/templates/base.html @@ -177,7 +177,7 @@ {% if user.is_authenticated %} -
+ {% csrf_token %} {{ imdb_form }}
@@ -197,6 +197,13 @@
+