Skip to content
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
9 changes: 7 additions & 2 deletions genericadmin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
from django.contrib.admin.views.main import IS_POPUP_VAR
except ImportError:
from django.contrib.admin.options import IS_POPUP_VAR
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ObjectDoesNotExist

JS_PATH = getattr(settings, 'GENERICADMIN_JS', 'genericadmin/js/')


class BaseGenericModelAdmin(object):
class Media:
js = ()
Expand Down Expand Up @@ -138,6 +139,11 @@ def generic_lookup(self, request):
try:
obj = content_type.get_object_for_this_type(pk=object_id)
obj_dict["object_text"] = capfirst(force_text(obj))
try:
absolute_url = obj.get_absolute_url()
except (AttributeError,):
absolute_url = ''
obj_dict["absolute_url"] = absolute_url
except ObjectDoesNotExist:
raise Http404

Expand All @@ -147,7 +153,6 @@ def generic_lookup(self, request):
return HttpResponse(resp, content_type='application/json')



class GenericAdminModelAdmin(BaseGenericModelAdmin, admin.ModelAdmin):
"""Model admin for generic relations. """

Expand Down
20 changes: 15 additions & 5 deletions genericadmin/static/genericadmin/js/genericadmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
link = '<a class="related-lookup" id="' + id + '" href="' + url + '">';

link = link + '<img src="' + this.admin_media_url.replace(/\/?$/, '/') + 'img/selector-search.gif" style="cursor: pointer; margin-left: 5px; margin-right: 10px;" width="16" height="16" alt="Lookup"></a>';
link = link + '<strong id="lookup_text_'+ this.getFkId() +'" margin-left: 5px"><a target="_new" href="#"></a><span></span></strong>';
link = link + '<strong id="lookup_text_'+ this.getFkId() +'" margin-left: 5px"><span></span><a target="_new" class="edit-link" href="#" style="display:inline-block;margin-left:10px;"></a><a href="#" target="_new" class="view-link" style="display:inline-block;margin-left:10px;"></a></strong>';

// insert link html after input element
this.object_input.after(link);
Expand Down Expand Up @@ -177,17 +177,27 @@
success: function(item) {
if (item && item.content_type_text && item.object_text) {
var url = that.getLookupUrl(that.cID, false);
$('#lookup_text_' + that.getFkId() + ' a')
.text(item.content_type_text + ': ' + item.object_text)
.attr('href', url + item.object_id);

// object description
$('#lookup_text_' + that.getFkId() + ' span')
.text(item.content_type_text + ': ' + item.object_text);

// Link to edit object
$('#lookup_text_' + that.getFkId() + ' .edit-link')
.text('Edit').attr('href', url + item.object_id);

// Link to view object
if (item.absolute_url) {
$('#lookup_text_' + that.getFkId() + ' .view-link')
.text('View').attr('href', item.absolute_url);
}

// run a callback to do other stuff like prepopulating url fields
// can't be done with normal django admin prepopulate
if (that.updateObjectDataCallback) {
that.updateObjectDataCallback(item);
}
}
$('#lookup_text_' + that.getFkId() + ' span').text('');
},
error: function(xhr, status, error) {
$('#lookup_text_' + that.getFkId() + ' span').text('')
Expand Down