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 _
11 class ModelLookupWidget(forms.TextInput):
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)
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)
23 url = u'?' + u'&'.join([u'%s=%s' % (k, v) for k, v in params.items()])
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')))
35 value_class = self.content_type.model_class()
37 value_object = value_class.objects.get(pk=value)
38 output.append(' <strong>%s</strong>' % escape(truncate_words(value_object, 14)))
39 except value_class.DoesNotExist:
41 return mark_safe(u''.join(output))
44 class TagFilteredSelectMultiple(FilteredSelectMultiple):
46 A SelectMultiple with a JavaScript filter interface.
48 Note that the resulting JavaScript assumes that the jsi18n
49 catalog has been loaded in the page
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",
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))
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
77 content_types = value_content_type_limiter.classes
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)})
83 json_ = json.dumps(data)
85 default_attrs = {'class': 'embedding vLargeTextField', 'data-content-types': json_ }
88 default_attrs.update(attrs)
90 super(EmbedWidget, self).__init__(default_attrs)
94 'all': ('philo/css/EmbedWidget.css',),
96 js = ('philo/js/EmbedWidget.js',)