__get_viewset_permissions.py 1.63 KB
Newer Older
1 2 3 4 5 6 7 8
from backend_app.permissions import (
    IsOwner,
    IsStaffOrReadOnly,
    IsDriOrReadOnly,
    ReadOnly,
    IsDriOrNoPost,
    NoPostIfNotStaff,
)
9
from rest_framework.permissions import IsAdminUser
Florent Chehab's avatar
Florent Chehab committed
10
from backend_app.permissions import DEFAULT_VIEWSET_PERMISSIONS
Florent Chehab's avatar
Florent Chehab committed
11
from shared import get_api_objs
12 13


Florent Chehab's avatar
Florent Chehab committed
14 15 16 17 18 19 20 21
def get_viewset_permissions(viewset: str) -> object:
    """
    Returns the permissions associated with  the viewset as configured in the config file.
    """

    for obj in get_api_objs(has_model=None, make_imports=False, is_api_view=None):
        if obj.viewset == viewset:
            custom_permission = obj.viewset_permission
22 23 24 25 26 27
            if custom_permission == "IsOwner":
                permission = (IsOwner,)
            elif custom_permission == "IsStaffOrReadOnly":
                permission = (IsStaffOrReadOnly,)
            elif custom_permission == "IsDriOrReadOnly":
                permission = (IsDriOrReadOnly,)
Florent Chehab's avatar
Florent Chehab committed
28 29
            elif custom_permission == "IsDriOrNoPost":
                permission = (IsDriOrNoPost,)
30 31
            elif custom_permission == "NoPostIfNotStaff":
                permission = (NoPostIfNotStaff,)
32 33
            elif custom_permission == "IsStaff":
                permission = (IsAdminUser,)
34
            elif custom_permission == "default":
35 36
                permission = tuple()
            else:
37
                raise Exception("Permission not supported ! Dev what did you do ?")
38

Florent Chehab's avatar
Florent Chehab committed
39
            if obj.read_only:
40 41
                permission += (ReadOnly,)
            return DEFAULT_VIEWSET_PERMISSIONS + permission
42

Florent Chehab's avatar
Florent Chehab committed
43 44 45
    raise Exception(
        "Viewset {} not found in API configuraiton, cannot proceed !".format(viewset)
    )