|
5
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
from copy import deepcopy |
|
|
3 |
|
|
|
4 |
from django.conf import settings |
|
|
5 |
from django.contrib import admin |
|
|
6 |
from django.contrib.contenttypes.models import ContentType |
|
|
7 |
from django.forms import widgets |
|
|
8 |
from django import forms, template |
|
|
9 |
from django.forms.fields import MultiValueField |
|
|
10 |
from django.shortcuts import get_object_or_404, render_to_response |
|
|
11 |
from django.utils.safestring import mark_safe |
|
|
12 |
|
|
|
13 |
from modeltranslation.translator import translator |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
class TranslationAdmin(admin.ModelAdmin): |
|
|
17 |
|
|
|
18 |
def patch_translation_field(self, db_field, field, **kwargs): |
|
|
19 |
trans_opts = translator.get_options_for_model(self.model) |
|
|
20 |
|
|
|
21 |
# Hide the original field by making it non-editable. |
|
|
22 |
if db_field.name in trans_opts.fields: |
|
|
23 |
db_field.editable = False |
|
|
24 |
|
|
|
25 |
# For every localized field copy the widget from the original field |
|
|
26 |
if db_field.name in trans_opts.localized_fieldnames_rev: |
|
|
27 |
orig_fieldname = trans_opts.localized_fieldnames_rev[db_field.name] |
|
|
28 |
orig_formfield = self.formfield_for_dbfield(self.model._meta.get_field(orig_fieldname), **kwargs) |
|
|
29 |
|
|
|
30 |
# In case the original form field was required, make the default |
|
|
31 |
# translation field required instead. |
|
|
32 |
if db_field.language == settings.LANGUAGES[0][0] and orig_formfield.required: |
|
|
33 |
orig_formfield.required = False |
|
|
34 |
field.required = True |
|
|
35 |
|
|
|
36 |
field.widget = deepcopy(orig_formfield.widget) |
|
|
37 |
|
|
|
38 |
def formfield_for_dbfield(self, db_field, **kwargs): |
|
|
39 |
# Call the baseclass function to get the formfield |
|
|
40 |
field = super(TranslationAdmin, self).formfield_for_dbfield(db_field, **kwargs) |
|
|
41 |
self.patch_translation_field(db_field, field, **kwargs) |
|
|
42 |
return field |
|
|
43 |
|
|
|
44 |
#def save_model(self, request, obj, form, change): |
|
|
45 |
#""" |
|
|
46 |
#Given a model instance save it to the database. |
|
|
47 |
|
|
|
48 |
#Because each translated field is wrapped with a descriptor to return |
|
|
49 |
#the translated fields value (determined by the current language) we |
|
|
50 |
#cannot set the field directly. |
|
|
51 |
#To bypass the descriptor the assignment is done using the __dict__ |
|
|
52 |
#of the object. |
|
|
53 |
#""" |
|
|
54 |
#trans_opts = translator.get_options_for_model(self.model) |
|
|
55 |
#for field_name in trans_opts.fields: |
|
|
56 |
## Bypass the descriptor applied to the original field by setting |
|
|
57 |
## it's value via the __dict__ (which doesn't call the descriptor). |
|
|
58 |
#obj.__dict__[field_name] = form.cleaned_data[field_name] |
|
|
59 |
|
|
|
60 |
## Call the baseclass method |
|
|
61 |
#super(TranslationAdmin, self).save_model(request, obj, form, change) |
|
|
62 |
|
|
|
63 |
|
|
|
64 |
class TranslationTabularInline(admin.TabularInline): |
|
|
65 |
|
|
|
66 |
def patch_translation_field(self, db_field, field, **kwargs): |
|
|
67 |
trans_opts = translator.get_options_for_model(self.model) |
|
|
68 |
|
|
|
69 |
# Hide the original field by making it non-editable. |
|
|
70 |
if db_field.name in trans_opts.fields: |
|
|
71 |
db_field.editable = False |
|
|
72 |
|
|
|
73 |
# For every localized field copy the widget from the original field |
|
|
74 |
if db_field.name in trans_opts.localized_fieldnames_rev: |
|
|
75 |
orig_fieldname = trans_opts.localized_fieldnames_rev[db_field.name] |
|
|
76 |
orig_formfield = self.formfield_for_dbfield(self.model._meta.get_field(orig_fieldname), **kwargs) |
|
|
77 |
|
|
|
78 |
# In case the original form field was required, make the default |
|
|
79 |
# translation field required instead. |
|
|
80 |
if db_field.language == settings.LANGUAGES[0][0] and orig_formfield.required: |
|
|
81 |
orig_formfield.required = False |
|
|
82 |
field.required = True |
|
|
83 |
|
|
|
84 |
field.widget = deepcopy(orig_formfield.widget) |
|
|
85 |
|
|
|
86 |
def formfield_for_dbfield(self, db_field, **kwargs): |
|
|
87 |
# Call the baseclass function to get the formfield |
|
|
88 |
field = super(TranslationTabularInline, self).formfield_for_dbfield(db_field, **kwargs) |
|
|
89 |
self.patch_translation_field(db_field, field, **kwargs) |
|
|
90 |
return field |
|
|
91 |
|
|
|
92 |
|
|
|
93 |
class TranslationStackedInline(admin.StackedInline): |
|
|
94 |
|
|
|
95 |
def patch_translation_field(self, db_field, field, **kwargs): |
|
|
96 |
trans_opts = translator.get_options_for_model(self.model) |
|
|
97 |
|
|
|
98 |
# Hide the original field by making it non-editable. |
|
|
99 |
if db_field.name in trans_opts.fields: |
|
|
100 |
db_field.editable = False |
|
|
101 |
|
|
|
102 |
# For every localized field copy the widget from the original field |
|
|
103 |
if db_field.name in trans_opts.localized_fieldnames_rev: |
|
|
104 |
orig_fieldname = trans_opts.localized_fieldnames_rev[db_field.name] |
|
|
105 |
orig_formfield = self.formfield_for_dbfield(self.model._meta.get_field(orig_fieldname), **kwargs) |
|
|
106 |
|
|
|
107 |
# In case the original form field was required, make the default |
|
|
108 |
# translation field required instead. |
|
|
109 |
if db_field.language == settings.LANGUAGES[0][0] and orig_formfield.required: |
|
|
110 |
orig_formfield.required = False |
|
|
111 |
field.required = True |
|
|
112 |
|
|
|
113 |
field.widget = deepcopy(orig_formfield.widget) |
|
|
114 |
|
|
|
115 |
def formfield_for_dbfield(self, db_field, **kwargs): |
|
|
116 |
# Call the baseclass function to get the formfield |
|
|
117 |
field = super(TranslationStackedInline, self).formfield_for_dbfield(db_field, **kwargs) |
|
|
118 |
self.patch_translation_field(db_field, field, **kwargs) |
|
|
119 |
return field |
|
|
120 |
|