1
"""Widgets for Zinnia admin"""
2
from itertools import chain
4
from django import forms
5
from django.conf import settings
6
from django.contrib.admin import widgets
7
from django.utils.html import escape
8
from django.utils.html import conditional_escape
9
from django.utils.encoding import smart_unicode
10
from django.utils.encoding import force_unicode
12
from zinnia.settings import MEDIA_URL
15
class TreeNodeChoiceField(forms.ModelChoiceField):
16
"""Duplicating the TreeNodeChoiceField bundled in django-mptt
17
to avoid conflict with the TreeNodeChoiceField bundled in django-cms..."""
18
def __init__(self, level_indicator=u'---', *args, **kwargs):
19
self.level_indicator = level_indicator
20
if kwargs.get('required', True) and not 'empty_label' in kwargs:
21
kwargs['empty_label'] = None
22
super(TreeNodeChoiceField, self).__init__(*args, **kwargs)
24
def label_from_instance(self, obj):
25
"""Creates labels which represent the tree level of each node
26
when generating option labels."""
27
return u'%s %s' % (self.level_indicator * getattr(obj,
28
obj._meta.level_attr),
32
class MPTTModelChoiceIterator(forms.models.ModelChoiceIterator):
33
"""MPTT version of ModelChoiceIterator"""
34
def choice(self, obj):
35
"""Overriding choice method"""
36
tree_id = getattr(obj, getattr(self.queryset.model._meta,
37
'tree_id_atrr', 'tree_id'), 0)
38
left = getattr(obj, getattr(self.queryset.model._meta,
39
'left_atrr', 'lft'), 0)
40
return super(MPTTModelChoiceIterator,
41
self).choice(obj) + ((tree_id, left),)
44
class MPTTModelMultipleChoiceField(forms.ModelMultipleChoiceField):
45
"""MPTT version of ModelMultipleChoiceField"""
46
def label_from_instance(self, obj):
47
"""Overriding label_from_instance"""
48
level = getattr(obj, getattr(self.queryset.model._meta,
49
'level_attr', 'level'), 0)
50
return u'%s %s' % ('-' * level, smart_unicode(obj))
52
def _get_choices(self):
53
"""Overriding _get_choices"""
54
if hasattr(self, '_choices'):
56
return MPTTModelChoiceIterator(self)
58
choices = property(_get_choices, forms.ChoiceField._set_choices)
61
class MPTTFilteredSelectMultiple(widgets.FilteredSelectMultiple):
62
"""MPTT version of FilteredSelectMultiple"""
63
def __init__(self, verbose_name, is_stacked, attrs=None, choices=()):
64
super(MPTTFilteredSelectMultiple, self).__init__(
65
verbose_name, is_stacked, attrs, choices)
67
def render_options(self, choices, selected_choices):
69
This is copy'n'pasted from django.forms.widgets Select(Widget)
70
change to the for loop and render_option so they will unpack
71
and use our extra tuple of mptt sort fields (if you pass in
72
some default choices for this field, make sure they have the
75
def render_option(option_value, option_label, sort_fields):
76
"""Inner scope render_option"""
77
option_value = force_unicode(option_value)
78
selected_html = (option_value in selected_choices) \
79
and u' selected="selected"' or ''
80
return u'<option value="%s" data-tree-id="%s" ' \
81
'data-left-value="%s"%s>%s</option>' % (
86
conditional_escape(force_unicode(option_label)),
88
# Normalize to strings.
89
selected_choices = set([force_unicode(v) for v in selected_choices])
91
for option_value, option_label, sort_fields in chain(
92
self.choices, choices):
93
if isinstance(option_label, (list, tuple)):
94
output.append(u'<optgroup label="%s">' % escape(
95
force_unicode(option_value)))
96
for option in option_label:
97
output.append(render_option(*option))
98
output.append(u'</optgroup>')
100
output.append(render_option(option_value, option_label,
102
return u'\n'.join(output)
105
"""MPTTFilteredSelectMultiple's Media"""
106
js = (settings.ADMIN_MEDIA_PREFIX + 'js/core.js',
107
MEDIA_URL + 'js/mptt_m2m_selectbox.js',
108
settings.ADMIN_MEDIA_PREFIX + 'js/SelectFilter2.js',)