From 4d0a4b63ab6221a8a3b647d611c31f61215f28fb Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Thu, 12 Nov 2015 11:59:55 -0500 Subject: [PATCH] Added absolute_url for object to values returned in generic_lookup view. Refactored JavaScript links and text for object retrieved to display the model, unicode representation of the object, a link to edit the object and a link to the absolute url of the object. --- genericadmin/admin.py | 9 +++++++-- .../static/genericadmin/js/genericadmin.js | 20 ++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/genericadmin/admin.py b/genericadmin/admin.py index e198ceb..6b21e4c 100644 --- a/genericadmin/admin.py +++ b/genericadmin/admin.py @@ -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 = () @@ -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 @@ -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. """ diff --git a/genericadmin/static/genericadmin/js/genericadmin.js b/genericadmin/static/genericadmin/js/genericadmin.js index 457a086..5a7894e 100755 --- a/genericadmin/static/genericadmin/js/genericadmin.js +++ b/genericadmin/static/genericadmin/js/genericadmin.js @@ -112,7 +112,7 @@ link = ''; link = link + 'Lookup'; - link = link + ''; + link = link + ''; // insert link html after input element this.object_input.after(link); @@ -177,9 +177,20 @@ 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 @@ -187,7 +198,6 @@ that.updateObjectDataCallback(item); } } - $('#lookup_text_' + that.getFkId() + ' span').text(''); }, error: function(xhr, status, error) { $('#lookup_text_' + that.getFkId() + ' span').text('')