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
import logging
from .models import get_userprofile
from themes import Context
import users
from users import parameter
try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
def password_recovery(request):
messages.error(request, "Password recovery is not yet implemented!")
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
if parameter.get(parameter.USERS_SELF_REGISTRATION):
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)
else:
messages.info(request, _("Self registration is deactivated. Contact your system administrator."))
return redirect('users-login')
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()
if parameter.get(parameter.USERS_SELF_REGISTRATION):
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:
if parameter.get(parameter.USERS_SELF_REGISTRATION):
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)})
else:
messages.error(request, _('Login failed! You can do a password recorvery here.') %
{'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 '/')