from django.shortcuts import render 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: return(render(request, LOGGED_PAGE, {'level': request.user.user_level.priority, 'base_template' : "base.html", 'weather_img': "normal"})) return(render(request, LOGGED_PAGE, {'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: return(render(request, LOGGED_PAGE, {'level': request.user.user_level.priority, 'base_template' : "base.html", 'weather_img': "normal"})) 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: return(render(request, LOGGED_PAGE, {'level': request.user.user_level.priority, 'base_template' : "base.html", 'weather_img': "normal"})) 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: return(render(request, LOGGED_PAGE, {'level': request.user.user_level.priority,'base_template' : "base.html", 'weather_img': "normal"})) 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: if user.is_active: login(request, user) request.session['user'] = email message = "Oui" success = True return(render(request, LOGGED_PAGE, {'level': request.user.user_level.priority, 'base_template' : "base.html", 'weather_img': "normal"})) 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 if (current_user.user_level.priority < 5): return(render(request, "dashboard/user_detail.html", {'user': current_user, 'admin': 0})) return(render(request, "user_manager/profile.html", locals())) @login_required def user_logout(request): ''' View called to log out. Redirects on login page. ''' logout(request) return(render(request, LOGGED_PAGE, {'level' : 0, 'base_template' : 'base_unlogged.html', 'weather_img': "red"})) def user_signin(request): return(render(request, "user_manager/home_login.html"))