Skip to content
This repository has been archived by the owner on Jul 18, 2018. It is now read-only.

Add RawGenericKey widget #14

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions armstrong/hatband/static/hatband/js/rawgenerickey.js
Original file line number Diff line number Diff line change
@@ -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();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<input type="text" id="{{ id }}" name="{{ name }}" value="{{ value }}" class="vForeignKeyRawIdAdminField" />
<a href="javascript:alert('Select a type first')" class="related-lookup" id="lookup_{{ id }}" onclick="return showRelatedObjectLookupPopup(this);">
<img src="{{ admin_media_prefix }}img/admin/selector-search.gif" width="16" height="16"/>
</a>

<script>
(function ($){
$(window).load(function(){
armstrong.widgets.raw_generic_key.content_types = {
{% for ct in content_types %}
{{ ct.id }}: {app_label: '{{ ct.app_label }}', model: '{{ ct.model }}'}{% if not forloop.last %},{% endif %}
{% endfor %}
};
{% if not is_templated %}
armstrong.widgets.raw_generic_key($('#{{ content_type_id }}'), $('#{{ id }}'),
$('#lookup_{{ id }}'));
{% endif %}
})
})(django.jQuery);
</script>
1 change: 1 addition & 0 deletions armstrong/hatband/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from .base import RichTextWidget
from .ckeditor import CKEditorWidget
from .visualsearch import GenericKeyWidget
from .rawgenerickey import RawGenericKeyWidget
47 changes: 47 additions & 0 deletions armstrong/hatband/widgets/rawgenerickey.py
Original file line number Diff line number Diff line change
@@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should respect ARMSTRONG_ADMIN_PROVIDE_STATIC (see 905d2ae)

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)