This repository has been archived by the owner on Jul 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Add RawGenericKey widget #14
Open
dmclain
wants to merge
5
commits into
armstrong:master
Choose a base branch
from
dmclain:rawgenerickey
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c7f4853
add RawGenericKey widget
5aff23c
Remove unneeded file
0e2f9a1
make sure that the content types are loaded when there are not visibl…
4ce9b3f
semicolons for everyone
e99b2e4
move some javascript around, so if a user choose sa type, but not an …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
20 changes: 20 additions & 0 deletions
20
armstrong/hatband/templates/admin/hatband/widgets/rawgenerickey.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)