forked from Brown-University-Library/ttwr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow easily adding new Genres (required using a template & js file f…
…rom new django versions)
- Loading branch information
Showing
8 changed files
with
194 additions
and
1 deletion.
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
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,91 @@ | ||
// Handles related-objects functionality: lookup link for raw_id_fields | ||
// and Add Another links. | ||
|
||
function html_unescape(text) { | ||
// Unescape a string that was escaped using django.utils.html.escape. | ||
text = text.replace(/</g, '<'); | ||
text = text.replace(/>/g, '>'); | ||
text = text.replace(/"/g, '"'); | ||
text = text.replace(/'/g, "'"); | ||
text = text.replace(/&/g, '&'); | ||
return text; | ||
} | ||
|
||
// IE doesn't accept periods or dashes in the window name, but the element IDs | ||
// we use to generate popup window names may contain them, therefore we map them | ||
// to allowed characters in a reversible way so that we can locate the correct | ||
// element when the popup window is dismissed. | ||
function id_to_windowname(text) { | ||
text = text.replace(/\./g, '__dot__'); | ||
text = text.replace(/\-/g, '__dash__'); | ||
return text; | ||
} | ||
|
||
function windowname_to_id(text) { | ||
text = text.replace(/__dot__/g, '.'); | ||
text = text.replace(/__dash__/g, '-'); | ||
return text; | ||
} | ||
|
||
function showAdminPopup(triggeringLink, name_regexp) { | ||
var name = triggeringLink.id.replace(name_regexp, ''); | ||
name = id_to_windowname(name); | ||
var href = triggeringLink.href; | ||
if (href.indexOf('?') == -1) { | ||
href += '?_popup=1'; | ||
} else { | ||
href += '&_popup=1'; | ||
} | ||
var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes'); | ||
win.focus(); | ||
return false; | ||
} | ||
|
||
function showRelatedObjectLookupPopup(triggeringLink) { | ||
return showAdminPopup(triggeringLink, /^lookup_/); | ||
} | ||
|
||
function dismissRelatedLookupPopup(win, chosenId) { | ||
var name = windowname_to_id(win.name); | ||
var elem = document.getElementById(name); | ||
if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) { | ||
elem.value += ',' + chosenId; | ||
} else { | ||
document.getElementById(name).value = chosenId; | ||
} | ||
win.close(); | ||
} | ||
|
||
function showAddAnotherPopup(triggeringLink) { | ||
return showAdminPopup(triggeringLink, /^add_/); | ||
} | ||
|
||
function dismissAddAnotherPopup(win, newId, newRepr) { | ||
// newId and newRepr are expected to have previously been escaped by | ||
// django.utils.html.escape. | ||
newId = html_unescape(newId); | ||
newRepr = html_unescape(newRepr); | ||
var name = windowname_to_id(win.name); | ||
var elem = document.getElementById(name); | ||
var o; | ||
if (elem) { | ||
var elemName = elem.nodeName.toUpperCase(); | ||
if (elemName == 'SELECT') { | ||
o = new Option(newRepr, newId); | ||
elem.options[elem.options.length] = o; | ||
o.selected = true; | ||
} else if (elemName == 'INPUT') { | ||
if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) { | ||
elem.value += ',' + newId; | ||
} else { | ||
elem.value = newId; | ||
} | ||
} | ||
} else { | ||
var toId = name + "_to"; | ||
o = new Option(newRepr, newId); | ||
SelectBox.add_to_cache(toId, o); | ||
SelectBox.redisplay(toId); | ||
} | ||
win.close(); | ||
} |
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,9 @@ | ||
<html> | ||
<head></head> | ||
<body> | ||
<form action="" method="post">{% csrf_token %} | ||
{{form.as_p}} | ||
<input type="submit" value="Submit" /> | ||
</form> | ||
</body> | ||
</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,9 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head><title></title></head> | ||
<body> | ||
<script type="text/javascript"> | ||
opener.dismissAddAnotherPopup(window, "{{ value }}", "{{ obj }}"); | ||
</script> | ||
</body> | ||
</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
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,57 @@ | ||
import copy | ||
from django import forms | ||
from django.core.urlresolvers import reverse | ||
from django.contrib.admin.templatetags.admin_static import static | ||
from django.utils.safestring import mark_safe | ||
from django.utils.translation import ugettext as _ | ||
|
||
|
||
class AddAnotherWidgetWrapper(forms.Widget): | ||
""" | ||
This class is a wrapper to a given widget to add the add icon for the | ||
admin interface. Modeled after | ||
django.contrib.admin.widgets.RelatedFieldWidgetWrapper | ||
""" | ||
def __init__(self, widget, model): | ||
self.is_hidden = widget.is_hidden | ||
self.needs_multipart_form = widget.needs_multipart_form | ||
self.attrs = widget.attrs | ||
self.choices = widget.choices | ||
self.widget = widget | ||
self.model = model | ||
|
||
def __deepcopy__(self, memo): | ||
obj = copy.copy(self) | ||
obj.widget = copy.deepcopy(self.widget, memo) | ||
obj.attrs = self.widget.attrs | ||
memo[id(self)] = obj | ||
return obj | ||
|
||
@property | ||
def media(self): | ||
return self.widget.media | ||
|
||
def render(self, name, value, *args, **kwargs): | ||
model = self.model | ||
info = (model._meta.app_label, model._meta.object_name.lower()) | ||
self.widget.choices = self.choices | ||
output = [self.widget.render(name, value, *args, **kwargs)] | ||
related_url = reverse('new_genre') | ||
output.append((' <a href="%s" class="add-another" id="add_id_%s" ' + 'onclick="return showAddAnotherPopup(this);">') % (related_url, name)) | ||
output.append('<img src="%s" width="10" height="10" alt="%s"/></a>' % (static('admin/img/icon_addlink.gif'), _('Add Another'))) | ||
return mark_safe(''.join(output)) | ||
|
||
def build_attrs(self, extra_attrs=None, **kwargs): | ||
"Helper function for building an attribute dictionary." | ||
self.attrs = self.widget.build_attrs(extra_attrs=None, **kwargs) | ||
return self.attrs | ||
|
||
def value_from_datadict(self, data, files, name): | ||
return self.widget.value_from_datadict(data, files, name) | ||
|
||
def _has_changed(self, initial, data): | ||
return self.widget._has_changed(initial, data) | ||
|
||
def id_for_label(self, id_): | ||
return self.widget.id_for_label(id_) | ||
|