Source code for election_day.views.auth

""" The authentication views. """

from import Private
from import Public
from onegov.core.templates import render_template
from onegov.election_day import _
from onegov.election_day import ElectionDayApp
from onegov.election_day import log
from onegov.election_day.layouts import DefaultLayout
from onegov.election_day.layouts import MailLayout
from onegov.election_day.models import Principal
from onegov.user import Auth
from onegov.user import UserCollection
from onegov.user.forms import LoginForm
from onegov.user.forms import PasswordResetForm
from onegov.user.forms import RequestPasswordResetForm
from onegov.user.utils import password_reset_url

from typing import TYPE_CHECKING
    from onegov.core.types import RenderData
    from onegov.election_day.request import ElectionDayRequest
    from webob.response import Response

[docs] def handle_login( self: Auth, request: 'ElectionDayRequest', form: LoginForm ) -> 'RenderData | Response': """ Handles the login requests. """ if form.submitted(request): response = self.login_to(request=request, **form.login_data) form.error_message = _("Wrong username or password") # type:ignore else: response = None return response or { 'layout': DefaultLayout(self, request), 'title': _("Login"), 'form': form, 'password_reset_link':, name='request-password' ), }
@ElectionDayApp.html( model=Auth, name='logout', permission=Private )
[docs] def view_logout(self: Auth, request: 'ElectionDayRequest') -> 'Response': """ Handles the logout requests. """ return self.logout_to(request)
@ElectionDayApp.form( model=Principal, name='request-password', template='', form=RequestPasswordResetForm, permission=Public )
[docs] def handle_password_reset_request( self: Principal, request: 'ElectionDayRequest', form: PasswordResetForm ) -> 'RenderData': """ Handles the password reset requests. """ show_form = True callout = None if form.submitted(request): assert is not None users = UserCollection(request.session) user = users.by_username( if user: url = password_reset_url( user, request,, name='reset-password') ) assert is not None subject=request.translate(_("Password reset")), receivers=(user.username, ), reply_to=( or['transactional']['sender'] ), content=render_template( '', request, { 'title': request.translate(_("Password reset")), 'model': None, 'url': url, 'layout': MailLayout(self, request) } ) ) else: "Failed password reset attempt by {}".format( request.client_addr ) ) show_form = False callout = _( ( 'A password reset link has been sent to ${email}, provided an ' 'account exists for this email address.' ), mapping={'email':} ) return { 'layout': DefaultLayout(self, request), 'title': _('Reset password'), 'form': form, 'show_form': show_form, 'callout': callout }
@ElectionDayApp.form( model=Principal, name='reset-password', template='', form=PasswordResetForm, permission=Public )
[docs] def handle_password_reset( self: Principal, request: 'ElectionDayRequest', form: PasswordResetForm ) -> 'RenderData': """ Handles password reset requests. """ callout = None show_form = True if form.submitted(request): if form.update_password(request): show_form = False callout = _("Password changed.") else: form.error_message = _( # type:ignore[attr-defined] "Wrong username or password reset link not valid any more." ) "Failed password reset attempt by {}".format( request.client_addr ) ) token = request.params.get('token') if isinstance(token, str): = token return { 'layout': DefaultLayout(self, request), 'title': _('Reset password'), 'form': form, 'show_form': show_form, 'callout': callout }