diff --git a/armstrong/hatband/static/hatband/js/rawgenerickey.js b/armstrong/hatband/static/hatband/js/rawgenerickey.js new file mode 100644 index 0000000..e230b54 --- /dev/null +++ b/armstrong/hatband/static/hatband/js/rawgenerickey.js @@ -0,0 +1,12 @@ +var jQuery = jQuery || django.jQuery; +var armstrong = armstrong || {}; +armstrong.widgets = armstrong.widgets || {}; + +armstrong.widgets.raw_generic_key = function(type_input, id_input, picker_anchor) { + var $ = jQuery; + type_input.change(function(){ + var ct = armstrong.widgets.raw_generic_key.content_types[type_input.val()]; + picker_anchor.attr('href', '/admin/' + ct.app_label + '/' + ct.model); + }); + type_input.change(); +}; \ No newline at end of file diff --git a/armstrong/hatband/templates/admin/hatband/widgets/rawgenerickey.html b/armstrong/hatband/templates/admin/hatband/widgets/rawgenerickey.html new file mode 100644 index 0000000..256778b --- /dev/null +++ b/armstrong/hatband/templates/admin/hatband/widgets/rawgenerickey.html @@ -0,0 +1,20 @@ + + + + + + diff --git a/armstrong/hatband/widgets/__init__.py b/armstrong/hatband/widgets/__init__.py index 2f23bd1..1ef5a09 100644 --- a/armstrong/hatband/widgets/__init__.py +++ b/armstrong/hatband/widgets/__init__.py @@ -5,3 +5,4 @@ from .base import RichTextWidget from .ckeditor import CKEditorWidget from .visualsearch import GenericKeyWidget +from .rawgenerickey import RawGenericKeyWidget \ No newline at end of file diff --git a/armstrong/hatband/widgets/rawgenerickey.py b/armstrong/hatband/widgets/rawgenerickey.py new file mode 100644 index 0000000..74cc75e --- /dev/null +++ b/armstrong/hatband/widgets/rawgenerickey.py @@ -0,0 +1,47 @@ +from django.core.urlresolvers import reverse +from django.forms import Widget +from django.template.loader import render_to_string +from django.conf import settings +from django.contrib.contenttypes.models import ContentType + +from ..utils import static_url + + +class RawGenericKeyWidget(Widget): + template = "admin/hatband/widgets/rawgenerickey.html" + + class Media: + js = ( + static_url("hatband/js/rawgenerickey.js"), + ) + + def __init__(self, object_id_name="object_id", + content_type_name="content_type", + facet_url=None, + query_lookup_url=None, + base_lookup_url=None, + *args, **kwargs): + super(RawGenericKeyWidget, self).__init__(*args, **kwargs) + self.object_id_name = object_id_name + self.content_type_name = content_type_name + self.facet_url = facet_url + self.base_lookup_url = base_lookup_url + + def render(self, name, value, attrs=None): + if value is None: + value = '' + final_attrs = self.build_attrs(attrs, name=name) + final_attrs.update({ + "value": value, + "is_templated": final_attrs["id"].find("__prefix__") > -1, + "object_id_name": self.object_id_name, + "content_type_name": self.content_type_name, + "content_type_id": final_attrs["id"].replace(self.object_id_name, self.content_type_name), + "facet_url": self.facet_url or + reverse("admin:generic_key_facets"), + "admin_media_prefix": settings.ADMIN_MEDIA_PREFIX, + "content_types": ContentType.objects.all(), + "base_lookup_url": (self.base_lookup_url or + reverse("admin:index")) + }) + return render_to_string(self.template, final_attrs)