21 lines
634 B
Python
21 lines
634 B
Python
|
|
"""Reusable permission classes."""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from rest_framework.permissions import SAFE_METHODS, BasePermission
|
||
|
|
|
||
|
|
|
||
|
|
class IsAdminRole(BasePermission):
|
||
|
|
"""Only users with the ADMIN role (or Django superusers)."""
|
||
|
|
|
||
|
|
message = "This action requires the admin role."
|
||
|
|
|
||
|
|
def has_permission(self, request, view) -> bool: # noqa: ANN001, ARG002
|
||
|
|
user = request.user
|
||
|
|
return bool(user and user.is_authenticated and user.is_admin_role)
|
||
|
|
|
||
|
|
|
||
|
|
class ReadOnly(BasePermission):
|
||
|
|
def has_permission(self, request, view) -> bool: # noqa: ANN001, ARG002
|
||
|
|
return request.method in SAFE_METHODS
|