Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. def login(request):
  2. print("test")
  3. if request.method == 'POST':
  4. form = LoginForm(request.POST)
  5. if form.is_valid():
  6. username = form.cleaned_data['username']
  7. password = form.cleaned_data['password']
  8. user = authenticate(username=username, password=password)
  9. if user is not None:
  10. return HttpResponseRedirect('/accountManagement/home')
  11. else:
  12. form = LoginForm()
  13. else:
  14. HttpResponse("form is not valid")
  15. else:
  16. form = LoginForm()
  17. return render(request, 'accountManagement/login.html', {'form': form})
  18.  
  19. def home(request):
  20. print(request.user.username)
  21. if request.user.is_authenticated:
  22. passwordForm = ChangePasswordForm()
  23. emailForm = ChangeEmailForm()
  24. return render(request, 'accountManagement/home.html', {'passwordForm': passwordForm, 'emailForm': emailForm})
  25. else:
  26. return HttpResponseRedirect("/accountManagement/")
  27.  
  28.  
  29. def change_password(request):
  30. if request.user.is_authenticated:
  31. if request.method == 'POST':
  32. passwordForm = ChangePasswordForm(request.POST)
  33. if passwordForm.is_valid():
  34. oldPassword = passwordForm.cleaned_data['oldPassword']
  35. newPassword = passwordForm.cleaned_data['newPassword']
  36. newPasswordConfirmation = passwordForm.cleaned_data['newPasswordConfirmation']
  37. if (newPassword == newPasswordConfirmation) and (request.user.check_password(oldPassword)):
  38. request.user.set_password(newPassword)
  39. request.user.save()
  40. return HttpResponseRedirect("/accountManagement/logout")
  41.  
  42. else:
  43. return HttpResponse("password change failed")
  44. else:
  45. return HttpResponse("password form not valid")
  46. else:
  47. return HttpResponse("request != POST")
  48. else:
  49. return HttpResponse("user ist not authenticated")
  50.  
  51. urlpatterns = [
  52. url(r'^$', views.login, name='login'),
  53. url(r'^home', views.home, name='home'),
  54. url(r'^changeEmail', views.change_email, name='changeEmail'),
  55. url(r'^changePassword', views.change_password, name='changePassword'),
  56. url(r'^logout', views.logout_view, name='logout'),
  57. ]
  58.  
  59. class LoginForm(forms.Form):
  60. username = forms.CharField(label='Username', max_length=20)
  61. password = forms.CharField(label='Password', max_length=20)
  62.  
  63.  
  64. class ChangeEmailForm(forms.Form):
  65. newEmail = forms.CharField(label='New Email', max_length=50)
  66.  
  67.  
  68. class ChangePasswordForm(forms.Form):
  69. oldPassword = forms.CharField(label='Old Password', max_length=20)
  70. newPassword = forms.CharField(label='New Password', max_length=20)
  71. newPasswordConfirmation = forms.CharField(label='Confirm new Password', max_length=20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement