Django Library Users
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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