views.py 777 Bytes
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from django.contrib.auth.models import Group
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
from backend.utils import is_member
from django.http import HttpResponse
import re
import json


def role_change(request):
    user = request.user

    moderator_group = Group.objects.get(name='Moderators')
    dri_group = Group.objects.get(name='DRI')
    moderator_group.user_set.remove(user)
    dri_group.user_set.remove(user)

    path = request.path
    if (bool(re.search('dri', path))):
        dri_group.user_set.add(user)
    if (bool(re.search('moderator', path))):
        moderator_group.user_set.add(user)

    role = "normal"
    if is_member("DRI", user):
        role = 'DRI'
    if is_member('Moderators', user):
        role = 'moderator'

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