Coverage for .tox/coverage/lib/python3.11/site-packages/wuttaweb/views/progress.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-26 14:40 -0500

1# -*- coding: utf-8; -*- 

2################################################################################ 

3# 

4# wuttaweb -- Web App for Wutta Framework 

5# Copyright © 2024 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""" 

26 

27from wuttaweb.progress import get_progress_session 

28 

29 

30def progress(request): 

31 """ 

32 View which returns JSON with current progress status. 

33 

34 The URL is like ``/progress/XXX`` where ``XXX`` is the "key" to a 

35 particular progress indicator, tied to a long-running operation. 

36 

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) 

43 

44 # session has 'complete' flag set when operation is over 

45 if session.get('complete'): 

46 

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) 

52 

53 elif session.get('error'): # uh-oh 

54 

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') 

59 

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 

64 

65 

66def defaults(config, **kwargs): 

67 base = globals() 

68 

69 progress = kwargs.get('progress', base['progress']) 

70 config.add_route('progress', '/progress/{key}') 

71 config.add_view(progress, route_name='progress', renderer='json') 

72 

73 

74def includeme(config): 

75 defaults(config)