utils.py 1.06 KB
Newer Older
1
import re
2

3
from backend_app.settings.defaults import OBJ_MODERATION_PERMISSIONS
4
5


6
def get_user_level(user) -> int:
7
8
9
10
11
12
13
14
15
16
17
18
19
    """
    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"]


20
def is_member(group_name: str, user) -> bool:
21
22
23
24
25
26
27
    """
    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()
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42


def clean_route(route):
    """
    Function to clean the route as it is stored in the viewsets.

    :param route: string
    :return: string
    """

    # Remove required parameters
    out = re.sub(r"\(.*\)", "", route)
    # Clean the string
    out = out.replace("//", "/")
    return out.rstrip("/")