Implemented Game/Album changing to Song in admin.

This commit is contained in:
RecursiveGreen 2018-01-14 21:23:26 -05:00
parent 476ddb7a5a
commit 739b2bddcb
3 changed files with 74 additions and 28 deletions

View file

@ -1,5 +1,5 @@
from django.contrib import messages from django import forms
from django.forms import inlineformset_factory from django.contrib import admin, messages
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render
from django.utils import timezone from django.utils import timezone
@ -7,14 +7,23 @@ from django.utils import timezone
from core.utils import create_success_message, quantify from core.utils import create_success_message, quantify
def change_m2m_items(request, queryset, parent_model, m2m_field, def change_items(request, queryset, parent_field, calling_function,
child_field, calling_function, remove=False): m2m=None, remove=False):
through_model = getattr(parent_model, m2m_field) through_field = getattr(queryset.model, parent_field)
ItemFormSet = inlineformset_factory(parent_model, child_model = through_field.field.related_model
through_model.through,
fields=(child_field,), class ItemForm(forms.Form):
can_delete=False, # _selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
extra=10,) item = forms.ModelChoiceField(child_model.objects.all())
if m2m:
ItemFormSet = forms.inlineformset_factory(queryset.model,
through_field.through,
fields=(m2m,),
can_delete=False,
extra=10,)
else:
ItemFormSet = forms.formset_factory(ItemForm, max_num=1)
# If we clicked Submit, 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
@ -27,16 +36,20 @@ def change_m2m_items(request, queryset, parent_model, m2m_field,
for child in data: for child in data:
for parent in queryset: for parent in queryset:
through = getattr(parent, m2m_field) if m2m:
if request.POST['removal'] == 'True': through_instance = getattr(parent, parent_field)
through.remove(child[child_field]) if request.POST['removal'] == 'True':
through_instance.remove(child[m2m])
else:
through_instance.add(child[m2m])
else: else:
through.add(child[child_field]) setattr(parent, parent_field, child['item'])
parent.save()
# Return with informative success message and counts # Return with informative success message and counts
message = create_success_message(parent_model, message = create_success_message(queryset.model,
queryset.count(), queryset.count(),
through_model.field.related_model, child_model,
len(data), len(data),
request.POST['removal'] == 'True') request.POST['removal'] == 'True')
messages.success(request, message) messages.success(request, message)
@ -46,14 +59,17 @@ def change_m2m_items(request, queryset, parent_model, m2m_field,
# . . .otherwise, create empty formset. # . . .otherwise, create empty formset.
else: else:
item_formset = ItemFormSet() item_formset = ItemFormSet()
# sel_act = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
# item_formset = ItemForm(initial={'_selected_action': sel_act})
return render(request, return render(request,
'admin/change_m2m_intermediate.html', 'admin/change_items_intermediate.html',
{'calling_function': calling_function, {'calling_function': calling_function,
'parent_queryset': queryset, 'parent_queryset': queryset,
'item_formset': item_formset, 'item_formset': item_formset,
'parent_model': parent_model, 'parent_model': queryset.model,
'child_model': through_model.field.related_model, 'child_model': child_model,
'is_m2m': bool(m2m is not None),
'is_removal': remove, }) 'is_removal': remove, })
@ -61,3 +77,14 @@ def publish_items(request, queryset):
rows_updated = queryset.update(published_date=timezone.now()) rows_updated = queryset.update(published_date=timezone.now())
message = quantify(rows_updated, queryset.model) message = quantify(rows_updated, queryset.model)
messages.success(request, '{} successfully published.'.format(message)) messages.success(request, '{} successfully published.'.format(message))
def remove_items(request, queryset, parent_field, calling_function):
through_field = getattr(queryset.model, parent_field)
child_model = through_field.field.related_model
for parent in queryset:
setattr(parent, parent_field, None)
parent.save()
message = create_success_message(queryset.model, queryset.count(),
child_model, 1, True)
messages.success(request, message)

View file

@ -2,7 +2,7 @@ from django.contrib import admin
from django.db import models from django.db import models
from django.forms import TextInput from django.forms import TextInput
from .actions import change_m2m_items, publish_items from .actions import change_items, publish_items, remove_items
from .models import Album, Artist, Game, Song from .models import Album, Artist, Game, Song
@ -115,7 +115,10 @@ class SongAdmin(admin.ModelAdmin):
'_is_enabled', '_is_enabled',
'_is_published') '_is_published')
search_fields = ['title'] search_fields = ['title']
actions = ['publish_songs', 'add_artists', 'remove_artists'] actions = ['publish_songs',
'add_game', 'remove_game',
'add_album', 'remove_album',
'add_artists', 'remove_artists']
# Edit Form display # Edit Form display
exclude = ('artists',) exclude = ('artists',)
@ -155,16 +158,32 @@ 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_album(self, request, queryset):
return change_items(request, queryset, 'album', 'add_album')
add_album.short_description = "Add album to selected items"
def remove_album(self, request, queryset):
return remove_items(request, queryset, 'album', 'remove_album')
remove_album.short_description = "Remove album from selected items"
def add_artists(self, request, queryset): def add_artists(self, request, queryset):
return change_m2m_items(request, queryset, Song, 'artists', return change_items(request, queryset, 'artists', 'add_artists',
'artist', 'add_artists') m2m='artist')
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):
return change_m2m_items(request, queryset, Song, 'artists', return change_items(request, queryset, 'artists', 'remove_artists',
'artist', 'remove_artists', remove=True) m2m='artist', remove=True)
remove_artists.short_description = "Remove artists from selected items" remove_artists.short_description = "Remove artists from selected items"
def add_game(self, request, queryset):
return change_items(request, queryset, 'game', 'add_game')
add_game.short_description = "Add game to selected items"
def remove_game(self, request, queryset):
return remove_items(request, queryset, 'game', 'remove_game')
remove_game.short_description = "Remove game from selected items"
def publish_songs(self, request, queryset): def publish_songs(self, request, queryset):
publish_items(request, queryset) publish_items(request, queryset)
publish_songs.short_description = "Publish selected songs" publish_songs.short_description = "Publish selected songs"

View file

@ -9,7 +9,7 @@
<thead> <thead>
<tr> <tr>
<th colspan="2"> <th colspan="2">
<p>Select up to ten {% model_name_plural child_model %} to {% if is_removal %}remove from{% else %}add to{% endif %} the {% model_name_plural parent_model %} below:</p> <p>Select {% if is_m2m %}up to ten {% model_name_plural child_model %}{% else %}the {% model_name child_model %}{% endif %} to {% if is_removal %}remove from{% else %}add to{% endif %} the {% model_name_plural parent_model %} below:</p>
</th> </th>
</tr> </tr>
</thead> </thead>
@ -21,7 +21,7 @@
<thead> <thead>
<tr> <tr>
<th> <th>
<p>{% filter capfirst %}{% model_name_plural child_model %}{% endfilter %} will be {% if is_removal %}removed from{% else %}added to{% endif %} the following {% model_name_plural parent_model %}:</p> <p>The {% if is_m2m %}{% model_name_plural child_model %}{% else %}{% model_name child_model %}{% endif %} will be {% if is_removal %}removed from{% else %}added to{% endif %} the following {% model_name_plural parent_model %}:</p>
</th> </th>
</tr> </tr>
</thead> </thead>
@ -40,6 +40,6 @@
<input type="hidden" name="action" value="{{ calling_function }}" /> <input type="hidden" name="action" value="{{ calling_function }}" />
<input type="hidden" name="removal" value="{{ is_removal }}" /> <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="{% if is_removal %}Remove{% else %}Add{% endif %} {% filter capfirst %}{% model_name_plural child_model %}{% endfilter %}" /> <input type="submit" name="apply" value="{% if is_removal %}Remove{% else %}Add{% endif %} {% filter capfirst %}{% if is_m2m %}{% model_name_plural child_model %}{% else %}{% model_name child_model %}{% endif %}{% endfilter %}" />
</form> </form>
{% endblock %} {% endblock %}