from .context import context_adaption from django.shortcuts import render, redirect from django.conf import settings from django.contrib import messages from django.contrib.auth import authenticate from django.contrib.auth import login as django_login from django.contrib.auth import logout as django_logout from django.contrib.auth.decorators import login_required from django.contrib.auth.forms import AuthenticationForm from django.utils.translation import gettext as _ from .forms import UserRegistrationForm, UserProfileForm from .models import get_userprofile from themes import Context import users def password_recovery(request): return redirect(request.GET.get('next') or '/') def profile_post_actions(request, context): if request.POST: form = context.get('form_userprofile') if form.is_valid(): form.save() return redirect(request.GET.get('next') or '/') def profile_pre_actions(request, context, form_to_be_used=UserProfileForm): profile = get_userprofile(request.user) if request.POST: form = form_to_be_used(request.POST, instance=profile) else: form = form_to_be_used(instance=profile) context['form_userprofile'] = form @login_required def profile(request): context = Context(request) # needs to be executed first because of time mesurement profile_pre_actions(request, context) response = profile_post_actions(request, context) if response is not None: return response else: context_adaption( context, request, _('Profile for %(username)s') % {'username': request.user.username}, ) return render(request, 'users/profile.html', context=context) def register(request): context = Context(request) # needs to be executed first because of time mesurement context_adaption(context, request, _('Register')) if not request.POST: form = UserRegistrationForm() messages.info(request, _('If you already have an account, login here.') % {'url': users.url_login(request)}) else: form = UserRegistrationForm(request.POST) if form.is_valid(): form.save() messages.success(request, _('Your account has been created! You are able to log in as %(username)s.') % {'username': form.cleaned_data.get('username')}) return redirect('users-login') else: messages.error(request, _('Registration failed!')) context['form'] = form return render(request, 'users/register.html', context) def login(request): context = Context(request) # needs to be executed first because of time mesurement context_adaption(context, request, _('Login')) if not request.POST: form = AuthenticationForm() messages.info(request, _('If you don\'t have an acount, register here.') % {'url': users.url_register(request)}) else: form = AuthenticationForm(request, data=request.POST) if form.is_valid(): username = form.cleaned_data.get('username') user = authenticate(username=username, password=form.cleaned_data.get('password')) django_login(request, user) messages.success(request, _('You are now logged in as %(username)s.') % {'username': username}) return redirect(request.GET.get('next') or '/') else: messages.error(request, _('Login failed! You can do a password recorvery here or you can register here.') % {'url_register': users.url_register(request), 'url_recover': users.url_password_recovery(request)}) context['form'] = form return render(request, 'users/login.html', context) def logout(request): messages.success(request, _('You are no longer logged in as %(username)s.') % {'username': request.user.username}) session_cache = {} try: for variable in settings.PERSISTENT_SESSION_VARIABLES: value = request.session.get(variable) if value is not None: session_cache[variable] = value except AttributeError: pass # PERSISTENT_SESSION_VARIABLES are possibly not defined in the settings django_logout(request) for variable in session_cache: request.session[variable] = session_cache[variable] return redirect(request.GET.get('next') or '/')