django-authority/authority/widgets.py
Jannis Leidel 71eee85b85 Moved authority app from src/ to root directory
--HG--
rename : src/authority/__init__.py => authority/__init__.py
rename : src/authority/admin.py => authority/admin.py
rename : src/authority/decorators.py => authority/decorators.py
rename : src/authority/exceptions.py => authority/exceptions.py
rename : src/authority/fixtures/tests.json => authority/fixtures/tests.json
rename : src/authority/forms.py => authority/forms.py
rename : src/authority/managers.py => authority/managers.py
rename : src/authority/models.py => authority/models.py
rename : src/authority/permissions.py => authority/permissions.py
rename : src/authority/sites.py => authority/sites.py
rename : src/authority/templates/admin/edit_inline/action_tabular.html => authority/templates/admin/edit_inline/action_tabular.html
rename : src/authority/templates/admin/permission_change_form.html => authority/templates/admin/permission_change_form.html
rename : src/authority/templates/authority/403.html => authority/templates/authority/403.html
rename : src/authority/templates/authority/permission_delete_link.html => authority/templates/authority/permission_delete_link.html
rename : src/authority/templates/authority/permission_form.html => authority/templates/authority/permission_form.html
rename : src/authority/templates/authority/permission_request_approve_link.html => authority/templates/authority/permission_request_approve_link.html
rename : src/authority/templates/authority/permission_request_delete_link.html => authority/templates/authority/permission_request_delete_link.html
rename : src/authority/templatetags/__init__.py => authority/templatetags/__init__.py
rename : src/authority/templatetags/permissions.py => authority/templatetags/permissions.py
rename : src/authority/tests.py => authority/tests.py
rename : src/authority/urls.py => authority/urls.py
rename : src/authority/views.py => authority/views.py
rename : src/authority/widgets.py => authority/widgets.py
2010-01-07 18:00:41 +01:00

53 lines
2.3 KiB
Python

from django import forms
from django.conf import settings
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.contrib.admin.widgets import ForeignKeyRawIdWidget
generic_script = """
<script type="text/javascript">
function showGenericRelatedObjectLookupPopup(ct_select, triggering_link, url_base) {
var url = content_types[ct_select.options[ct_select.selectedIndex].value];
if (url != undefined) {
triggering_link.href = url_base + url;
return showRelatedObjectLookupPopup(triggering_link);
}
return false;
}
</script>
"""
class GenericForeignKeyRawIdWidget(ForeignKeyRawIdWidget):
def __init__(self, ct_field, cts=[], attrs=None):
self.ct_field = ct_field
self.cts = cts
forms.TextInput.__init__(self, attrs)
def render(self, name, value, attrs=None):
if attrs is None:
attrs = {}
related_url = '../../../'
params = self.url_parameters()
if params:
url = '?' + '&amp;'.join(['%s=%s' % (k, v) for k, v in params.iteritems()])
else:
url = ''
if 'class' not in attrs:
attrs['class'] = 'vForeignKeyRawIdAdminField'
output = [forms.TextInput.render(self, name, value, attrs)]
output.append("""%(generic_script)s
<a href="%(related)s%(url)s" class="related-lookup" id="lookup_id_%(name)s" onclick="return showGenericRelatedObjectLookupPopup(document.getElementById('id_%(ct_field)s'), this, '%(related)s%(url)s');"> """
% {'generic_script': generic_script, 'related': related_url, 'url': url, 'name': name, 'ct_field': self.ct_field})
output.append('<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="%s" /></a>' % (settings.ADMIN_MEDIA_PREFIX, _('Lookup')))
from django.contrib.contenttypes.models import ContentType
content_types = """
<script type="text/javascript">
var content_types = new Array();
%s
</script>
""" % ('\n'.join(["content_types[%s] = '%s/%s/';" % (ContentType.objects.get_for_model(ct).id, ct._meta.app_label, ct._meta.object_name.lower()) for ct in self.cts]))
return mark_safe(u''.join(output) + content_types)
def url_parameters(self):
return {}