PCv5/account/views.py

102 lines
3.5 KiB
Python
Raw Normal View History

2017-04-08 18:31:33 +02:00
#-*- coding: utf-8 -*-
2016-10-02 17:07:47 +02:00
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate
2017-04-01 20:20:01 +02:00
from django.contrib.auth.models import User
2016-10-02 17:07:47 +02:00
from django.contrib.auth import login as a_login, logout as a_logout
from django.core.urlresolvers import reverse
2017-04-01 20:20:01 +02:00
from django.conf import settings as s
2017-04-02 14:40:38 +02:00
from django.http import JsonResponse
2017-04-08 18:31:33 +02:00
from django.http import Http404
from account.forms import *
from home.views import homepage
2017-04-01 20:20:01 +02:00
2016-10-02 17:07:47 +02:00
def login(request):
2017-04-02 14:40:38 +02:00
"""
Login validation
Only accessible with ajax POST from menu and return json
"""
2017-04-03 01:21:34 +02:00
form = LoginForm(request.POST or None)
2016-10-02 17:07:47 +02:00
if request.method == "POST":
if form.is_valid():
username = form.cleaned_data["username"]
password = form.cleaned_data["password"]
user = authenticate(username=username, password=password)
if user:
a_login(request, user)
2017-04-02 14:40:38 +02:00
data = {
'login':True
}
2016-10-02 17:07:47 +02:00
else:
2017-04-02 14:40:38 +02:00
data = {
2017-04-03 12:44:58 +02:00
'login':False
2017-04-02 14:40:38 +02:00
}
2017-04-03 12:44:58 +02:00
if(User.objects.filter(username=username).exists()):
data['error'] = 'Mauvais mot de passe'
else:
data['error'] = 'Identifiant inconnu'
2017-04-03 01:21:34 +02:00
if('HTTP_X_REQUESTED_WITH' in request.META and request.META['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'):
2017-04-02 14:40:38 +02:00
return JsonResponse(data)
2017-04-03 12:44:58 +02:00
return render(request, 'account/login.html', locals())
2017-04-03 01:21:34 +02:00
else:
2017-04-08 18:31:33 +02:00
#hack attempt, log it
2017-04-03 01:21:34 +02:00
print(request.POST)
elif request.method == "GET":
2017-04-03 12:44:58 +02:00
return render(request, 'account/login.html')
2017-04-08 18:31:33 +02:00
raise Http404
2016-10-02 17:07:47 +02:00
def logout(request):
2017-04-02 14:40:38 +02:00
"""
Logout user
with ajax POST from menu and reload page
"""
2017-04-01 20:20:01 +02:00
a_logout(request)
2016-10-02 17:07:47 +02:00
return redirect(reverse(homepage))
2017-04-01 20:20:01 +02:00
def signup(request):
2017-04-02 14:40:38 +02:00
"""
Signup user
with ajax POST from signup page and return json
"""
2017-04-01 20:20:01 +02:00
form = InscriptionForm()
if request.method == "POST":
if form.is_valid():
password1 = form.cleaned_data["password1"]
password2 = form.cleaned_data["password2"]
username = form.cleaned_data["username"]
email = form.cleaned_data["email"]
cgu = form.cleaned_data["cgu"]
2017-04-03 12:44:58 +02:00
if(password1 != password2):
2017-04-02 14:40:38 +02:00
data = {
2017-04-03 12:44:58 +02:00
'registered':False,
'error':"Les mots de passe ne sont pas identique !"
2017-04-02 14:40:38 +02:00
}
2017-04-03 12:44:58 +02:00
elif(User.objects.filter(username=username).exists()):
2017-04-02 14:40:38 +02:00
data = {
2017-04-03 12:44:58 +02:00
'registered':False,
'error':"Ce nom d'utilisateur est déjà utilisé !"
}
elif(User.objects.filter(email=email).exists()):
data = {
'registered':False,
'error':"Cette adresse email est déjà utilisé !"
}
else:
create_user(username, email, password1)
authenticate(username, password1)
data = {
'registered':True
2017-04-02 14:40:38 +02:00
}
2017-04-01 20:20:01 +02:00
2017-04-03 12:44:58 +02:00
if('HTTP_X_REQUESTED_WITH' in request.META and request.META['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'):
return JsonResponse(data)
return render(request, 'account/signup.html', locals())
elif request.method == "GET":
return render(request, 'account/signup.html', locals())
2017-04-08 18:31:33 +02:00
raise Http404
2017-04-01 20:20:01 +02:00
2016-10-02 17:07:47 +02:00
def account(request):
return render(request, 'account.html')