Add SessionProgress class.

This commit is contained in:
Lance Edgar 2014-01-28 22:14:11 -08:00
parent a6226700f1
commit c6d01e0205
3 changed files with 80 additions and 2 deletions

78
tailbone/progress.py Normal file
View file

@ -0,0 +1,78 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2012 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Progress Indicator
"""
from beaker.session import Session
def get_progress_session(session, key):
request = session.request
id = '%s.progress.%s' % (session.id, key)
session = Session(request, id)
return session
class SessionProgress(object):
"""
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.
"""
def __init__(self, session, key):
self.session = get_progress_session(session, key)
self.canceled = False
self.clear()
def __call__(self, message, maximum):
self.clear()
self.session['message'] = message
self.session['maximum'] = maximum
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
def destroy(self):
pass

View file

@ -35,8 +35,8 @@ import edbob
from edbob.pyramid.forms import PrettyDateTimeFieldRenderer from edbob.pyramid.forms import PrettyDateTimeFieldRenderer
from ...forms import EnumFieldRenderer from ...forms import EnumFieldRenderer
from ...grids.search import BooleanSearchFilter from ...grids.search import BooleanSearchFilter
from edbob.pyramid.progress import SessionProgress
from .. import SearchableAlchemyGridView, CrudView, View from .. import SearchableAlchemyGridView, CrudView, View
from ...progress import SessionProgress
from rattail import enum from rattail import enum
from rattail import batches from rattail import batches

View file

@ -35,7 +35,6 @@ from pyramid.httpexceptions import HTTPFound
from pyramid.renderers import render_to_response from pyramid.renderers import render_to_response
import edbob import edbob
from edbob.pyramid.progress import SessionProgress
from . import SearchableAlchemyGridView from . import SearchableAlchemyGridView
import rattail.labels import rattail.labels
@ -52,6 +51,7 @@ from rattail.db.api import get_product_by_upc
from ..db import Session from ..db import Session
from ..forms import GPCFieldRenderer, BrandFieldRenderer, PriceFieldRenderer from ..forms import GPCFieldRenderer, BrandFieldRenderer, PriceFieldRenderer
from . import CrudView from . import CrudView
from ..progress import SessionProgress
class ProductsGrid(SearchableAlchemyGridView): class ProductsGrid(SearchableAlchemyGridView):