diff --git a/genericadmin/__init__.py b/genericadmin/__init__.py old mode 100644 new mode 100755 diff --git a/genericadmin/admin.py b/genericadmin/admin.py old mode 100644 new mode 100755 index 3daf035..40aff4c --- a/genericadmin/admin.py +++ b/genericadmin/admin.py @@ -1,7 +1,6 @@ import json from functools import update_wrapper -from django.core.urlresolvers import reverse, NoReverseMatch from django.contrib import admin from django.conf.urls import url from django.conf import settings @@ -75,14 +74,7 @@ def get_generic_field_list(self, request, prefix=''): }) if hasattr(self, 'inlines') and len(self.inlines) > 0: - try: - # Django < 1.9 - formsets = zip(self.get_formsets(request), self.get_inline_instances(request)) - except (AttributeError, ): - # Django >= 1.9 - formsets = self.get_formsets_with_inlines(request) - - for FormSet, inline in formsets: + for FormSet, inline in zip(self.get_formsets_with_inlines(request), self.get_inline_instances(request)): if hasattr(inline, 'get_generic_field_list'): prefix = FormSet.get_default_prefix() field_list = field_list + inline.get_generic_field_list(request, prefix) @@ -96,33 +88,23 @@ def wrapper(*args, **kwargs): return update_wrapper(wrapper, view) custom_urls = [ - url(r'^(.*)genericadmin-obj-data/', wrap( - self.generic_lookup), name='admin_genericadmin_obj_lookup'), - url(r'^(.*)genericadmin-init/', wrap( - self.genericadmin_js_init), name='admin_genericadmin_init'), + url(r'^obj-data/$', wrap(self.generic_lookup), name='admin_genericadmin_obj_lookup'), + url(r'^genericadmin-init/$', wrap(self.genericadmin_js_init), name='admin_genericadmin_init'), ] return custom_urls + super(BaseGenericModelAdmin, self).get_urls() - def genericadmin_js_init(self, request, *args, **kwargs): + def genericadmin_js_init(self, request): if request.method == 'GET': obj_dict = {} for c in ContentType.objects.all(): val = force_text('%s/%s' % (c.app_label, c.model)) params = self.content_type_lookups.get('%s.%s' % (c.app_label, c.model), {}) params = url_params_from_lookup_dict(params) - - try: - # Reverse the admin changelist url - url = reverse('admin:%s_%s_changelist' % ( - c.app_label, c.model)) - except (NoReverseMatch, ): - continue - if self.content_type_whitelist: if val in self.content_type_whitelist: - obj_dict[c.id] = (val, url, params) + obj_dict[c.id] = (val, params) elif val not in self.content_type_blacklist: - obj_dict[c.id] = (val, url, params) + obj_dict[c.id] = (val, params) data = { 'url_array': obj_dict, @@ -133,7 +115,7 @@ def genericadmin_js_init(self, request, *args, **kwargs): return HttpResponse(resp, content_type='application/json') return HttpResponseNotAllowed(['GET']) - def generic_lookup(self, request, *args, **kwargs): + def generic_lookup(self, request): if request.method != 'GET': return HttpResponseNotAllowed(['GET']) diff --git a/genericadmin/static/genericadmin/js/genericadmin.js b/genericadmin/static/genericadmin/js/genericadmin.js index c26744e..529dbb9 100755 --- a/genericadmin/static/genericadmin/js/genericadmin.js +++ b/genericadmin/static/genericadmin/js/genericadmin.js @@ -12,10 +12,10 @@ var GenericAdmin = { url_array: null, fields: null, - obj_url: "../genericadmin-obj-data/", + obj_url: "../obj-data/", admin_media_url: window.__admin_media_prefix__, popup: '_popup', - + prepareSelect: function(select) { var that = this, opt_keys = [], @@ -30,7 +30,7 @@ if (this.value) { if (that.url_array[this.value]) { key = that.url_array[this.value][0].split('/')[0]; - + opt = $(this).clone(); opt.text(that.capFirst(opt.text())); if ($.inArray(key, opt_keys) < 0) { @@ -47,7 +47,7 @@ } }); select.empty().append(no_value); - + opt_keys = opt_keys.sort(); $.each(opt_keys, function(index, key) { @@ -61,9 +61,8 @@ return select; }, - getLookupUrlParams: function(cID) { - var q = this.url_array[cID][2] || {}, + var q = this.url_array[cID][1] || {}, str = []; for(var p in q) { str.push(encodeURIComponent(p) + "=" + encodeURIComponent(q[p])); @@ -72,13 +71,11 @@ url = x ? ("?" + x) : ""; return url; }, - - getLookupUrl: function(cID, with_params) { - var url = this.url_array[cID][1]; - if(with_params){url = url + this.getLookupUrlParams(cID);} - return url; + + getLookupUrl: function(cID) { + return '../../../' + this.url_array[cID][0] + '/' + this.getLookupUrlParams(cID); }, - + getFkId: function() { if (this.fields.inline === false) { return 'id_' + this.fields.fk_field; @@ -86,7 +83,7 @@ return ['id_', this.fields.prefix, '-', this.fields.number, '-', this.fields.fk_field].join(''); } }, - + getCtId: function() { if (this.fields.inline === false) { return 'id_' + this.fields.ct_field; @@ -94,24 +91,24 @@ return ['id_', this.fields.prefix, '-', this.fields.number, '-', this.fields.ct_field].join(''); } }, - + capFirst: function(string) { return string.charAt(0).toUpperCase() + string.slice(1); }, - + hideLookupLink: function() { var this_id = this.getFkId(); $('#lookup_' + this_id).unbind().remove(); $('#lookup_text_' + this_id + ' a').remove(); $('#lookup_text_' + this_id + ' span').remove(); }, - + showLookupLink: function() { var that = this, - url = this.getLookupUrl(this.cID, true), + url = this.getLookupUrl(this.cID), id = 'lookup_' + this.getFkId(), link = ' '; - + link = link + ''; // insert link html after input element @@ -119,7 +116,7 @@ return id; }, - + pollInputChange: function(window) { var that = this, interval_id = setInterval(function() { @@ -131,11 +128,11 @@ }, 150); }, - + popRelatedObjectLookup: function(link) { var name = id_to_windowname(this.getFkId()), url_parts = [], - href, + href, win; if (link.href.search(/\?/) >= 0) { @@ -159,14 +156,14 @@ win.focus(); return false; }, - + updateObjectData: function() { var that = this; return function() { var value = that.object_input.val(); - - if (!value) { - return + + if (!value) { + return } //var this_id = that.getFkId(); $('#lookup_text_' + that.getFkId() + ' span').text('loading...'); @@ -179,7 +176,7 @@ }, success: function(item) { if (item && item.content_type_text && item.object_text) { - var url = that.getLookupUrl(that.cID, false); + var url = that.getLookupUrl(that.cID); $('#lookup_text_' + that.getFkId() + ' a') .text(item.content_type_text + ': ' + item.object_text) .attr('href', url + item.object_id); @@ -211,10 +208,10 @@ this.url_array = url_array; this.fields = fields; this.popup = popup_var || this.popup; - + // store the base element this.object_input = $("#" + this.getFkId()); - + // find the select we need to change this.object_select = this.prepareSelect($("#" + this.getCtId())); @@ -248,22 +245,22 @@ this.updateObjectData()(); } }; - + var InlineAdmin = { sub_admins: null, url_array: null, fields: null, popup: '_popup', - + install: function(fields, url_array, popup_var) { var inline_count = $('#id_' + fields.prefix + '-TOTAL_FORMS').val(), admin; - + this.url_array = url_array; this.fields = fields; this.sub_admins = []; this.popup = popup_var || this.popup; - + for (var j = 0; j < inline_count; j++) { f = $.extend({}, this.fields); f.number = j; @@ -282,7 +279,7 @@ added_fields.number = ($('#id_' + that.fields.prefix + '-TOTAL_FORMS').val() - 1); admin.install(added_fields, that.url_array, that.popup); that.sub_admins.push(admin); - + $('#' + that.fields.prefix + '-' + added_fields.number + ' .inline-deletelink').click( that.removeHandler(that) ); @@ -293,7 +290,7 @@ var parent_id, deleted_num, sub_admin; - + e.preventDefault(); parent_id = $(e.currentTarget).parents('.dynamic-' + that.fields.prefix).first().attr('id'); deleted_num = parseInt(parent_id.charAt(parent_id.length - 1), 10); @@ -318,7 +315,7 @@ ct_fields = data.fields, popup_var = data.popup_var, fields; - + for (var i = 0; i < ct_fields.length; i++) { fields = ct_fields[i]; if (fields.inline === false) {