1
0
mirror of https://github.com/Damillora/Altessimo synced 2024-10-31 21:17:32 +00:00
Altessimo/songs/admin.py

62 lines
2.2 KiB
Python
Raw Normal View History

2020-12-17 12:21:37 +00:00
from django import forms
2020-12-15 21:32:20 +00:00
from django.contrib import admin
2020-12-17 12:21:37 +00:00
from .models import Song, OutsideSong
from artists.models import Artist
2020-12-16 07:29:08 +00:00
2020-12-17 12:21:37 +00:00
class SongForm(forms.ModelForm):
lyricist_str = forms.CharField(label='Lyricists', required=False)
composer_str = forms.CharField(label='Composers', required=False)
arranger_str = forms.CharField(label='Arrangers', required=False)
class Meta:
model = Song
fields = [
'branch',
'title',
'romanized_title',
'lyricist_str',
'composer_str',
'arranger_str',
'impression',
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['impression'].widget.attrs['class'] = 'tm-textfield'
instance = kwargs.get("instance")
if instance:
self.fields['lyricist_str'].initial = ', '.join(x.romanized_name for x in instance.lyricist.all() )
self.fields['composer_str'].initial = ', '.join(x.romanized_name for x in instance.composer.all() )
self.fields['arranger_str'].initial = ', '.join(x.romanized_name for x in instance.arranger.all() )
2020-12-16 14:26:15 +00:00
class SongAdmin(admin.ModelAdmin):
2020-12-17 12:21:37 +00:00
form = SongForm
2020-12-16 14:26:15 +00:00
search_fields = ['romanized_title','title']
2020-12-17 12:21:37 +00:00
class Media:
js = (
'https://cdnjs.cloudflare.com/ajax/libs/tinymce/5.6.2/tinymce.min.js',
'tinymce-init.js'
)
def save_model(self, request, obj, form, change):
lyricist_str = form.cleaned_data.get('lyricist_str')
composer_str = form.cleaned_data.get('composer_str')
arranger_str = form.cleaned_data.get('arranger_str')
lyricist = Artist.objects.comma_to_qs(lyricist_str)
composer = Artist.objects.comma_to_qs(composer_str)
arranger = Artist.objects.comma_to_qs(arranger_str)
if not obj.id:
obj.save()
obj.lyricist.clear()
obj.lyricist.add(*lyricist)
obj.composer.clear()
obj.composer.add(*composer)
obj.arranger.clear()
obj.arranger.add(*arranger)
obj.save()
2020-12-16 14:26:15 +00:00
class OutsideSongAdmin(admin.ModelAdmin):
search_fields = ['title']
2020-12-17 12:21:37 +00:00
admin.site.register(Song, SongAdmin)
admin.site.register(OutsideSong, OutsideSongAdmin)