Clean up Songs/Artists admin actions.
This commit is contained in:
parent
497ce3f381
commit
25485b54b9
4 changed files with 29 additions and 94 deletions
2
TODO.md
2
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
|
||||
|
|
|
@ -173,21 +173,24 @@ 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:
|
||||
if request.POST['removal'] == 'True':
|
||||
song.artists.remove(artist['artist'])
|
||||
else:
|
||||
song.artists.add(artist['artist'])
|
||||
|
||||
# Return with informative success message and counts
|
||||
|
@ -196,8 +199,13 @@ 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,
|
||||
'{} successfully {} {}.'.format(a_msg,
|
||||
act_msg,
|
||||
s_msg))
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
else:
|
||||
|
@ -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):
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
<thead>
|
||||
<tr>
|
||||
<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>
|
||||
</tr>
|
||||
</thead>
|
||||
|
@ -20,7 +20,7 @@
|
|||
<thead>
|
||||
<tr>
|
||||
<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>
|
||||
</tr>
|
||||
</thead>
|
||||
|
@ -36,8 +36,9 @@
|
|||
</tbody>
|
||||
</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>
|
||||
<input type="submit" name="apply" value="Add Artists" />
|
||||
<input type="submit" name="apply" value="{% if is_removal %}Remove{% else %}Add{% endif %} Artists" />
|
||||
</form>
|
||||
{% endblock %}
|
|
@ -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 %}
|
Loading…
Reference in a new issue