tatterdemalion / django-nece

Content Translation Framework based on Postgresql's JSONB field
BSD 3-Clause "New" or "Revised" License
83 stars 25 forks source link

Admin interface #10

Open tatterdemalion opened 8 years ago

tatterdemalion commented 8 years ago

Nece translations field is already on django admin as a textarea. It is incredibly hard to change and maintain. We can use the contents of this field to create dynamic form elements directly from the frontend. There are various JSON to form renderer libraries. Make a research and find one suitable. Otherwise write one on your own.

Natim commented 6 years ago

For instance you can use https://github.com/abogushov/django-admin-json-editor I am tempted to build something that would configure it automatically based on the TRANSLATION_MAP settings. Does anyone uses anything else?

Natim commented 6 years ago

Here is how I managed to do it with django-admin-json-editor:

settings.py

INSTALLED_APPS += ['django-admin-json-editor']

LANGUAGE_CODE = 'en'
LANGUAGES = [
    ('de', 'German - Deutch'),
    ('en', 'English'),
    ('es', 'Spanish - Español'),
    ('fr', 'French - Français'),
    ('it', 'Italian - Italiano'),
    ('jp', 'Japanese - 日本語'),
    ('ko', 'Korean - 한국어'),
    ('pt', 'Portugese - Português'),
    ('th', 'Thai - ไทย'),
    ('vi', 'Vietnamese - Tiếng Việt'),
    ('zh', 'Chinese - 中文'),
]

# django-nece
TRANSLATIONS_DEFAULT = LANGUAGE_CODE
TRANSLATION_MAP = {lang[0]: lang[0] for lang in LANGUAGES}

admin.py

from django.core.exceptions import ValidationError 
from django import forms 

from django_admin_json_editor import JSONEditorWidget 

def translatable_schema(widget):
    return {
        'type': 'object',
        'required': [LANGUAGE_CODE],
        'properties': {
            language_code: {
                'type': 'object',
                'additionnal_properties': False,
                'required': ['name'],
                'properties': {
                    'name': {
                        'type': 'string',
                        'title': 'Name in {}'.format(language_title)
                    }
                }
            } for language_code, language_title in LANGUAGES
    }

class CategoryAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['translations'].widget = JSONEditorWidget(translatable_schema, False)
        # self.fields['translations'].initial = translatable_value()

    def clean(self):
        cleaned_data = super().clean()
        translations = cleaned_data['translations']
        if not translations or LANGUAGE_CODE not in translations:
            raise ValidationError("Please give at least an English name")
        self.instance.name = cleaned_data['translations'][LANGUAGE_CODE]['name']

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': ('translations')
        }),
    )

    form = CategoryAdminForm
chubz commented 6 years ago

I really like the approach you took here and went to implement it in my project to test it out.

Would it be good idea to add a method to TranslatableModel which would generate a jsonschema that could be used to validate values when saving then?

Also it could be used for the admin widget as a way to generate proper forms etc.

Natim commented 6 years ago

@chubz Yes definitely.

I did it by hand because it was quicker and I am used to write jsonschema however having it generated automatically would be much better.

k0t3n commented 5 years ago

@chubz @Natim please, see #35