X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/2f7a76a00f2bcae2f67459b7fd86806278ef9b66..b8334054d908e8c49bdbd8ac28ab2c9e7b37a937:/admin/widgets.py?ds=sidebyside
diff --git a/admin/widgets.py b/admin/widgets.py
index a1f5bae..fb13ac7 100644
--- a/admin/widgets.py
+++ b/admin/widgets.py
@@ -1,29 +1,68 @@
from django import forms
from django.conf import settings
+from django.contrib.admin.widgets import FilteredSelectMultiple, url_params_from_lookup_dict
+from django.utils.translation import ugettext as _
+from django.utils.safestring import mark_safe
+from django.utils.text import truncate_words
+from django.utils.html import escape
class ModelLookupWidget(forms.TextInput):
# is_hidden = False
- def __init__(self, content_type, attrs=None):
+ def __init__(self, content_type, attrs=None, limit_choices_to=None):
self.content_type = content_type
+ self.limit_choices_to = limit_choices_to
super(ModelLookupWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
related_url = '../../../%s/%s/' % (self.content_type.app_label, self.content_type.model)
+ params = url_params_from_lookup_dict(self.limit_choices_to)
+ if params:
+ url = u'?' + u'&'.join([u'%s=%s' % (k, v) for k, v in params.items()])
+ else:
+ url = u''
if attrs is None:
attrs = {}
- if not attrs.has_key('class'):
+ if "class" not in attrs:
attrs['class'] = 'vForeignKeyRawIdAdminField'
- output = super(ModelLookupWidget, self).render(name, value, attrs)
- output += '' % (related_url, name)
- output += '' % (settings.ADMIN_MEDIA_PREFIX, _('Lookup'))
- output += ''
+ output = [super(ModelLookupWidget, self).render(name, value, attrs)]
+ output.append('' % (related_url, url, name))
+ output.append('' % (settings.ADMIN_MEDIA_PREFIX, _('Lookup')))
+ output.append('')
if value:
value_class = self.content_type.model_class()
try:
value_object = value_class.objects.get(pk=value)
- output += ' %s' % escape(truncate_words(value_object, 14))
+ output.append(' %s' % escape(truncate_words(value_object, 14)))
except value_class.DoesNotExist:
pass
- return mark_safe(output)
\ No newline at end of file
+ return mark_safe(u''.join(output))
+
+
+class TagFilteredSelectMultiple(FilteredSelectMultiple):
+ """
+ A SelectMultiple with a JavaScript filter interface.
+
+ Note that the resulting JavaScript assumes that the jsi18n
+ catalog has been loaded in the page
+ """
+ class Media:
+ js = (
+ settings.ADMIN_MEDIA_PREFIX + "js/core.js",
+ settings.ADMIN_MEDIA_PREFIX + "js/SelectBox.js",
+ settings.ADMIN_MEDIA_PREFIX + "js/SelectFilter2.js",
+ settings.ADMIN_MEDIA_PREFIX + "js/TagCreation.js",
+ )
+
+ def render(self, name, value, attrs=None, choices=()):
+ if attrs is None: attrs = {}
+ attrs['class'] = 'selectfilter'
+ if self.is_stacked: attrs['class'] += 'stacked'
+ output = [super(FilteredSelectMultiple, self).render(name, value, attrs, choices)]
+ output.append(u'\n' % \
+ (name, self.verbose_name.replace('"', '\\"'), int(self.is_stacked), settings.ADMIN_MEDIA_PREFIX, name))
+ return mark_safe(u''.join(output))
\ No newline at end of file