Skip to content

Commit 4523df9

Browse files
committed
Decouple viewset mixins from models
1 parent cdfb83f commit 4523df9

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

rules/contrib/rest_framework.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
2+
23
from ..viewsets import BaseAutoPermissionMixin
34

5+
46
class AutoPermissionViewSetMixin(BaseAutoPermissionMixin):
57
"""
68
Enforces object-level permissions in ``rest_framework.viewsets.ViewSet``,

rules/viewsets.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# All auto permission mixins (both for DRF and Django views)
22
# inherit from this mixin.
33
class BaseAutoPermissionMixin:
4+
def get_perm(self, model, perm_type):
5+
return "%s.%s_%s" % (model._meta.app_label, perm_type, model._meta.model_name)
6+
47
def get_permission_for_model(self, model, perm_type):
5-
return model.get_perm(perm_type)
8+
9+
# Attempt to use model mixin. If model mixin is not available,
10+
# default to standard convetion of this project (or allow
11+
# overriding `get_perm` through a custom Mixin)
12+
try:
13+
use_model_mixin_method = callable(model.get_perm)
14+
except AttributeError:
15+
use_model_mixin_method = False
16+
17+
if use_model_mixin_method:
18+
perm = model.get_perm(perm_type)
19+
else:
20+
perm = self.get_perm(model, perm_type)
21+
return perm

0 commit comments

Comments
 (0)