utils.py 1.3 KB
Newer Older
1
import json
2
import re
3
from os.path import join
4

5
from backend_app.settings.defaults import OBJ_MODERATION_PERMISSIONS
6
from base_app.settings.dir_locations import REPO_ROOT_DIR
7
8


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


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


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("/")
46
47
48
49
50


def get_default_theme_settings():
    with open(join(REPO_ROOT_DIR, "frontend/src/config/defaultTheme.json"), "r") as f:
        return json.load(f)