Clean up Songs/Artists admin actions.

This commit is contained in:
RecursiveGreen 2018-01-13 00:39:13 -05:00
parent 497ce3f381
commit 25485b54b9
4 changed files with 29 additions and 94 deletions

View file

@ -18,7 +18,7 @@
# Radio # Radio
## General ## General
- [ ] Admin actions for Song need some serious DRY cleaning. - [x] Admin actions for Song need some serious DRY cleaning.
## Functionality ## Functionality
- [x] Import old radio database - [x] Import old radio database

View file

@ -173,22 +173,25 @@ class SongAdmin(admin.ModelAdmin):
def artist_list(self, obj): def artist_list(self, obj):
return ', '.join([a.full_name for a in obj.artists.all()]) 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 artist_formset = None
# If we clicked "Add Artists", then continue. . . # If we clicked Submit, then continue. . .
if 'apply' in request.POST: if 'apply' in request.POST:
# Fill the formset with values from the POST request # Fill the formset with values from the POST request
artist_formset = ArtistFormSet(request.POST) 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(): 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)) data = list(filter(None, artist_formset.cleaned_data))
for artist in data: for artist in data:
for song in queryset: 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 # Return with informative success message and counts
a_count = len(data) a_count = len(data)
@ -196,9 +199,14 @@ class SongAdmin(admin.ModelAdmin):
a_msg = ('1 artist was', a_msg = ('1 artist was',
'{} artists were'.format(a_count))[a_count > 1] '{} artists were'.format(a_count))[a_count > 1]
s_msg = ('1 song', '{} songs'.format(s_count))[s_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, self.message_user(request,
'{} successfully added to {}.'.format(a_msg, '{} successfully {} {}.'.format(a_msg,
s_msg)) act_msg,
s_msg))
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
else: else:
self.message_user(request, self.message_user(request,
@ -209,48 +217,17 @@ class SongAdmin(admin.ModelAdmin):
artist_formset = ArtistFormSet() artist_formset = ArtistFormSet()
return render(request, return render(request,
'admin/add_artists_intermediate.html', 'admin/change_artists_intermediate.html',
{'songs': queryset, 'artist_formset': artist_formset, }) {'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" add_artists.short_description = "Add artists to selected items"
def remove_artists(self, request, queryset): def remove_artists(self, request, queryset):
artist_formset = None return self.change_artists(request, queryset, remove=True)
# 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, })
remove_artists.short_description = "Remove artists from selected items" remove_artists.short_description = "Remove artists from selected items"
def publish_items(self, request, queryset): def publish_items(self, request, queryset):

View file

@ -8,7 +8,7 @@
<thead> <thead>
<tr> <tr>
<th colspan="2"> <th colspan="2">
<p>Select up to ten artists to add to the songs below:</p> <p>Select up to ten artists to {% if is_removal %}remove from{% else %}add to{% endif %} the songs below:</p>
</th> </th>
</tr> </tr>
</thead> </thead>
@ -20,7 +20,7 @@
<thead> <thead>
<tr> <tr>
<th> <th>
<p>Artists will be added to the following songs:</p> <p>Artists will be {% if is_removal %}removed from{% else %}added to{% endif %} the following songs:</p>
</th> </th>
</tr> </tr>
</thead> </thead>
@ -36,8 +36,9 @@
</tbody> </tbody>
</table> </table>
<input type="hidden" name="action" value="add_artists" /> <input type="hidden" name="action" value="{% if is_removal %}remove_artists{% else %}add_artists{% endif %}" />
<input type="hidden" name="removal" value="{{ is_removal }}" />
<a href="./"><input type="button" name="Cancel" value="Cancel"></a> <a href="./"><input type="button" name="Cancel" value="Cancel"></a>
<input type="submit" name="apply" value="Add Artists" /> <input type="submit" name="apply" value="{% if is_removal %}Remove{% else %}Add{% endif %} Artists" />
</form> </form>
{% endblock %} {% endblock %}

View file

@ -1,43 +0,0 @@
{% extends "admin/base_site.html" %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{{ artist_formset.management_form }}
<table>
<thead>
<tr>
<th colspan="2">
<p>Select up to ten artists to remove from the songs below:</p>
</th>
</tr>
</thead>
{% for form in artist_formset %}
{{ form }}
{% endfor %}
</table>
<table>
<thead>
<tr>
<th>
<p>Artists will be removed from the following songs:</p>
</th>
</tr>
</thead>
<tbody>
{% for song in songs %}
<tr>
<th>
{{ song }}
<input type="hidden" name="_selected_action" value="{{ song.pk }}" />
</th>
</tr>
{% endfor %}
</tbody>
</table>
<input type="hidden" name="action" value="remove_artists" />
<a href="./"><input type="button" name="Cancel" value="Cancel"></a>
<input type="submit" name="apply" value="Remove Artists" />
</form>
{% endblock %}