From 135208d9d12d46246922bc97d4ff3778b6e29105 Mon Sep 17 00:00:00 2001 From: Dirk Eschler Date: Fri, 18 Dec 2009 15:08:39 +0000 Subject: [PATCH] Added support for inlines. --- modeltranslation/admin.py | 64 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/modeltranslation/admin.py b/modeltranslation/admin.py index ca66804..80b981b 100644 --- a/modeltranslation/admin.py +++ b/modeltranslation/admin.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from copy import deepcopy from django.conf import settings @@ -11,6 +12,7 @@ from django.utils.safestring import mark_safe from modeltranslation.translator import translator + class TranslationAdmin(admin.ModelAdmin): def patch_translation_field(self, db_field, field, **kwargs): @@ -19,7 +21,6 @@ class TranslationAdmin(admin.ModelAdmin): # Hide the original field by making it non-editable. if db_field.name in trans_opts.fields: db_field.editable = False - # field.widget.attrs['readonly'] = "true" # For every localized field copy the widget from the original field if db_field.name in trans_opts.localized_fieldnames_rev: @@ -34,14 +35,12 @@ class TranslationAdmin(admin.ModelAdmin): field.widget = deepcopy(orig_formfield.widget) - def formfield_for_dbfield(self, db_field, **kwargs): # Call the baseclass function to get the formfield field = super(TranslationAdmin, self).formfield_for_dbfield(db_field, **kwargs) self.patch_translation_field(db_field, field, **kwargs) - return field + return field - #def save_model(self, request, obj, form, change): #""" #Given a model instance save it to the database. @@ -61,4 +60,61 @@ class TranslationAdmin(admin.ModelAdmin): ## Call the baseclass method #super(TranslationAdmin, self).save_model(request, obj, form, change) + +class TranslationTabularInline(admin.TabularInline): + + def patch_translation_field(self, db_field, field, **kwargs): + trans_opts = translator.get_options_for_model(self.model) + + # Hide the original field by making it non-editable. + if db_field.name in trans_opts.fields: + db_field.editable = False + + # For every localized field copy the widget from the original field + if db_field.name in trans_opts.localized_fieldnames_rev: + orig_fieldname = trans_opts.localized_fieldnames_rev[db_field.name] + orig_formfield = self.formfield_for_dbfield(self.model._meta.get_field(orig_fieldname), **kwargs) + + # In case the original form field was required, make the default + # translation field required instead. + if db_field.language == settings.LANGUAGES[0][0] and orig_formfield.required: + orig_formfield.required = False + field.required = True + + field.widget = deepcopy(orig_formfield.widget) + + def formfield_for_dbfield(self, db_field, **kwargs): + # Call the baseclass function to get the formfield + field = super(TranslationTabularInline, self).formfield_for_dbfield(db_field, **kwargs) + self.patch_translation_field(db_field, field, **kwargs) + return field + + +class TranslationStackedInline(admin.StackedInline): + + def patch_translation_field(self, db_field, field, **kwargs): + trans_opts = translator.get_options_for_model(self.model) + + # Hide the original field by making it non-editable. + if db_field.name in trans_opts.fields: + db_field.editable = False + + # For every localized field copy the widget from the original field + if db_field.name in trans_opts.localized_fieldnames_rev: + orig_fieldname = trans_opts.localized_fieldnames_rev[db_field.name] + orig_formfield = self.formfield_for_dbfield(self.model._meta.get_field(orig_fieldname), **kwargs) + + # In case the original form field was required, make the default + # translation field required instead. + if db_field.language == settings.LANGUAGES[0][0] and orig_formfield.required: + orig_formfield.required = False + field.required = True + + field.widget = deepcopy(orig_formfield.widget) + + def formfield_for_dbfield(self, db_field, **kwargs): + # Call the baseclass function to get the formfield + field = super(TranslationStackedInline, self).formfield_for_dbfield(db_field, **kwargs) + self.patch_translation_field(db_field, field, **kwargs) + return field \ No newline at end of file