From 25485b54b982002ea6404bf69c8b2ad9df0b73c3 Mon Sep 17 00:00:00 2001 From: RecursiveGreen Date: Sat, 13 Jan 2018 00:39:13 -0500 Subject: [PATCH] Clean up Songs/Artists admin actions. --- TODO.md | 2 +- savepointradio/radio/admin.py | 69 +++++++------------ ....html => change_artists_intermediate.html} | 9 +-- .../admin/remove_artists_intermediate.html | 43 ------------ 4 files changed, 29 insertions(+), 94 deletions(-) rename savepointradio/radio/templates/admin/{add_artists_intermediate.html => change_artists_intermediate.html} (62%) delete mode 100644 savepointradio/radio/templates/admin/remove_artists_intermediate.html diff --git a/TODO.md b/TODO.md index ae7ac1f..dd07729 100644 --- a/TODO.md +++ b/TODO.md @@ -18,7 +18,7 @@ # Radio ## General -- [ ] Admin actions for Song need some serious DRY cleaning. +- [x] Admin actions for Song need some serious DRY cleaning. ## Functionality - [x] Import old radio database diff --git a/savepointradio/radio/admin.py b/savepointradio/radio/admin.py index 1445c41..3f38520 100644 --- a/savepointradio/radio/admin.py +++ b/savepointradio/radio/admin.py @@ -173,22 +173,25 @@ class SongAdmin(admin.ModelAdmin): def artist_list(self, obj): return ', '.join([a.full_name for a in obj.artists.all()]) - def add_artists(self, request, queryset): + def change_artists(self, request, queryset, remove=False): artist_formset = None - # If we clicked "Add Artists", then continue. . . + # If we clicked Submit, then continue. . . if 'apply' in request.POST: # Fill the formset with values from the POST request artist_formset = ArtistFormSet(request.POST) - # Will only returned "cleaned_data" if form is valid + # Will only returned "cleaned_data" if form is valid, so check if artist_formset.is_valid(): - # remove the empty form data from the list + # Remove the empty form data from the list data = list(filter(None, artist_formset.cleaned_data)) for artist in data: for song in queryset: - song.artists.add(artist['artist']) + if request.POST['removal'] == 'True': + song.artists.remove(artist['artist']) + else: + song.artists.add(artist['artist']) # Return with informative success message and counts a_count = len(data) @@ -196,9 +199,14 @@ class SongAdmin(admin.ModelAdmin): a_msg = ('1 artist was', '{} artists were'.format(a_count))[a_count > 1] s_msg = ('1 song', '{} songs'.format(s_count))[s_count > 1] + if request.POST['removal'] == 'True': + act_msg = 'removed from' + else: + act_msg = 'added to' self.message_user(request, - '{} successfully added to {}.'.format(a_msg, - s_msg)) + '{} successfully {} {}.'.format(a_msg, + act_msg, + s_msg)) return HttpResponseRedirect(request.get_full_path()) else: self.message_user(request, @@ -209,48 +217,17 @@ class SongAdmin(admin.ModelAdmin): artist_formset = ArtistFormSet() return render(request, - 'admin/add_artists_intermediate.html', - {'songs': queryset, 'artist_formset': artist_formset, }) + 'admin/change_artists_intermediate.html', + {'songs': queryset, + 'artist_formset': artist_formset, + 'is_removal': remove, }) + + def add_artists(self, request, queryset): + return self.change_artists(request, queryset) add_artists.short_description = "Add artists to selected items" def remove_artists(self, request, queryset): - artist_formset = None - - # If we clicked "Remove Artists", then continue. . . - if 'apply' in request.POST: - # Fill the formset with values from the POST request - artist_formset = ArtistFormSet(request.POST) - - # Will only returned "cleaned_data" if form is valid - if artist_formset.is_valid(): - # remove the empty form data from the list - data = list(filter(None, artist_formset.cleaned_data)) - - for artist in data: - for song in queryset: - song.artists.remove(artist['artist']) - - # Return with informative success message and counts - a_count = len(data) - s_count = queryset.count() - a_msg = ('1 artist was', - '{} artists were'.format(a_count))[a_count > 1] - s_msg = ('1 song', '{} songs'.format(s_count))[s_count > 1] - self.message_user(request, - '{} successfully removed from {}.'.format(a_msg, - s_msg)) - return HttpResponseRedirect(request.get_full_path()) - else: - self.message_user(request, - "See below for errors in the form.", - level=messages.ERROR) - # . . .otherwise, create empty formset. - if not artist_formset: - artist_formset = ArtistFormSet() - - return render(request, - 'admin/remove_artists_intermediate.html', - {'songs': queryset, 'artist_formset': artist_formset, }) + return self.change_artists(request, queryset, remove=True) remove_artists.short_description = "Remove artists from selected items" def publish_items(self, request, queryset): diff --git a/savepointradio/radio/templates/admin/add_artists_intermediate.html b/savepointradio/radio/templates/admin/change_artists_intermediate.html similarity index 62% rename from savepointradio/radio/templates/admin/add_artists_intermediate.html rename to savepointradio/radio/templates/admin/change_artists_intermediate.html index 078a35c..d08977a 100644 --- a/savepointradio/radio/templates/admin/add_artists_intermediate.html +++ b/savepointradio/radio/templates/admin/change_artists_intermediate.html @@ -8,7 +8,7 @@ -

Select up to ten artists to add to the songs below:

+

Select up to ten artists to {% if is_removal %}remove from{% else %}add to{% endif %} the songs below:

@@ -20,7 +20,7 @@ -

Artists will be added to the following songs:

+

Artists will be {% if is_removal %}removed from{% else %}added to{% endif %} the following songs:

@@ -36,8 +36,9 @@ - + + - + {% endblock %} \ No newline at end of file diff --git a/savepointradio/radio/templates/admin/remove_artists_intermediate.html b/savepointradio/radio/templates/admin/remove_artists_intermediate.html deleted file mode 100644 index fb41221..0000000 --- a/savepointradio/radio/templates/admin/remove_artists_intermediate.html +++ /dev/null @@ -1,43 +0,0 @@ -{% extends "admin/base_site.html" %} - -{% block content %} -
- {% csrf_token %} - {{ artist_formset.management_form }} - - - - - - - {% for form in artist_formset %} - {{ form }} - {% endfor %} -
-

Select up to ten artists to remove from the songs below:

-
- - - - - - - - {% for song in songs %} - - - - {% endfor %} - -
-

Artists will be removed from the following songs:

-
- {{ song }} - -
- - - - -
-{% endblock %} \ No newline at end of file