2017-08-07 18:19:29 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
2014-01-29 00:14:11 -06:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
2022-08-20 17:39:33 -05:00
|
|
|
# Copyright © 2010-2022 Lance Edgar
|
2014-01-29 00:14:11 -06:00
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
2017-07-06 23:47:56 -05:00
|
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
2014-01-29 00:14:11 -06:00
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2017-07-06 23:47:56 -05:00
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
2014-01-29 00:14:11 -06:00
|
|
|
#
|
2017-07-06 23:47:56 -05:00
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Rattail. If not, see <http://www.gnu.org/licenses/>.
|
2014-01-29 00:14:11 -06:00
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
|
|
|
Progress Indicator
|
|
|
|
"""
|
|
|
|
|
2016-10-16 03:30:12 -05:00
|
|
|
from __future__ import unicode_literals, absolute_import
|
2014-11-02 20:48:22 -06:00
|
|
|
|
2017-08-07 18:19:29 -05:00
|
|
|
import os
|
2022-08-20 17:39:33 -05:00
|
|
|
import warnings
|
2017-08-07 18:19:29 -05:00
|
|
|
|
2018-06-07 12:40:25 -05:00
|
|
|
from rattail.progress import ProgressBase
|
|
|
|
|
2014-01-29 00:14:11 -06:00
|
|
|
from beaker.session import Session
|
|
|
|
|
|
|
|
|
2022-08-20 17:39:33 -05:00
|
|
|
def get_basic_session(config, request={}, **kwargs):
|
|
|
|
"""
|
|
|
|
Create/get a "basic" Beaker session object.
|
|
|
|
"""
|
|
|
|
kwargs['use_cookies'] = False
|
|
|
|
session = Session(request, **kwargs)
|
|
|
|
return session
|
|
|
|
|
|
|
|
|
2017-08-07 18:19:29 -05:00
|
|
|
def get_progress_session(request, key, **kwargs):
|
2014-11-02 20:48:22 -06:00
|
|
|
"""
|
|
|
|
Create/get a Beaker session object, to be used for progress.
|
|
|
|
"""
|
2022-08-20 17:39:33 -05:00
|
|
|
kwargs['id'] = '{}.progress.{}'.format(request.session.id, key)
|
2017-08-07 18:19:29 -05:00
|
|
|
if kwargs.get('type') == 'file':
|
2022-08-20 17:39:33 -05:00
|
|
|
warnings.warn("Passing a 'type' kwarg to get_progress_session() "
|
|
|
|
"is deprecated...i think",
|
|
|
|
DeprecationWarning, stacklevel=2)
|
2017-08-07 18:19:29 -05:00
|
|
|
kwargs['data_dir'] = os.path.join(request.rattail_config.appdir(), 'sessions')
|
2022-08-20 17:39:33 -05:00
|
|
|
return get_basic_session(request.rattail_config, request, **kwargs)
|
2014-01-29 00:14:11 -06:00
|
|
|
|
|
|
|
|
2018-06-07 12:40:25 -05:00
|
|
|
class SessionProgress(ProgressBase):
|
2014-01-29 00:14:11 -06:00
|
|
|
"""
|
|
|
|
Provides a session-based progress bar mechanism.
|
|
|
|
|
|
|
|
This class is only responsible for keeping the progress *data* current. It
|
|
|
|
is the responsibility of some client-side AJAX (etc.) to consume the data
|
|
|
|
for display to the user.
|
2022-08-20 17:39:33 -05:00
|
|
|
|
|
|
|
:param ws: If true, then websockets are assumed, and the progress will
|
|
|
|
behave accordingly. The default is false, "traditional" behavior.
|
2014-01-29 00:14:11 -06:00
|
|
|
"""
|
|
|
|
|
2022-08-20 17:39:33 -05:00
|
|
|
def __init__(self, request, key, session_type=None, ws=False):
|
2017-08-07 18:19:29 -05:00
|
|
|
self.key = key
|
2022-08-20 17:39:33 -05:00
|
|
|
self.ws = ws
|
|
|
|
|
|
|
|
if self.ws:
|
|
|
|
self.session = get_basic_session(request.rattail_config, id=key)
|
|
|
|
else:
|
|
|
|
self.session = get_progress_session(request, key, type=session_type)
|
|
|
|
|
2014-01-29 00:14:11 -06:00
|
|
|
self.canceled = False
|
|
|
|
self.clear()
|
|
|
|
|
|
|
|
def __call__(self, message, maximum):
|
|
|
|
self.clear()
|
|
|
|
self.session['message'] = message
|
|
|
|
self.session['maximum'] = maximum
|
2016-10-16 03:30:12 -05:00
|
|
|
self.session['maximum_display'] = '{:,d}'.format(maximum)
|
2014-01-29 00:14:11 -06:00
|
|
|
self.session['value'] = 0
|
|
|
|
self.session.save()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self.session.clear()
|
|
|
|
self.session['complete'] = False
|
|
|
|
self.session['error'] = False
|
|
|
|
self.session['canceled'] = False
|
|
|
|
self.session.save()
|
|
|
|
|
|
|
|
def update(self, value):
|
|
|
|
self.session.load()
|
|
|
|
if self.session.get('canceled'):
|
|
|
|
self.canceled = True
|
|
|
|
else:
|
|
|
|
self.session['value'] = value
|
|
|
|
self.session.save()
|
|
|
|
return not self.canceled
|