Newer
Older
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import copy
from django.conf import settings
from hvad.admin import TranslatableAdmin
from hvad.forms import translatable_modelform_factory
from django.utils.translation import ugettext_lazy as _
try:
from django.contrib.admin.utils import flatten_fieldsets
except ImportError:
# django < 1.7
from django.contrib.admin.util import flatten_fieldsets
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from django.utils.functional import curry
def edit_translation_links(obj):
output = ''
for lang in obj.get_available_languages():
output += (
'<a href="./%s/?language=%s" '
'target="_parent" title="Edit this %s in %s">%s'
'</a>'
) % (obj.pk, lang, obj._meta.verbose_name, lang.upper(), lang.upper(),)
return output
edit_translation_links.allow_tags = True
edit_translation_links.short_description = _('translations')
class NewsboxHVADBaseAdmin(TranslatableAdmin):
#XXX: hvad TranslatableAdmin doesn't support search_fields
# search_fields = ['newsbox_title', 'newsbox_summary', ]
# date_hierarchy = 'newsbox_publication_start_date'
def get_list_display(self, request):
list_display = super(NewsboxHVADBaseAdmin, self).get_list_display(request)
# prevent modifying class list_display variable
list_display = copy.deepcopy(list_display)
if settings.LANGUAGES and len(settings.LANGUAGES) > 1:
list_display.insert(1, edit_translation_links)
return list_display
def get_form(self, request, obj=None, **kwargs):
"""
This function is a workaround to an hvad issue. It doesn't use
get_fieldsets function.
Returns a Form class for use in the admin add view. This is used by
add_view and change_view.
"""
fieldsets = self.get_fieldsets(request, obj)
if fieldsets:
fields = flatten_fieldsets(fieldsets)
else:
fields = None
if self.exclude is None:
exclude = []
else:
exclude = list(self.exclude)
exclude.extend(kwargs.get("exclude", []))
exclude.extend(self.get_readonly_fields(request, obj))
# Exclude language_code, adding it again to the instance is done by
# the LanguageAwareCleanMixin (see translatable_modelform_factory)
exclude.append('language_code')
old_formfield_callback = curry(self.formfield_for_dbfield,
request=request)
defaults = {
"form": self.form,
"fields": fields,
"exclude": exclude,
"formfield_callback": old_formfield_callback,
}
defaults.update(kwargs)
language = self._language(request)
return translatable_modelform_factory(language, self.model, **defaults)
class NewsboxHVADAdmin(NewsboxHVADBaseAdmin):
pass