Polished AttributeForm to better handle changes to an attribute instance. Introduced...
[philo.git] / admin / widgets.py
1 from django import forms
2 from django.conf import settings
3 from django.utils.translation import ugettext as _
4 from django.utils.safestring import mark_safe
5 from django.utils.text import truncate_words
6 from django.utils.html import escape
7
8
9 class ModelLookupWidget(forms.TextInput):
10         # is_hidden = False
11         
12         def __init__(self, content_type, attrs=None):
13                 self.content_type = content_type
14                 super(ModelLookupWidget, self).__init__(attrs)
15         
16         def render(self, name, value, attrs=None):
17                 related_url = '../../../%s/%s/' % (self.content_type.app_label, self.content_type.model)
18                 if attrs is None:
19                         attrs = {}
20                 if not attrs.has_key('class'):
21                         attrs['class'] = 'vForeignKeyRawIdAdminField'
22                 output = super(ModelLookupWidget, self).render(name, value, attrs)
23                 output += '<a href="%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);">' % (related_url, name)
24                 output += '<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="%s" />' % (settings.ADMIN_MEDIA_PREFIX, _('Lookup'))
25                 output += '</a>'
26                 if value:
27                         value_class = self.content_type.model_class()
28                         try:
29                                 value_object = value_class.objects.get(pk=value)
30                                 output += '&nbsp;<strong>%s</strong>' % escape(truncate_words(value_object, 14))
31                         except value_class.DoesNotExist:
32                                 pass
33                 return mark_safe(output)