From c7f485315c12c87dc3450b31d3fcbf45bae3c5df Mon Sep 17 00:00:00 2001 From: Dave McLain Date: Tue, 6 Mar 2012 18:29:44 -0600 Subject: [PATCH 1/5] add RawGenericKey widget --- .../static/hatband/js/rawgenerickey.js | 11 +++++ .../admin/edit_inline/rawgenerickey.html | 0 .../admin/hatband/widgets/rawgenerickey.html | 20 ++++++++ armstrong/hatband/widgets/__init__.py | 1 + armstrong/hatband/widgets/rawgenerickey.py | 47 +++++++++++++++++++ 5 files changed, 79 insertions(+) create mode 100644 armstrong/hatband/static/hatband/js/rawgenerickey.js create mode 100644 armstrong/hatband/templates/admin/edit_inline/rawgenerickey.html create mode 100644 armstrong/hatband/templates/admin/hatband/widgets/rawgenerickey.html create mode 100644 armstrong/hatband/widgets/rawgenerickey.py diff --git a/armstrong/hatband/static/hatband/js/rawgenerickey.js b/armstrong/hatband/static/hatband/js/rawgenerickey.js new file mode 100644 index 0000000..57edc9c --- /dev/null +++ b/armstrong/hatband/static/hatband/js/rawgenerickey.js @@ -0,0 +1,11 @@ +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); + }) +}; \ No newline at end of file diff --git a/armstrong/hatband/templates/admin/edit_inline/rawgenerickey.html b/armstrong/hatband/templates/admin/edit_inline/rawgenerickey.html new file mode 100644 index 0000000..e69de29 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..474c28b --- /dev/null +++ b/armstrong/hatband/templates/admin/hatband/widgets/rawgenerickey.html @@ -0,0 +1,20 @@ + + + + + +{% if not is_templated %} + +{% endif %} \ No newline at end of file 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) From 5aff23c7bcec2b3417c555e9cb97bb487c6aa4f4 Mon Sep 17 00:00:00 2001 From: Dave McLain Date: Wed, 23 May 2012 11:39:07 -0500 Subject: [PATCH 2/5] Remove unneeded file --- armstrong/hatband/templates/admin/edit_inline/rawgenerickey.html | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 armstrong/hatband/templates/admin/edit_inline/rawgenerickey.html diff --git a/armstrong/hatband/templates/admin/edit_inline/rawgenerickey.html b/armstrong/hatband/templates/admin/edit_inline/rawgenerickey.html deleted file mode 100644 index e69de29..0000000 From 0e2f9a1db324e6e6ac283528739cfed315bf11b1 Mon Sep 17 00:00:00 2001 From: Dave McLain Date: Wed, 23 May 2012 12:49:30 -0500 Subject: [PATCH 3/5] make sure that the content types are loaded when there are not visible widgets --- .../admin/hatband/widgets/rawgenerickey.html | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/armstrong/hatband/templates/admin/hatband/widgets/rawgenerickey.html b/armstrong/hatband/templates/admin/hatband/widgets/rawgenerickey.html index 474c28b..86b82f2 100644 --- a/armstrong/hatband/templates/admin/hatband/widgets/rawgenerickey.html +++ b/armstrong/hatband/templates/admin/hatband/widgets/rawgenerickey.html @@ -5,6 +5,15 @@ {% if not is_templated %} +{% else %} + From 4ce9b3f913d8721b82518a430b6f577cd18957bd Mon Sep 17 00:00:00 2001 From: Dave McLain Date: Wed, 23 May 2012 12:51:30 -0500 Subject: [PATCH 4/5] semicolons for everyone --- armstrong/hatband/static/hatband/js/rawgenerickey.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/armstrong/hatband/static/hatband/js/rawgenerickey.js b/armstrong/hatband/static/hatband/js/rawgenerickey.js index 57edc9c..748b430 100644 --- a/armstrong/hatband/static/hatband/js/rawgenerickey.js +++ b/armstrong/hatband/static/hatband/js/rawgenerickey.js @@ -7,5 +7,5 @@ armstrong.widgets.raw_generic_key = function(type_input, id_input, picker_anchor 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); - }) + }); }; \ No newline at end of file From e99b2e4e164a87ff426b602c4fff4e6a982b039c Mon Sep 17 00:00:00 2001 From: Dave McLain Date: Wed, 23 May 2012 15:20:44 -0500 Subject: [PATCH 5/5] move some javascript around, so if a user choose sa type, but not an id, on the next page load the object picker will be initialized properly --- .../hatband/static/hatband/js/rawgenerickey.js | 1 + .../admin/hatband/widgets/rawgenerickey.html | 15 ++++----------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/armstrong/hatband/static/hatband/js/rawgenerickey.js b/armstrong/hatband/static/hatband/js/rawgenerickey.js index 748b430..e230b54 100644 --- a/armstrong/hatband/static/hatband/js/rawgenerickey.js +++ b/armstrong/hatband/static/hatband/js/rawgenerickey.js @@ -8,4 +8,5 @@ armstrong.widgets.raw_generic_key = function(type_input, id_input, picker_anchor 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 index 86b82f2..256778b 100644 --- a/armstrong/hatband/templates/admin/hatband/widgets/rawgenerickey.html +++ b/armstrong/hatband/templates/admin/hatband/widgets/rawgenerickey.html @@ -3,16 +3,6 @@ -{% if not is_templated %} - -{% else %} -{% endif %} \ No newline at end of file