Coverage for .tox / coverage / lib / python3.11 / site-packages / wuttaweb / views / progress.py: 100%
19 statements
« prev ^ index » next coverage.py v7.13.1, created at 2025-12-28 15:23 -0600
« prev ^ index » next coverage.py v7.13.1, created at 2025-12-28 15:23 -0600
1# -*- coding: utf-8; -*-
2################################################################################
3#
4# wuttaweb -- Web App for Wutta Framework
5# Copyright © 2024-2025 Lance Edgar
6#
7# This file is part of Wutta Framework.
8#
9# Wutta Framework is free software: you can redistribute it and/or modify it
10# under the terms of the GNU General Public License as published by the Free
11# Software Foundation, either version 3 of the License, or (at your option) any
12# later version.
13#
14# Wutta Framework is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17# more details.
18#
19# You should have received a copy of the GNU General Public License along with
20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>.
21#
22################################################################################
23"""
24Progress Views
25"""
27from wuttaweb.progress import get_progress_session
30def progress(request):
31 """
32 View which returns JSON with current progress status.
34 The URL is like ``/progress/XXX`` where ``XXX`` is the "key" to a
35 particular progress indicator, tied to a long-running operation.
37 This key is used to lookup the progress status within the Beaker
38 session storage. See also
39 :class:`~wuttaweb.progress.SessionProgress`.
40 """
41 key = request.matchdict["key"]
42 session = get_progress_session(request, key)
44 # session has 'complete' flag set when operation is over
45 if session.get("complete"):
47 # set a flash msg for user if one is defined. this is the
48 # time to do it since user is about to get redirected.
49 msg = session.get("success_msg")
50 if msg:
51 request.session.flash(msg)
53 elif session.get("error"): # uh-oh
55 # set an error flash msg for user. this is the time to do it
56 # since user is about to get redirected.
57 msg = session.get("error_msg", "An unspecified error occurred.")
58 request.session.flash(msg, "error")
60 # nb. we return the session as-is; since it is dict-like (and only
61 # contains relevant progress data) it can be used directly for the
62 # JSON response context
63 return session
66def defaults(config, **kwargs): # pylint: disable=missing-function-docstring
67 base = globals()
69 progress = kwargs.get( # pylint: disable=redefined-outer-name
70 "progress", base["progress"]
71 )
72 config.add_route("progress", "/progress/{key}")
73 config.add_view(progress, route_name="progress", renderer="json")
76def includeme(config): # pylint: disable=missing-function-docstring
77 defaults(config)