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.3KB

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