Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django 2.2 #55

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ MANIFEST
*egg*
*.bak
*.tmproj
.idea
28 changes: 21 additions & 7 deletions genericadmin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
except ImportError:
from django.contrib.admin.options import IS_POPUP_VAR
from django.core.exceptions import ObjectDoesNotExist
from django.views.generic import RedirectView

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

Expand Down Expand Up @@ -63,7 +64,12 @@ def get_generic_field_list(self, request, prefix=''):
fields['prefix'] = prefix
field_list.append(fields)
else:
for field in self.model._meta.virtual_fields:
# virtual_fields were deprecated in django 2.0
try:
fields = self.model._meta.virtual_fields
except AttributeError:
fields = self.model._meta.private_fields
for field in fields:
if isinstance(field, GenericForeignKey) and \
field.ct_field not in exclude and field.fk_field not in exclude:
field_list.append({
Expand All @@ -74,10 +80,16 @@ def get_generic_field_list(self, request, prefix=''):
})

if hasattr(self, 'inlines') and len(self.inlines) > 0:
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)
try:
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)
except AttributeError:
for FormSet, inline in self.get_formsets_with_inlines(request):
if hasattr(inline, 'get_generic_field_list'):
prefix = FormSet.get_default_prefix()
field_list = field_list + inline.get_generic_field_list(request, prefix)

return field_list

Expand All @@ -90,10 +102,12 @@ def wrapper(*args, **kwargs):
custom_urls = [
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'),
url(r'^(\d+)/obj-data/$', wrap(self.generic_lookup), name='admin_genericadmin_obj_lookup_change'),
url(r'^(\d+)/genericadmin-init/change/$', wrap(self.genericadmin_js_init), name='admin_genericadmin_init_change'),
]
return custom_urls + super(BaseGenericModelAdmin, self).get_urls()

def genericadmin_js_init(self, request):
def genericadmin_js_init(self, request, pk=None):
if request.method == 'GET':
obj_dict = {}
for c in ContentType.objects.all():
Expand All @@ -115,7 +129,7 @@ def genericadmin_js_init(self, request):
return HttpResponse(resp, content_type='application/json')
return HttpResponseNotAllowed(['GET'])

def generic_lookup(self, request):
def generic_lookup(self, request, pk=None):
if request.method != 'GET':
return HttpResponseNotAllowed(['GET'])

Expand Down
64 changes: 35 additions & 29 deletions genericadmin/static/genericadmin/js/genericadmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
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 @@ -62,7 +62,7 @@
},

getLookupUrlParams: function(cID) {
var q = this.url_array[cID][1] || {},
var q = this.url_array[cID][1] || {},
str = [];
for(var p in q) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(q[p]));
Expand All @@ -71,52 +71,58 @@
url = x ? ("?" + x) : "";
return url;
},

getLookupUrl: function(cID) {
return '../../../' + this.url_array[cID][0] + '/' + this.getLookupUrlParams(cID);
var forword = '../../../';
var suffix = '/change/';
var href = window.location.href;
if (href.indexOf(suffix, href.length - suffix.length) !== -1) {
forword += '../';
}
return forword + 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),
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 @@ -128,11 +134,11 @@
},
150);
},

popRelatedObjectLookup: function(link) {
var name = id_to_windowname(this.getFkId()),
url_parts = [],
href,
href,
win;

if (link.href.search(/\?/) >= 0) {
Expand All @@ -156,14 +162,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 Down Expand Up @@ -208,10 +214,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 @@ -245,22 +251,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 @@ -279,7 +285,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 @@ -290,7 +296,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 @@ -315,7 +321,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 All @@ -327,4 +333,4 @@
}
});
});
} (django.jQuery));
} (django.jQuery || jQuery));
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def convert_readme():

setup(
name='django-genericadmin',
version='0.7.0',
version='0.7.1',
description="Adds support for generic relations within Django's admin interface.",
author='Weston Nielson, Jan Schrewe, Arthur Hanson',
author_email='[email protected], [email protected], [email protected]',
Expand Down