Blame view

src/core/pyros_django/user_manager/views.py 4.22 KB
a01cb74b   ALEXIS-PC\alexis   adding redirectio...
1
from django.shortcuts import render,redirect
1cffbf1c   Etienne Pallier   moved pyros_djang...
2
3
4
5
6
7
8
9
10
11
12
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from .forms import PyrosUserCreationForm

LOGGED_PAGE = "../../dashboard/templates/dashboard/index.html"

def home(request):
    '''
        Initial login view when coming on the website
    '''
    if request.user.is_authenticated:
7ae4faa1   ALEXIS-PC\alexis   Change in PyrosUs...
13
        return(render(request, LOGGED_PAGE, {'USER_LEVEL': request.user.get_priority(), 'base_template' : "base.html", 'weather_img': "normal"}))
1cffbf1c   Etienne Pallier   moved pyros_djang...
14
15
16
17
18
19
20
    return(render(request, LOGGED_PAGE, {'USER_LEVEL': 0, 'base_template' : 'base_unlogged.html', 'weather_img': "red"}))

def create_user(request):
    '''
        View called to open the user creation form
    '''
    if request.user.is_authenticated:
7ae4faa1   ALEXIS-PC\alexis   Change in PyrosUs...
21
        return(render(request, LOGGED_PAGE, {'USER_LEVEL': request.user.get_priority(), 'base_template' : "base.html", 'weather_img': "normal"}))
1cffbf1c   Etienne Pallier   moved pyros_djang...
22
23
24
25
26
27
28
29
    form = PyrosUserCreationForm()
    return (render(request, "user_manager/home_user_creation.html", locals()))

def user_signup_validation(request):
    '''
        View called to validate the user creation (form submitted)
    '''
    if request.user.is_authenticated:
7ae4faa1   ALEXIS-PC\alexis   Change in PyrosUs...
30
        return(render(request, LOGGED_PAGE, {'USER_LEVEL': request.user.get_priority(), 'base_template' : "base.html", 'weather_img': "normal"}))
1cffbf1c   Etienne Pallier   moved pyros_djang...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    form = PyrosUserCreationForm(request.POST)
    if request.POST:
        if form.is_valid():
            form.save()
            message = "Account creation successful ! Login to continue"
            success = True
            return(render(request, "user_manager/home_login.html", locals()))
        else:
            message = "One or more fields contain errors. Please try again"

    else:
        message = "The system encountered an error. Please try again"

    error = True
    return (render(request, "user_manager/home_user_creation.html", locals()))

def login_validation(request):
    '''
        View called when the user log in (form submitted)
    '''
    if request.user.is_authenticated:
a01cb74b   ALEXIS-PC\alexis   adding redirectio...
52
53
        if request.POST.get("next"):
            return redirect(request.POST.get('next'))
7ae4faa1   ALEXIS-PC\alexis   Change in PyrosUs...
54
        return(render(request, LOGGED_PAGE, {'USER_LEVEL': request.user.get_priority(),'base_template' : "base.html", 'weather_img': "normal"}))
1cffbf1c   Etienne Pallier   moved pyros_djang...
55
56
57
58
59
60
61
62
63
64
65
66
    username = password = ''
    if request.POST:
        email = request.POST.get('email')
        password = request.POST.get('password')
        user = authenticate(username=email, password=password)
        if user is not None:
            success = False
            if user.is_active:
                login(request, user)
                request.session['user'] = email
                message = "Oui"
                success = True
a01cb74b   ALEXIS-PC\alexis   adding redirectio...
67
68
                if request.POST.get("next"):
                    return redirect(request.POST.get('next'))
7ae4faa1   ALEXIS-PC\alexis   Change in PyrosUs...
69
                return(render(request, LOGGED_PAGE, {'USER_LEVEL': request.user.get_priority(), 'base_template' : "base.html", 'weather_img': "normal", 'success' : success}))
1cffbf1c   Etienne Pallier   moved pyros_djang...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
            else:
                message = "Your account is not active, please contact the site administrator."
        else:
            message = "Your email and/or password were incorrect."
    else:
        message = "An unexpected error has occurred"
    error = True
    return(render(request, "user_manager/home_login.html", locals()))

@login_required
def profile(request):
    '''
        View called to see the current user profile
    '''
    current_user = request.user
7ae4faa1   ALEXIS-PC\alexis   Change in PyrosUs...
85
86
    USER_LEVEL = request.user.get_priority()
    if (current_user.get_priority() < 4):
1cffbf1c   Etienne Pallier   moved pyros_djang...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        return(render(request, "dashboard/user_detail.html", {'user': current_user, 'admin': 0}))
    return(render(request, "user_manager/profile.html", locals()))

@login_required
def superoperator_return(request):
    current_user = request.user
    return(render(request, "dashboard/user_detail.html", {'user': current_user, 'admin': 0}))

@login_required
def user_logout(request):
    '''
        View called to log out. Redirects on login page.
    '''

    logout(request)
    return(render(request, LOGGED_PAGE, {'USER_LEVEL' :  0, 'base_template' : 'base_unlogged.html', 'weather_img': "red"}))

def user_signin(request):
a01cb74b   ALEXIS-PC\alexis   adding redirectio...
105
    return(render(request, "user_manager/home_login.html",{"next": request.GET.get("next")}))