Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Modified to work with Django 4.0 #7

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion objectpermissions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__version_info__ = {
'major': 0,
'minor': 3,
'micro': 3,
'micro': 4,
'releaselevel': 'final',
'serial': 0
}
Expand Down
16 changes: 9 additions & 7 deletions objectpermissions/admin.py
Original file line number Diff line number Diff line change
@@ -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.
"""
Expand All @@ -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.
Expand Down Expand Up @@ -50,14 +51,15 @@ class UserPermModelForm(PermissionModelForm):
"""
class Meta:
model=UserPermission
fields='__all__'

class GroupPermModelForm(PermissionModelForm):
"""
Subclass the :class:`PermissionModelForm` for :class:`GroupPermission`
"""
class Meta:
model=GroupPermission

fields = '__all__'

def inline_permission_form_factory(model, Form):
class InlinePermissionModelForm(Form):
Expand All @@ -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):
Expand Down
19 changes: 10 additions & 9 deletions objectpermissions/models.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand All @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)):
Expand Down Expand Up @@ -226,15 +227,15 @@ def choice_list(self):
return self.items()


class UserPermissionRelation(generic.GenericRelation):
class UserPermissionRelation(GenericRelation):
"""A generic relation for Object Permissions"""

def __init__(self,**kwargs):
"""Override this to automatically set the "to" field """
super(UserPermissionRelation, self).__init__(UserPermission, **kwargs)


class GroupPermissionRelation(generic.GenericRelation):
class GroupPermissionRelation(GenericRelation):
"""A generic relation for Object Permissions"""

def __init__(self,**kwargs):
Expand Down
5 changes: 3 additions & 2 deletions objectpermissions/registration.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand Down
4 changes: 2 additions & 2 deletions objectpermissions/signals.py
Original file line number Diff line number Diff line change
@@ -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'))
permission_changed = Signal()