__list_user_post_permission.py 1.29 KB
Newer Older
1
from typing import List
2

3
from base_app.models import User
4

5
from shared import get_api_objs
6
7

ALL_VIEWSETS = {}
8

9
10
11
12
13
14
15
16
17
18
19
20
for api_obj in get_api_objs(
    has_model=None,
    requires_testing=False,
    is_api_view=False,
    ignore_models=["UserData"],
):
    ALL_VIEWSETS[api_obj.viewset] = api_obj.Viewset

for api_obj in get_api_objs(
    has_model=None, requires_testing=True, is_api_view=False, ignore_models=["UserData"]
):
    ALL_VIEWSETS[api_obj.viewset] = api_obj.Viewset
21
22
23
24
25
26
27


class Request(object):
    def __init__(self, user, method):
        self.user = user
        self.method = method

28

29
30
31
32
33
def list_user_post_permission(user: User) -> List[str]:
    """
    Function the list the viewset to which a user can submit a post request to.
    """

34
    viewsets_user_can_post = []
35
    request = Request(user, "POST")
36
37
38
39
40
41
42
43
    for viewset_name in ALL_VIEWSETS:
        viewset = ALL_VIEWSETS[viewset_name]
        user_can_post = True
        for permission_class in viewset.permission_classes:
            if not permission_class.has_permission(None, request, None):
                user_can_post = False
                break
        if user_can_post:
44
            name = viewset_name.split("ViewSet")[0]
45
46
47
            name = name[0].lower() + name[1:]
            viewsets_user_can_post.append(name)
    return viewsets_user_can_post