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

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