From b59d78f2de5b057fa641f08409528301b5a3d862 Mon Sep 17 00:00:00 2001 From: Dmitriy Zagaynov Date: Mon, 24 Jan 2022 20:44:15 +0600 Subject: [PATCH] Modified to work with Django 4.0 --- objectpermissions/__init__.py | 2 +- objectpermissions/admin.py | 16 +++++++++------- objectpermissions/models.py | 19 ++++++++++--------- objectpermissions/registration.py | 5 +++-- objectpermissions/signals.py | 4 ++-- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/objectpermissions/__init__.py b/objectpermissions/__init__.py index 87b66db..ae972b2 100644 --- a/objectpermissions/__init__.py +++ b/objectpermissions/__init__.py @@ -1,7 +1,7 @@ __version_info__ = { 'major': 0, 'minor': 3, - 'micro': 3, + 'micro': 4, 'releaselevel': 'final', 'serial': 0 } diff --git a/objectpermissions/admin.py b/objectpermissions/admin.py index 130a14b..91aca11 100644 --- a/objectpermissions/admin.py +++ b/objectpermissions/admin.py @@ -1,9 +1,10 @@ +from django.forms import forms, ModelForm +from .models import Permission, UserPermission, GroupPermission from django import forms -from django.contrib.contenttypes import generic +from django.forms.utils import ErrorList +from django.contrib.contenttypes.admin import GenericInlineModelAdmin -from models import Permission, UserPermission, GroupPermission - -class PermissionModelForm(forms.ModelForm): +class PermissionModelForm(ModelForm): """ A model form that represents the permissions as a multiple choice field. """ @@ -14,7 +15,7 @@ class Meta: exclude=['permission',] def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, - initial=None, error_class=forms.util.ErrorList, label_suffix=':', + initial=None, error_class=ErrorList, label_suffix=':', empty_permitted=False, instance=None): """ We have to set the value for ``perm_list`` since it isn't a real field. @@ -50,6 +51,7 @@ class UserPermModelForm(PermissionModelForm): """ class Meta: model=UserPermission + fields='__all__' class GroupPermModelForm(PermissionModelForm): """ @@ -57,7 +59,7 @@ class GroupPermModelForm(PermissionModelForm): """ class Meta: model=GroupPermission - + fields = '__all__' def inline_permission_form_factory(model, Form): class InlinePermissionModelForm(Form): @@ -66,7 +68,7 @@ class InlinePermissionModelForm(Form): return InlinePermissionModelForm -class InlinePermissionModelAdmin(generic.GenericInlineModelAdmin): +class InlinePermissionModelAdmin(GenericInlineModelAdmin): form = PermissionModelForm def __init__(self, parent_model, admin_site): diff --git a/objectpermissions/models.py b/objectpermissions/models.py index b91aca3..746d859 100644 --- a/objectpermissions/models.py +++ b/objectpermissions/models.py @@ -1,7 +1,8 @@ from django.db import models from django.contrib.auth.models import User, Group from django.contrib.contenttypes.models import ContentType -from django.contrib.contenttypes import generic +from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation + class UnknownPermission(Exception): """ @@ -26,9 +27,9 @@ class Permission(models.Model): A privilege granted to a specific User or Group to a specific object. """ permission = models.IntegerField(null=True, blank=True, default=0) - content_type = models.ForeignKey(ContentType) + content_type = models.ForeignKey(ContentType, on_delete=models.DO_NOTHING) object_id = models.PositiveIntegerField() - content_object = generic.GenericForeignKey('content_type', 'object_id') + content_object = GenericForeignKey('content_type', 'object_id') class Meta: abstract = True @@ -96,19 +97,19 @@ def _get_perm_as_list(self): perm_list = property(_get_perm_as_list, _set_perm_with_list, doc="The permissions as an integer list") class UserPermission(Permission): - user = models.ForeignKey(User) + user = models.ForeignKey(User, on_delete=models.DO_NOTHING) def save(self, *a, **kw): """ Send out a signal indicating that a permission was changed """ super(Permission, self).save(*a, **kw) - from signals import permission_changed + from .signals import permission_changed permission_changed.send(sender=self, to_whom=self.user, to_what=self.content_object) class GroupPermission(Permission): - group = models.ForeignKey(Group, null=True) + group = models.ForeignKey(Group, null=True, on_delete=models.DO_NOTHING) def save(self, *a, **kw): """ @@ -172,7 +173,7 @@ def as_int(self, perm): """ if isinstance(perm, int): valid_perm = perm - elif isinstance(perm, basestring): + elif isinstance(perm, str): # Look up the attribute, it will raise an error if it doesn't exist valid_perm = getattr(self, perm) elif isinstance(perm, (list, tuple)): @@ -226,7 +227,7 @@ def choice_list(self): return self.items() -class UserPermissionRelation(generic.GenericRelation): +class UserPermissionRelation(GenericRelation): """A generic relation for Object Permissions""" def __init__(self,**kwargs): @@ -234,7 +235,7 @@ def __init__(self,**kwargs): super(UserPermissionRelation, self).__init__(UserPermission, **kwargs) -class GroupPermissionRelation(generic.GenericRelation): +class GroupPermissionRelation(GenericRelation): """A generic relation for Object Permissions""" def __init__(self,**kwargs): diff --git a/objectpermissions/registration.py b/objectpermissions/registration.py index e10899f..b1c0883 100644 --- a/objectpermissions/registration.py +++ b/objectpermissions/registration.py @@ -1,8 +1,9 @@ -from django.db.models import FieldDoesNotExist, Q +from django.db.models import Q from django.contrib.contenttypes.models import ContentType from django.contrib.auth.models import User, Group +from django.core.exceptions import FieldDoesNotExist +from .models import UserPermission, GroupPermission, ModelPermissions, UserPermissionRelation, GroupPermissionRelation -from models import UserPermission, GroupPermission, ModelPermissions, UserPermissionRelation, GroupPermissionRelation class AlreadyRegistered(Exception): """ diff --git a/objectpermissions/signals.py b/objectpermissions/signals.py index f0bae9f..e4334f2 100644 --- a/objectpermissions/signals.py +++ b/objectpermissions/signals.py @@ -1,5 +1,5 @@ -import django.dispatch +from django.dispatch import Signal # Whenever a permission object is saved, it sends out the signal. This allows # models to keep their permissions in sync -permission_changed = django.dispatch.Signal(providing_args=('to_whom', 'to_what')) \ No newline at end of file +permission_changed = Signal() \ No newline at end of file