1
0
mirror of https://github.com/Damillora/Altessimo synced 2024-11-21 21:47:33 +00:00

Add TinyMCE for admin

This commit is contained in:
Damillora 2020-12-17 19:21:37 +07:00
parent 5205dab431
commit 56a3460160
10 changed files with 150 additions and 30 deletions

View File

@ -1,8 +1,54 @@
from django.contrib import admin
from . import models
from django import forms
from .models import Artist
from categories.models import Category
# Register your models here.
class ArtistForm(forms.ModelForm):
categories_str = forms.CharField(label='Category', required=False)
aliases_str = forms.CharField(label='Aliases')
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() )
class ArtistAdmin(admin.ModelAdmin):
form = ArtistForm
search_fields = ['romanized_name','name']
admin.site.register(models.Artist, ArtistAdmin)
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)

View File

@ -4,17 +4,10 @@ from django.utils.text import slugify
# Create your models here.
class ArtistManager(models.Manager):
def create_or_new(self, romanized_name):
romanized_name = romanized_name.strip()
qs = self.get_queryset().filter(romanized_name__iexact=romanized_name)
if qs.exists():
return qs.first(), False
return Artist.objects.create(romanized_name=romanized_name), True
def comma_to_qs(self, artists_str):
final_ids = []
for artist in artists_str.split(','):
obj, created = self.create_or_new(artist.strip())
obj, created = self.get_or_create(romanized_name=artist.strip())
final_ids.append(obj.id)
qs = self.get_queryset().filter(id__in=final_ids).distinct()
return qs

View File

@ -50,9 +50,9 @@
</tbody>
</table>
<h2>About Artist</h2>
<p>{{ artist.about_artist }}</p>
<div>{{ artist.about_artist|safe }}</div>
<h2>About Music</h2>
<p>{{ artist.about_music }}</p>
<div>{{ artist.about_music|safe }}</div>
<h2>Credits</h2>
{% include 'components/song-table.html' with songs=credit_songs %}
<h2>Sample works outside Idolmaster</h2>

View File

@ -1,13 +1,47 @@
from django.contrib import admin
from django import forms
from . import models
from .models import Branch, Category
# Register your models here.
class BranchForm(forms.ModelForm):
class Meta:
model = Branch
exclude = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['description'].widget.attrs['class'] = 'tm-textfield'
class CategoryForm(forms.ModelForm):
class Meta:
model = Category
exclude = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['description'].widget.attrs['class'] = 'tm-textfield'
class BranchAdmin(admin.ModelAdmin):
form = BranchForm
search_fields = ['name', 'acronym']
class Media:
js = (
'https://cdnjs.cloudflare.com/ajax/libs/tinymce/5.6.2/tinymce.min.js',
'tinymce-init.js'
)
class CategoryAdmin(admin.ModelAdmin):
form = CategoryForm
search_fields = ['name']
admin.site.register(models.Branch, BranchAdmin)
admin.site.register(models.Category,CategoryAdmin)
class Media:
js = (
'https://cdnjs.cloudflare.com/ajax/libs/tinymce/5.6.2/tinymce.min.js',
'tinymce-init.js'
)
admin.site.register(Branch, BranchAdmin)
admin.site.register(Category,CategoryAdmin)

View File

@ -12,17 +12,10 @@ class Branch(models.Model):
# Create your models here.
class CategoryManager(models.Manager):
def create_or_new(self, name):
name = name.strip()
qs = self.get_queryset().filter(name__iexact=name)
if qs.exists():
return qs.first(), False
return Category.objects.create(name=name), True
def comma_to_qs(self, categorys_str):
def comma_to_qs(self, categories_str):
final_ids = []
for category in categories_str.split(','):
obj, created = self.create_or_new(category)
obj, created = self.get_or_create(name=category.strip())
final_ids.append(obj.id)
qs = self.get_queryset().filter(id__in=final_ids).distinct()
return qs

View File

@ -3,7 +3,7 @@
{% block content %}
<h1>[{{ branch.acronym }}] {{ branch.name }}</h1>
<h2>Description</h2>
<p>{{ branch.description }}</p>
<div>{{ branch.description|safe }}</div>
<h2>Songs</h2>
{% include 'components/song-table.html' with songs=page_obj %}
<nav aria-label="Pages">

View File

@ -5,7 +5,7 @@
{% block content %}
<h1>Category: {{ category.name }}</h1>
<h2>Description</h2>
<p>{{ category.description }}</p>
<div>{{ category.description|safe }}</div>
<h2>Artists</h2>
<ul>
{% for artist in category.artist_set.all %}

View File

@ -0,0 +1,6 @@
tinymce.init({
selector: '.tm-textfield', // change this value according to your HTML
menubar: false,
plugins: 'advlist lists link',
toolbar: 'undo redo | styleselect | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | link',
});

View File

@ -1,13 +1,61 @@
from django import forms
from django.contrib import admin
from . import models
from .models import Song, OutsideSong
from artists.models import Artist
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() )
class SongAdmin(admin.ModelAdmin):
form = SongForm
search_fields = ['romanized_title','title']
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()
class OutsideSongAdmin(admin.ModelAdmin):
search_fields = ['title']
admin.site.register(models.Song, SongAdmin)
admin.site.register(models.OutsideSong, OutsideSongAdmin)
admin.site.register(Song, SongAdmin)
admin.site.register(OutsideSong, OutsideSongAdmin)

View File

@ -46,5 +46,5 @@
</tbody>
</table>
<h2>Impression</h2>
<p>{{ song.impression }}</p>
<div>{{ song.impression|safe }}</div>
{% endblock %}