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

55 lines
1.8 KiB
Python
Raw Normal View History

2020-12-15 21:32:20 +00:00
from django.contrib import admin
2020-12-17 12:21:37 +00:00
from django import forms
from .models import Artist
from categories.models import Category
2020-12-15 21:32:20 +00:00
# Register your models here.
2020-12-17 12:21:37 +00:00
class ArtistForm(forms.ModelForm):
categories_str = forms.CharField(label='Category', required=False)
2020-12-17 12:32:12 +00:00
aliases_str = forms.CharField(label='Aliases', required=False)
2020-12-17 12:21:37 +00:00
class Meta:
model = Artist
fields = [
'name',
'romanized_name',
'slug',
'categories_str',
'aliases_str',
'about_artist',
'about_music',
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['about_artist'].widget.attrs['class'] = 'tm-textfield'
self.fields['about_music'].widget.attrs['class'] = 'tm-textfield'
instance = kwargs.get("instance")
if instance:
self.fields['categories_str'].initial = ", ".join(x.name for x in instance.category.all() )
self.fields['aliases_str'].initial = ", ".join(x.romanized_name for x in instance.aliases.all() )
2020-12-15 21:32:20 +00:00
2020-12-16 14:26:15 +00:00
class ArtistAdmin(admin.ModelAdmin):
2020-12-17 12:21:37 +00:00
form = ArtistForm
2020-12-16 14:26:15 +00:00
search_fields = ['romanized_name','name']
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):
categories_str = form.cleaned_data.get('categories_str')
categories = Category.objects.comma_to_qs(categories_str)
aliases_str = form.cleaned_data.get('aliases_str')
aliases = Artist.objects.comma_to_qs(aliases_str)
if not obj.id:
obj.save()
obj.category.clear()
obj.category.add(*categories)
obj.aliases.clear()
obj.aliases.add(*aliases)
obj.save()
admin.site.register(Artist, ArtistAdmin)