1 """Widgets for Zinnia admin"""
2 from itertools import chain
3
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
11
12 from zinnia.settings import MEDIA_URL
13
14
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)
23
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),
29 smart_unicode(obj))
30
31
33 """MPTT version of ModelChoiceIterator"""
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),)
42
43
45 """MPTT version of ModelMultipleChoiceField"""
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))
51
53 """Overriding _get_choices"""
54 if hasattr(self, '_choices'):
55 return self._choices
56 return MPTTModelChoiceIterator(self)
57
58 choices = property(_get_choices, forms.ChoiceField._set_choices)
59
60
62 """MPTT version of FilteredSelectMultiple"""
63 - def __init__(self, verbose_name, is_stacked, attrs=None, choices=()):
66
68 """
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
73 extra tuple too!)
74 """
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>' % (
82 escape(option_value),
83 sort_fields[0],
84 sort_fields[1],
85 selected_html,
86 conditional_escape(force_unicode(option_label)),
87 )
88
89 selected_choices = set([force_unicode(v) for v in selected_choices])
90 output = []
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>')
99 else:
100 output.append(render_option(option_value, option_label,
101 sort_fields))
102 return u'\n'.join(output)
103
109