Skip to content

Commit

Permalink
fixes for django 1.11
Browse files Browse the repository at this point in the history
  • Loading branch information
arthanson committed Apr 25, 2018
1 parent 85980c7 commit 91d250c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 60 deletions.
Empty file modified genericadmin/__init__.py
100644 → 100755
Empty file.
32 changes: 7 additions & 25 deletions genericadmin/admin.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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'])

Expand Down
67 changes: 32 additions & 35 deletions genericadmin/static/genericadmin/js/genericadmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
Expand All @@ -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) {
Expand All @@ -47,7 +47,7 @@
}
});
select.empty().append(no_value);

opt_keys = opt_keys.sort();

$.each(opt_keys, function(index, key) {
Expand All @@ -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]));
Expand All @@ -72,54 +71,52 @@
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;
} else {
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;
} else {
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 = '<a class="related-lookup" id="' + id + '" href="' + url + '">&nbsp;</a>';

link = link + '<strong id="lookup_text_'+ this.getFkId() +'" margin-left: 5px"><a target="_new" href="#"></a><span></span></strong>';

// insert link html after input element
this.object_input.after(link);

return id;
},

pollInputChange: function(window) {
var that = this,
interval_id = setInterval(function() {
Expand All @@ -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) {
Expand All @@ -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...');
Expand All @@ -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);
Expand Down Expand Up @@ -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()));

Expand Down Expand Up @@ -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;
Expand All @@ -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)
);
Expand All @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit 91d250c

Please sign in to comment.