get_viewset_permissions.py 1.46 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import yaml
from os.path import join, realpath, dirname
from backend.permissions import IsOwner, IsStaffOrReadOnly, IsDriOrReadOnly
from rest_framework.permissions import IsAdminUser
from backend.permissions import DEFAULT_VIEWSET_PERMISSIONS


def get_viewset_permissions(viewset):
    current_dir = dirname(realpath(__file__))
    api_file_loc = join(current_dir, '../../general/api/api_config.yml')
    with open(api_file_loc, "r") as file:
        api_config = yaml.load(file.read())
        for obj in api_config:
            if obj['viewset'] == viewset:
                try:
                    custom_permission = obj["viewset_permission"]
                    if custom_permission == "IsOwner":
                        permission = (IsOwner,)
                    elif custom_permission == "IsStaffOrReadOnly":
                        permission = (IsStaffOrReadOnly,)
                    elif custom_permission == "IsDriOrReadOnly":
                        permission = (IsDriOrReadOnly,)
                    elif custom_permission == "IsStaff":
                        permission = (IsAdminUser,)
                    else:
                        raise Exception(
                            "Permission not supported ! Dev what did you do ?")
                    return DEFAULT_VIEWSET_PERMISSIONS + permission
                except KeyError:
                    return DEFAULT_VIEWSET_PERMISSIONS

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