__get_viewset_permissions.py 1.47 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
11
from shared import load_api_config
12
13
14


def get_viewset_permissions(viewset):
15
    api_config = load_api_config()
16
    for obj in api_config:
17
        if obj["viewset"] == viewset:
18
19
20
21
22
23
24
            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
25
26
            elif custom_permission == "IsDriOrNoPost":
                permission = (IsDriOrNoPost,)
27
28
            elif custom_permission == "NoPostIfNotStaff":
                permission = (NoPostIfNotStaff,)
29
30
            elif custom_permission == "IsStaff":
                permission = (IsAdminUser,)
31
            elif custom_permission == "default":
32
33
                permission = tuple()
            else:
34
                raise Exception("Permission not supported ! Dev what did you do ?")
35
36
37
38

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

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