Exemple #1
0
def test_default_layout(wtfs_app):
    request = DummyRequest()
    request.app = wtfs_app
    request.session = wtfs_app.session()
    model = None

    layout = DefaultLayout(model, request)
    assert layout.title == ""
    assert layout.editbar_links == []
    assert path(layout.breadcrumbs) == 'Principal'
    assert layout.static_path == 'Principal/static'
    assert layout.app_version
    assert layout.request.includes == ['frameworks', 'chosen', 'common']
    assert layout.top_navigation == []
    assert layout.cancel_url == ''
    assert layout.success_url == ''
    assert layout.homepage_url == 'Principal/'
    assert layout.login_url == 'Auth/login'
    assert layout.login_to_url(layout.logout_url) == 'Auth/login'
    assert layout.logout_url is None
    assert layout.users_url == 'UserCollection/'
    assert layout.municipalities_url == 'MunicipalityCollection/'
    assert layout.scan_jobs_url == 'ScanJobCollection/'
    assert layout.payment_types_url == 'PaymentTypeCollection/'
    assert layout.current_year == date.today().year

    # Login
    request.is_logged_in = True
    request.roles = ['admin']
    layout = DefaultLayout(model, request)
    assert layout.login_url is None
    assert layout.logout_url == 'Auth/logout'
    assert list(hrefs(layout.top_navigation)) == [
        'ScanJobCollection/',
        'DailyList/',
        'Report/',
        'Invoice/',
        'UserCollection/',
        'MunicipalityCollection/',
        'NotificationCollection/',
        'UserManual/'
    ]
Exemple #2
0
def handle_notfound(self, request):
    """ Displays a nice HTTP 404 error. """
    @request.after
    def set_status_code(response):
        response.status_code = self.code

    return {
        'layout': DefaultLayout(self, request),
        'title': _("Page not Found"),
        'message': _("The page you are looking for could not be found."),
    }
Exemple #3
0
def view_home(self, request):
    """ The home page. """

    layout = DefaultLayout(self, request)

    if not request.is_logged_in:
        return request.redirect(layout.login_url)

    if not layout.notifications.query().first():
        return request.redirect(layout.top_navigation[0].attrs['href'])

    return {'layout': layout}
Exemple #4
0
def handle_forbidden(self, request):
    """ Displays a nice HTTP 403 error. """
    @request.after
    def set_status_code(response):
        response.status_code = self.code

    return {
        'layout':
        DefaultLayout(self, request),
        'title':
        _("Access Denied"),
        'message':
        _("You are trying to open a page for which you are not authorized.")
    }
Exemple #5
0
def view_dispatch_dates(self, request, form):
    """ Show dispatches dates for a given municipality. """

    if form.submitted(request):
        layout = DefaultLayout(self, request)
        dates = [
            r.date for r in form.municipality.pickup_dates.filter(
                PickupDate.date > date.today())
        ]
        return render_macro(layout.macros['dispatch_dates'], request, {
            'dates': dates,
            'layout': layout
        })

    return Response()
Exemple #6
0
def handle_login(self, request, form):
    """ Handles the login requests. """

    form.delete_field('yubikey')

    if form.submitted(request):
        response = self.login_to(request=request, **form.login_data)
        form.error_message = _("Wrong username or password")
    else:
        response = None

    return response or {
        'layout':
        DefaultLayout(self, request),
        'title':
        _("Login"),
        'form':
        form,
        'password_reset_link':
        request.link(Auth.from_request(request), name='request-password'),
    }
Exemple #7
0
def handle_password_reset_request(self, request, form):
    """ Handles the password reset requests. """

    layout = DefaultLayout(self, request)

    if form.submitted(request):
        users = UserCollection(request.session)
        user = users.by_username(form.email.data)
        if user:
            url = password_reset_url(user, request,
                                     request.link(self, name='reset-password'))

            request.app.send_transactional_email(
                subject=request.translate(_("Password reset")),
                receivers=(user.username, ),
                reply_to=request.app.mail['transactional']['sender'],
                content=render_template(
                    'mail_password_reset.pt', request, {
                        'title': request.translate(_("Password reset")),
                        'model': None,
                        'url': url,
                        'layout': MailLayout(self, request)
                    }))
        else:
            log.info("Failed password reset attempt by {}".format(
                request.client_addr))

        message = _(
            'A password reset link has been sent to ${email}, provided an '
            'account exists for this email address.',
            mapping={'email': form.email.data})
        request.message(message, 'success')
        return request.redirect(layout.login_url)

    return {
        'layout': layout,
        'title': _('Reset password'),
        'form': form,
    }
Exemple #8
0
def handle_password_reset(self, request, form):
    """ Handles password reset requests. """

    layout = DefaultLayout(self, request)

    if form.submitted(request):
        if form.update_password(request):
            request.message(_("Password changed."), 'success')
            return request.redirect(layout.login_url)
        else:
            form.error_message = _(
                "Wrong username or password reset link not valid any more.")
            log.info("Failed password reset attempt by {}".format(
                request.client_addr))

    if 'token' in request.params:
        form.token.data = request.params['token']

    return {
        'layout': layout,
        'title': _('Reset password'),
        'form': form,
    }