Implemented Game/Album changing to Song in admin.
This commit is contained in:
parent
476ddb7a5a
commit
739b2bddcb
3 changed files with 74 additions and 28 deletions
|
@ -1,5 +1,5 @@
|
|||
from django.contrib import messages
|
||||
from django.forms import inlineformset_factory
|
||||
from django import forms
|
||||
from django.contrib import admin, messages
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import render
|
||||
from django.utils import timezone
|
||||
|
@ -7,14 +7,23 @@ from django.utils import timezone
|
|||
from core.utils import create_success_message, quantify
|
||||
|
||||
|
||||
def change_m2m_items(request, queryset, parent_model, m2m_field,
|
||||
child_field, calling_function, remove=False):
|
||||
through_model = getattr(parent_model, m2m_field)
|
||||
ItemFormSet = inlineformset_factory(parent_model,
|
||||
through_model.through,
|
||||
fields=(child_field,),
|
||||
can_delete=False,
|
||||
extra=10,)
|
||||
def change_items(request, queryset, parent_field, calling_function,
|
||||
m2m=None, remove=False):
|
||||
through_field = getattr(queryset.model, parent_field)
|
||||
child_model = through_field.field.related_model
|
||||
|
||||
class ItemForm(forms.Form):
|
||||
# _selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
|
||||
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 'apply' in request.POST:
|
||||
# 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 parent in queryset:
|
||||
through = getattr(parent, m2m_field)
|
||||
if request.POST['removal'] == 'True':
|
||||
through.remove(child[child_field])
|
||||
if m2m:
|
||||
through_instance = getattr(parent, parent_field)
|
||||
if request.POST['removal'] == 'True':
|
||||
through_instance.remove(child[m2m])
|
||||
else:
|
||||
through_instance.add(child[m2m])
|
||||
else:
|
||||
through.add(child[child_field])
|
||||
setattr(parent, parent_field, child['item'])
|
||||
parent.save()
|
||||
|
||||
# Return with informative success message and counts
|
||||
message = create_success_message(parent_model,
|
||||
message = create_success_message(queryset.model,
|
||||
queryset.count(),
|
||||
through_model.field.related_model,
|
||||
child_model,
|
||||
len(data),
|
||||
request.POST['removal'] == 'True')
|
||||
messages.success(request, message)
|
||||
|
@ -46,14 +59,17 @@ def change_m2m_items(request, queryset, parent_model, m2m_field,
|
|||
# . . .otherwise, create empty formset.
|
||||
else:
|
||||
item_formset = ItemFormSet()
|
||||
# sel_act = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
|
||||
# item_formset = ItemForm(initial={'_selected_action': sel_act})
|
||||
|
||||
return render(request,
|
||||
'admin/change_m2m_intermediate.html',
|
||||
'admin/change_items_intermediate.html',
|
||||
{'calling_function': calling_function,
|
||||
'parent_queryset': queryset,
|
||||
'item_formset': item_formset,
|
||||
'parent_model': parent_model,
|
||||
'child_model': through_model.field.related_model,
|
||||
'parent_model': queryset.model,
|
||||
'child_model': child_model,
|
||||
'is_m2m': bool(m2m is not None),
|
||||
'is_removal': remove, })
|
||||
|
||||
|
||||
|
@ -61,3 +77,14 @@ def publish_items(request, queryset):
|
|||
rows_updated = queryset.update(published_date=timezone.now())
|
||||
message = quantify(rows_updated, queryset.model)
|
||||
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)
|
||||
|
|
|
@ -2,7 +2,7 @@ from django.contrib import admin
|
|||
from django.db import models
|
||||
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
|
||||
|
||||
|
||||
|
@ -115,7 +115,10 @@ class SongAdmin(admin.ModelAdmin):
|
|||
'_is_enabled',
|
||||
'_is_published')
|
||||
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
|
||||
exclude = ('artists',)
|
||||
|
@ -155,16 +158,32 @@ class SongAdmin(admin.ModelAdmin):
|
|||
def artist_list(self, obj):
|
||||
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):
|
||||
return change_m2m_items(request, queryset, Song, 'artists',
|
||||
'artist', 'add_artists')
|
||||
return change_items(request, queryset, 'artists', 'add_artists',
|
||||
m2m='artist')
|
||||
add_artists.short_description = "Add artists to selected items"
|
||||
|
||||
def remove_artists(self, request, queryset):
|
||||
return change_m2m_items(request, queryset, Song, 'artists',
|
||||
'artist', 'remove_artists', remove=True)
|
||||
return change_items(request, queryset, 'artists', 'remove_artists',
|
||||
m2m='artist', remove=True)
|
||||
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):
|
||||
publish_items(request, queryset)
|
||||
publish_songs.short_description = "Publish selected songs"
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
<thead>
|
||||
<tr>
|
||||
<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>
|
||||
</tr>
|
||||
</thead>
|
||||
|
@ -21,7 +21,7 @@
|
|||
<thead>
|
||||
<tr>
|
||||
<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>
|
||||
</tr>
|
||||
</thead>
|
||||
|
@ -40,6 +40,6 @@
|
|||
<input type="hidden" name="action" value="{{ calling_function }}" />
|
||||
<input type="hidden" name="removal" value="{{ is_removal }}" />
|
||||
<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>
|
||||
{% endblock %}
|
Loading…
Reference in a new issue