Reverted TemplateField parent to models.TextField and moved EmbedWidget into philo...
[philo.git] / philo / admin / widgets.py
1 from django import forms
2 from django.conf import settings
3 from django.contrib.admin.widgets import FilteredSelectMultiple, url_params_from_lookup_dict
4 from django.utils import simplejson as json
5 from django.utils.html import escape
6 from django.utils.safestring import mark_safe
7 from django.utils.text import truncate_words
8 from django.utils.translation import ugettext as _
9
10
11 class ModelLookupWidget(forms.TextInput):
12         # is_hidden = False
13         
14         def __init__(self, content_type, attrs=None, limit_choices_to=None):
15                 self.content_type = content_type
16                 self.limit_choices_to = limit_choices_to
17                 super(ModelLookupWidget, self).__init__(attrs)
18         
19         def render(self, name, value, attrs=None):
20                 related_url = '../../../%s/%s/' % (self.content_type.app_label, self.content_type.model)
21                 params = url_params_from_lookup_dict(self.limit_choices_to)
22                 if params:
23                         url = u'?' + u'&'.join([u'%s=%s' % (k, v) for k, v in params.items()])
24                 else:
25                         url = u''
26                 if attrs is None:
27                         attrs = {}
28                 if "class" not in attrs:
29                         attrs['class'] = 'vForeignKeyRawIdAdminField'
30                 output = [super(ModelLookupWidget, self).render(name, value, attrs)]
31                 output.append('<a href="%s%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);">' % (related_url, url, name))
32                 output.append('<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="%s" />' % (settings.ADMIN_MEDIA_PREFIX, _('Lookup')))
33                 output.append('</a>')
34                 if value:
35                         value_class = self.content_type.model_class()
36                         try:
37                                 value_object = value_class.objects.get(pk=value)
38                                 output.append('&nbsp;<strong>%s</strong>' % escape(truncate_words(value_object, 14)))
39                         except value_class.DoesNotExist:
40                                 pass
41                 return mark_safe(u''.join(output))
42
43
44 class TagFilteredSelectMultiple(FilteredSelectMultiple):
45         """
46         A SelectMultiple with a JavaScript filter interface.
47
48         Note that the resulting JavaScript assumes that the jsi18n
49         catalog has been loaded in the page
50         """
51         class Media:
52                 js = (
53                         settings.ADMIN_MEDIA_PREFIX + "js/core.js",
54                         settings.ADMIN_MEDIA_PREFIX + "js/SelectBox.js",
55                         settings.ADMIN_MEDIA_PREFIX + "js/SelectFilter2.js",
56                         "philo/js/TagCreation.js",
57                 )
58
59         def render(self, name, value, attrs=None, choices=()):
60                 if attrs is None: attrs = {}
61                 attrs['class'] = 'selectfilter'
62                 if self.is_stacked: attrs['class'] += 'stacked'
63                 output = [super(FilteredSelectMultiple, self).render(name, value, attrs, choices)]
64                 output.append(u'<script type="text/javascript">addEvent(window, "load", function(e) {')
65                 # TODO: "id_" is hard-coded here. This should instead use the correct
66                 # API to determine the ID dynamically.
67                 output.append(u'SelectFilter.init("id_%s", "%s", %s, "%s"); tagCreation.init("id_%s"); });</script>\n' % \
68                         (name, self.verbose_name.replace('"', '\\"'), int(self.is_stacked), settings.ADMIN_MEDIA_PREFIX, name))
69                 return mark_safe(u''.join(output))
70
71
72 class EmbedWidget(forms.Textarea):
73         """A form widget with the HTML class embedding and an embedded list of content-types."""
74         def __init__(self, attrs=None):
75                 from philo.models import value_content_type_limiter
76                 
77                 content_types = value_content_type_limiter.classes
78                 data = []
79                 
80                 for content_type in content_types:
81                         data.append({'app_label': content_type._meta.app_label, 'object_name': content_type._meta.object_name.lower(), 'verbose_name': unicode(content_type._meta.verbose_name)})
82                 
83                 json_ = json.dumps(data)
84                 
85                 default_attrs = {'class': 'embedding vLargeTextField', 'data-content-types': json_ }
86                 
87                 if attrs:
88                         default_attrs.update(attrs)
89                         
90                 super(EmbedWidget, self).__init__(default_attrs)
91                 
92         class Media:
93                 css = {
94                         'all': ('philo/css/EmbedWidget.css',),
95                 }
96                 js = ('philo/js/EmbedWidget.js',)