Skip to content

Commit

Permalink
Add redirect back to a correct page after editing/deleting a song
Browse files Browse the repository at this point in the history
  • Loading branch information
pehala committed Jan 19, 2024
1 parent 912bb94 commit f6f9aa2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
17 changes: 17 additions & 0 deletions backend/mixins.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""General mixins"""
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin

from backend.auth import is_localadmin, is_superadmin

# pylint: disable=no-member


class RegenerateViewMixin:
"""Mixin which tell you if the object changed or not"""
Expand Down Expand Up @@ -42,3 +45,17 @@ class SuperAdminRequired(LoginRequiredMixin, UserPassesTestMixin):

def test_func(self):
return is_superadmin(self.request)


class RedirectToNextMixin:
"""Mixin for redirecting to the page specified in next parameter"""

default_next_page = None

def get_success_url(self):
"""Returns success_url to redirect to"""
if "next" in self.request.GET:
return self.request.GET["next"]
if "next" in self.request.POST:
return self.request.POST["next"]
return self.default_next_page
4 changes: 2 additions & 2 deletions backend/templates/songs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ <h6 class="d-inline" ><%:author%></h6>
const canWakeLock = () => 'wakeLock' in navigator;

{% if user.is_authenticated %}
const baseEditUrl = "{% url "chords:edit" pk="0" %}"
const baseDeleteUrl = "{% url "chords:delete" pk="0" %}"
const baseEditUrl = "{% url "chords:edit" pk="0" %}?next={{ request.path }}"
const baseDeleteUrl = "{% url "chords:delete" pk="0" %}?next={{ request.path }}"
{% endif %}

$.views.settings.delimiters("<%", "%>");
Expand Down
17 changes: 12 additions & 5 deletions backend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from django.utils.translation import gettext_lazy as _
from django.views.generic import ListView, CreateView, UpdateView, DeleteView, RedirectView

from backend.mixins import RegenerateViewMixin, PassRequestToFormMixin, LocalAdminRequired
from backend.forms import SongForm
from backend.mixins import RegenerateViewMixin, PassRequestToFormMixin, LocalAdminRequired, RedirectToNextMixin
from backend.models import Song
from backend.utils import regenerate_pdf, regenerate_prerender

Expand Down Expand Up @@ -97,13 +97,20 @@ def get_success_url(self):
return super().get_success_url()


class SongUpdateView(LocalAdminRequired, PassRequestToFormMixin, SuccessMessageMixin, RegenerateViewMixin, UpdateView):
class SongUpdateView(
LocalAdminRequired,
PassRequestToFormMixin,
SuccessMessageMixin,
RegenerateViewMixin,
RedirectToNextMixin,
UpdateView,
):
"""Updates existing song"""

form_class = SongForm
model = Song
template_name = "songs/add.html"
success_url = reverse_lazy("backend:index")
default_next_page = reverse_lazy("backend:index")
success_message = _("Song %(name)s was successfully updated")

def post(self, request, *args, **kwargs):
Expand All @@ -114,12 +121,12 @@ def post(self, request, *args, **kwargs):
return response


class SongDeleteView(LocalAdminRequired, DeleteView):
class SongDeleteView(LocalAdminRequired, RedirectToNextMixin, DeleteView):
"""Removes song"""

model = Song
template_name = "songs/confirm_delete.html"
success_url = reverse_lazy("backend:index")
default_next_page = reverse_lazy("backend:index")
success_message = _("Song %s was successfully deleted")

def post(self, request, *args, **kwargs):
Expand Down

0 comments on commit f6f9aa2

Please sign in to comment.