views.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. from __future__ import unicode_literals
  2. from django.contrib.auth import (login as auth_login, authenticate,
  3. logout as auth_logout, get_user_model)
  4. from django.contrib.auth.decorators import login_required
  5. from django.contrib.messages import info, error
  6. from django.core.urlresolvers import NoReverseMatch, get_script_prefix
  7. from django.shortcuts import get_object_or_404, redirect
  8. from django.template.response import TemplateResponse
  9. from django.utils.translation import ugettext_lazy as _
  10. from mezzanine.accounts import get_profile_form
  11. from mezzanine.accounts.forms import LoginForm, PasswordResetForm
  12. from mezzanine.conf import settings
  13. from mezzanine.utils.email import send_verification_mail, send_approve_mail
  14. from mezzanine.utils.urls import login_redirect, next_url
  15. User = get_user_model()
  16. def login(request, template="accounts/account_login.html",
  17. form_class=LoginForm, extra_context=None):
  18. """
  19. Login form.
  20. """
  21. form = form_class(request.POST or None)
  22. if request.method == "POST" and form.is_valid():
  23. authenticated_user = form.save()
  24. info(request, _("Successfully logged in"))
  25. auth_login(request, authenticated_user)
  26. return login_redirect(request)
  27. context = {"form": form, "title": _("Log in")}
  28. context.update(extra_context or {})
  29. # agregando para mas tiempo de expiracion de sesion
  30. request.session.set_expiry(300000)
  31. return TemplateResponse(request, template, context)
  32. def logout(request):
  33. """
  34. Log the user out.
  35. """
  36. auth_logout(request)
  37. info(request, _("Successfully logged out"))
  38. return redirect(next_url(request) or get_script_prefix())
  39. def signup(request, template="accounts/account_signup.html",
  40. extra_context=None):
  41. """
  42. Signup form.
  43. """
  44. profile_form = get_profile_form()
  45. form = profile_form(request.POST or None, request.FILES or None)
  46. if request.method == "POST" and form.is_valid():
  47. new_user = form.save()
  48. if not new_user.is_active:
  49. if settings.ACCOUNTS_APPROVAL_REQUIRED:
  50. send_approve_mail(request, new_user)
  51. info(request, _("Thanks for signing up! You'll receive "
  52. "an email when your account is activated."))
  53. else:
  54. send_verification_mail(request, new_user, "signup_verify")
  55. info(request, _("A verification email has been sent with "
  56. "a link for activating your account."))
  57. return redirect(next_url(request) or "/")
  58. else:
  59. info(request, _("Successfully signed up"))
  60. auth_login(request, new_user)
  61. return login_redirect(request)
  62. context = {"form": form, "title": _("Sign up")}
  63. context.update(extra_context or {})
  64. return TemplateResponse(request, template, context)
  65. def signup_verify(request, uidb36=None, token=None):
  66. """
  67. View for the link in the verification email sent to a new user
  68. when they create an account and ``ACCOUNTS_VERIFICATION_REQUIRED``
  69. is set to ``True``. Activates the user and logs them in,
  70. redirecting to the URL they tried to access when signing up.
  71. """
  72. user = authenticate(uidb36=uidb36, token=token, is_active=False)
  73. if user is not None:
  74. user.is_active = True
  75. user.save()
  76. auth_login(request, user)
  77. info(request, _("Successfully signed up"))
  78. return login_redirect(request)
  79. else:
  80. error(request, _("The link you clicked is no longer valid."))
  81. return redirect("/")
  82. @login_required
  83. def profile_redirect(request):
  84. """
  85. Just gives the URL prefix for profiles an action - redirect
  86. to the logged in user's profile.
  87. """
  88. return redirect("profile", username=request.user.username)
  89. def profile(request, username, template="accounts/account_profile.html",
  90. extra_context=None):
  91. """
  92. Display a profile.
  93. """
  94. lookup = {"username__iexact": username, "is_active": True}
  95. context = {"profile_user": get_object_or_404(User, **lookup)}
  96. context.update(extra_context or {})
  97. return TemplateResponse(request, template, context)
  98. @login_required
  99. def account_redirect(request):
  100. """
  101. Just gives the URL prefix for accounts an action - redirect
  102. to the profile update form.
  103. """
  104. return redirect("profile_update")
  105. @login_required
  106. def profile_update(request, template="accounts/account_profile_update.html",
  107. extra_context=None):
  108. """
  109. Profile update form.
  110. """
  111. profile_form = get_profile_form()
  112. form = profile_form(request.POST or None, request.FILES or None,
  113. instance=request.user)
  114. if request.method == "POST" and form.is_valid():
  115. user = form.save()
  116. info(request, _("Profile updated"))
  117. try:
  118. return redirect("profile", username=user.username)
  119. except NoReverseMatch:
  120. return redirect("profile_update")
  121. context = {"form": form, "title": _("Update Profile")}
  122. context.update(extra_context or {})
  123. return TemplateResponse(request, template, context)
  124. def password_reset(request, template="accounts/account_password_reset.html",
  125. form_class=PasswordResetForm, extra_context=None):
  126. form = form_class(request.POST or None)
  127. if request.method == "POST" and form.is_valid():
  128. user = form.save()
  129. send_verification_mail(request, user, "password_reset_verify")
  130. info(request, _("A verification email has been sent with "
  131. "a link for resetting your password."))
  132. context = {"form": form, "title": _("Password Reset")}
  133. context.update(extra_context or {})
  134. return TemplateResponse(request, template, context)
  135. def password_reset_verify(request, uidb36=None, token=None):
  136. user = authenticate(uidb36=uidb36, token=token, is_active=True)
  137. if user is not None:
  138. auth_login(request, user)
  139. return redirect("profile_update")
  140. else:
  141. error(request, _("The link you clicked is no longer valid."))
  142. return redirect("/")