utils.py 1.11 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
33
34
35
36
37
38
39
from backend_app.config.other import OBJ_MODERATION_PERMISSIONS
from backend_app.config.utils import load_viewsets_config
from base_app.models import User


def find_api_end_point_for_viewset(viewset_name: str) -> str:
    """
    Gets the api endpoint associated with a viewset
    """

    for obj in load_viewsets_config():
        if obj.viewset == viewset_name:
            return obj.api_end_point

    return None


def get_user_level(user: User) -> int:
    """
    Returns the user level as int.
    """
    if user.is_staff:
        return OBJ_MODERATION_PERMISSIONS["staff"]
    elif is_member("DRI", user):
        return OBJ_MODERATION_PERMISSIONS["DRI"]
    elif is_member("Moderators", user):
        return OBJ_MODERATION_PERMISSIONS["moderator"]
    else:
        return OBJ_MODERATION_PERMISSIONS["authenticated_user"]


def is_member(group_name: str, user: User) -> bool:
    """
    Function to know if a user is part of a specific group.
    """
    return group_name in user.cached_groups
    # before:
    # When we were using the standard django model
    # return user.groups.filter(name=group_name).exists()