Django Library Users
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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from .context import context_adaption
  2. from django.shortcuts import render, redirect
  3. from django.conf import settings
  4. from django.contrib import messages
  5. from django.contrib.auth import authenticate
  6. from django.contrib.auth import login as django_login
  7. from django.contrib.auth import logout as django_logout
  8. from django.contrib.auth.decorators import login_required
  9. from django.contrib.auth.forms import AuthenticationForm
  10. from django.utils.translation import gettext as _
  11. from .forms import UserRegistrationForm, UserProfileForm
  12. import logging
  13. from .models import get_userprofile
  14. from themes import Context
  15. import users
  16. from users import parameter
  17. # TODO: Implement password change.
  18. try:
  19. from config import APP_NAME as ROOT_LOGGER_NAME
  20. except ImportError:
  21. ROOT_LOGGER_NAME = 'root'
  22. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  23. def password_recovery(request):
  24. # TODO: Implement password recovery
  25. messages.error(request, "Password recovery is not yet implemented!")
  26. return redirect(request.GET.get('next') or '/')
  27. def profile_post_actions(request, context):
  28. if request.POST:
  29. form = context.get('form_userprofile')
  30. if form.is_valid():
  31. form.save()
  32. return redirect(request.GET.get('next') or '/')
  33. def profile_pre_actions(request, context, form_to_be_used=UserProfileForm):
  34. profile = get_userprofile(request.user)
  35. if request.POST:
  36. form = form_to_be_used(request.POST, instance=profile)
  37. else:
  38. form = form_to_be_used(instance=profile)
  39. context['form_userprofile'] = form
  40. @login_required
  41. def profile(request):
  42. context = Context(request) # needs to be executed first because of time mesurement
  43. profile_pre_actions(request, context)
  44. response = profile_post_actions(request, context)
  45. if response is not None:
  46. return response
  47. else:
  48. context_adaption(
  49. context,
  50. request,
  51. _('Profile for %(username)s') % {'username': request.user.username},
  52. )
  53. return render(request, 'users/profile.html', context=context)
  54. def register(request):
  55. context = Context(request) # needs to be executed first because of time mesurement
  56. if parameter.get(parameter.USERS_SELF_REGISTRATION):
  57. context_adaption(context, request, _('Register'))
  58. if not request.POST:
  59. form = UserRegistrationForm()
  60. messages.info(request, _('If you already have an account, login <a href="%(url)s">here</a>.') % {'url': users.url_login(request)})
  61. else:
  62. form = UserRegistrationForm(request.POST)
  63. if form.is_valid():
  64. form.save()
  65. messages.success(request, _('Your account has been created! You are able to log in as %(username)s.') %
  66. {'username': form.cleaned_data.get('username')})
  67. return redirect('users-login')
  68. else:
  69. messages.error(request, _('Registration failed!'))
  70. context['form'] = form
  71. return render(request, 'users/register.html', context)
  72. else:
  73. messages.info(request, _("Self registration is deactivated. Contact your system administrator."))
  74. return redirect('users-login')
  75. def login(request):
  76. context = Context(request) # needs to be executed first because of time mesurement
  77. context_adaption(context, request, _('Login'))
  78. if not request.POST:
  79. form = AuthenticationForm()
  80. if parameter.get(parameter.USERS_SELF_REGISTRATION):
  81. messages.info(request, _('If you don\'t have an acount, register <a href="%(url)s">here</a>.') % {'url': users.url_register(request)})
  82. else:
  83. form = AuthenticationForm(request, data=request.POST)
  84. if form.is_valid():
  85. username = form.cleaned_data.get('username')
  86. user = authenticate(username=username, password=form.cleaned_data.get('password'))
  87. django_login(request, user)
  88. messages.success(request, _('You are now logged in as %(username)s.') % {'username': username})
  89. return redirect(request.GET.get('next') or '/')
  90. else:
  91. if parameter.get(parameter.USERS_SELF_REGISTRATION):
  92. messages.error(request, _('Login failed! You can do a password recorvery <a href="%(url_recover)s">here</a> or you can register <a href="%(url_register)s">here</a>.') %
  93. {'url_register': users.url_register(request), 'url_recover': users.url_password_recovery(request)})
  94. else:
  95. messages.error(request, _('Login failed! You can do a password recorvery <a href="%(url_recover)s">here</a>.') %
  96. {'url_recover': users.url_password_recovery(request)})
  97. context['form'] = form
  98. return render(request, 'users/login.html', context)
  99. def logout(request):
  100. messages.success(request, _('You are no longer logged in as %(username)s.') % {'username': request.user.username})
  101. session_cache = {}
  102. try:
  103. for variable in settings.PERSISTENT_SESSION_VARIABLES:
  104. value = request.session.get(variable)
  105. if value is not None:
  106. session_cache[variable] = value
  107. except AttributeError:
  108. pass # PERSISTENT_SESSION_VARIABLES are possibly not defined in the settings
  109. django_logout(request)
  110. for variable in session_cache:
  111. request.session[variable] = session_cache[variable]
  112. return redirect(request.GET.get('next') or '/')