views.py 763 Bytes
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from django.contrib.auth.models import Group
Florent Chehab's avatar
Florent Chehab committed
2
from backend_app.utils import is_member
3
4
5
6
7
8
9
10
from django.http import HttpResponse
import re
import json


def role_change(request):
    user = request.user

11
12
    moderator_group = Group.objects.get(name="Moderators")
    dri_group = Group.objects.get(name="DRI")
13
14
15
16
    moderator_group.user_set.remove(user)
    dri_group.user_set.remove(user)

    path = request.path
17
    if bool(re.search("dri", path)):
18
        dri_group.user_set.add(user)
19
    if bool(re.search("moderator", path)):
20
21
22
23
        moderator_group.user_set.add(user)

    role = "normal"
    if is_member("DRI", user):
24
25
26
        role = "DRI"
    if is_member("Moderators", user):
        role = "moderator"
27

28
    return HttpResponse(json.dumps({"role_actuel": role}))