__get_viewset_permissions.py 1.45 KB
Newer Older
1
from backend.permissions import IsOwner, IsStaffOrReadOnly, IsDriOrReadOnly, ReadOnly, IsDriOrNoPost, NoPostIfNotStaff
2
3
from rest_framework.permissions import IsAdminUser
from backend.permissions import DEFAULT_VIEWSET_PERMISSIONS
4
from shared import get_api_config
5
6
7


def get_viewset_permissions(viewset):
8
9
10
11
12
13
14
15
16
17
    api_config = get_api_config()
    for obj in api_config:
        if obj['viewset'] == viewset:
            custom_permission = obj["viewset_permission"]
            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
18
19
            elif custom_permission == "IsDriOrNoPost":
                permission = (IsDriOrNoPost,)
20
21
            elif custom_permission == "NoPostIfNotStaff":
                permission = (NoPostIfNotStaff,)
22
23
24
25
26
27
28
29
30
31
32
            elif custom_permission == "IsStaff":
                permission = (IsAdminUser,)
            elif custom_permission == 'default':
                permission = tuple()
            else:
                raise Exception(
                    "Permission not supported ! Dev what did you do ?")

            if obj["read_only"]:
                permission += (ReadOnly,)
            return DEFAULT_VIEWSET_PERMISSIONS + permission
33
34

    raise Exception("Viewset not found in API configuraiton, cannot process !")