Skip to content

Commit

Permalink
适配django 2.1 2.0 fixed : #612
Browse files Browse the repository at this point in the history
  • Loading branch information
wgbbiao committed Aug 15, 2018
1 parent d7e283f commit a9daa73
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions xadmin/plugins/actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import OrderedDict
from django import forms
from django import forms, VERSION as django_version
from django.core.exceptions import PermissionDenied
from django.db import router
from django.http import HttpResponse, HttpResponseRedirect
Expand All @@ -19,19 +19,23 @@
from xadmin.views import BaseAdminPlugin, ListAdminView
from xadmin.views.base import filter_hook, ModelAdminView

from xadmin import views

ACTION_CHECKBOX_NAME = '_selected_action'
checkbox = forms.CheckboxInput({'class': 'action-select'}, lambda value: False)


def action_checkbox(obj):
return checkbox.render(ACTION_CHECKBOX_NAME, force_text(obj.pk))


action_checkbox.short_description = mark_safe(
'<input type="checkbox" id="action-toggle" />')
action_checkbox.allow_tags = True
action_checkbox.allow_export = False
action_checkbox.is_column = False


class BaseActionView(ModelAdminView):
action_name = None
description = None
Expand Down Expand Up @@ -70,7 +74,7 @@ def delete_models(self, queryset):
n = queryset.count()
if n:
if self.delete_models_batch:
self.log('delete', _('Batch delete %(count)d %(items)s.') % { "count": n, "items": model_ngettext(self.opts, n) })
self.log('delete', _('Batch delete %(count)d %(items)s.') % {"count": n, "items": model_ngettext(self.opts, n)})
queryset.delete()
else:
for obj in queryset:
Expand All @@ -88,10 +92,18 @@ def do_action(self, queryset):

using = router.db_for_write(self.model)

if django_version > (2, 0):
setattr(self.admin_site._registry[self.model], 'has_delete_permission', self.has_delete_permission)

# Populate deletable_objects, a data structure of all related objects that
# will also be deleted.
deletable_objects, model_count, perms_needed, protected = get_deleted_objects(
queryset, self.opts, self.user, self.admin_site, using)

if django_version > (2, 0):

This comment has been minimized.

Copy link
@Flamingo-Chen

Flamingo-Chen Oct 9, 2018

Django2.0报错

This comment has been minimized.

Copy link
@hongweipeng

hongweipeng Oct 30, 2018

对,这里的判断有误,至少在django2.0.9里面传递是5个参数

deletable_objects, model_count, perms_needed, protected = get_deleted_objects(
queryset, self.opts, self.admin_site)
else:
deletable_objects, model_count, perms_needed, protected = get_deleted_objects(
queryset, self.opts, self.user, self.admin_site, using)

# The user has already confirmed the deletion.
# Do the deletion and return a None to display the change list view again.
Expand Down
2 changes: 1 addition & 1 deletion xadmin/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,6 @@ def has_change_permission(self, obj=None):
codename = get_permission_codename('change', self.opts)
return ('change' not in self.remove_permissions) and self.user.has_perm('%s.%s' % (self.app_label, codename))

def has_delete_permission(self, obj=None):
def has_delete_permission(self, request=None, obj=None):
codename = get_permission_codename('delete', self.opts)
return ('delete' not in self.remove_permissions) and self.user.has_perm('%s.%s' % (self.app_label, codename))

0 comments on commit a9daa73

Please sign in to comment.