La version 5 de Planète Casio. Regroupe le forum, les programmes, les tutoriel, les sprites et tous les autres outils développés par nos soins.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #-*- coding: utf-8 -*-
  2. from django.shortcuts import render, redirect
  3. from django.contrib.auth import authenticate
  4. from django.contrib.auth.models import User
  5. from django.contrib.auth import login as a_login, logout as a_logout
  6. from django.core.urlresolvers import reverse
  7. from django.conf import settings as s
  8. from django.http import JsonResponse
  9. from django.http import Http404
  10. from account.forms import *
  11. from home.views import homepage
  12. def login(request):
  13. """
  14. Login validation
  15. Only accessible with ajax POST from menu and return json
  16. """
  17. form = LoginForm(request.POST or None)
  18. if request.method == "POST":
  19. if form.is_valid():
  20. username = form.cleaned_data["username"]
  21. password = form.cleaned_data["password"]
  22. user = authenticate(username=username, password=password)
  23. if user:
  24. a_login(request, user)
  25. data = {
  26. 'login':True
  27. }
  28. else:
  29. data = {
  30. 'login':False
  31. }
  32. if(User.objects.filter(username=username).exists()):
  33. data['error'] = 'Mauvais mot de passe'
  34. else:
  35. data['error'] = 'Identifiant inconnu'
  36. if('HTTP_X_REQUESTED_WITH' in request.META and request.META['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'):
  37. return JsonResponse(data)
  38. return render(request, 'account/login.html', locals())
  39. else:
  40. #hack attempt, log it
  41. print(request.POST)
  42. elif request.method == "GET":
  43. return render(request, 'account/login.html')
  44. raise Http404
  45. def logout(request):
  46. """
  47. Logout user
  48. with ajax POST from menu and reload page
  49. """
  50. a_logout(request)
  51. return redirect(reverse(homepage))
  52. def signup(request):
  53. """
  54. Signup user
  55. with ajax POST from signup page and return json
  56. """
  57. form = InscriptionForm()
  58. if request.method == "POST":
  59. if form.is_valid():
  60. password1 = form.cleaned_data["password1"]
  61. password2 = form.cleaned_data["password2"]
  62. username = form.cleaned_data["username"]
  63. email = form.cleaned_data["email"]
  64. cgu = form.cleaned_data["cgu"]
  65. if(password1 != password2):
  66. data = {
  67. 'registered':False,
  68. 'error':"Les mots de passe ne sont pas identique !"
  69. }
  70. elif(User.objects.filter(username=username).exists()):
  71. data = {
  72. 'registered':False,
  73. 'error':"Ce nom d'utilisateur est déjà utilisé !"
  74. }
  75. elif(User.objects.filter(email=email).exists()):
  76. data = {
  77. 'registered':False,
  78. 'error':"Cette adresse email est déjà utilisé !"
  79. }
  80. else:
  81. create_user(username, email, password1)
  82. authenticate(username, password1)
  83. data = {
  84. 'registered':True
  85. }
  86. if('HTTP_X_REQUESTED_WITH' in request.META and request.META['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'):
  87. return JsonResponse(data)
  88. return render(request, 'account/signup.html', locals())
  89. elif request.method == "GET":
  90. return render(request, 'account/signup.html', locals())
  91. raise Http404
  92. def account(request):
  93. return render(request, 'account.html')