From c6d01e02053b2069a00e1f7d075590b6c0a83261 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Tue, 28 Jan 2014 22:14:11 -0800 Subject: [PATCH] Add `SessionProgress` class. --- tailbone/progress.py | 78 ++++++++++++++++++++++++++++++++++ tailbone/views/batches/core.py | 2 +- tailbone/views/products.py | 2 +- 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 tailbone/progress.py diff --git a/tailbone/progress.py b/tailbone/progress.py new file mode 100644 index 00000000..04eda280 --- /dev/null +++ b/tailbone/progress.py @@ -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 . +# +################################################################################ + +""" +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 diff --git a/tailbone/views/batches/core.py b/tailbone/views/batches/core.py index b54256b1..67716106 100644 --- a/tailbone/views/batches/core.py +++ b/tailbone/views/batches/core.py @@ -35,8 +35,8 @@ import edbob from edbob.pyramid.forms import PrettyDateTimeFieldRenderer from ...forms import EnumFieldRenderer from ...grids.search import BooleanSearchFilter -from edbob.pyramid.progress import SessionProgress from .. import SearchableAlchemyGridView, CrudView, View +from ...progress import SessionProgress from rattail import enum from rattail import batches diff --git a/tailbone/views/products.py b/tailbone/views/products.py index de3f6e40..5f5804ac 100644 --- a/tailbone/views/products.py +++ b/tailbone/views/products.py @@ -35,7 +35,6 @@ from pyramid.httpexceptions import HTTPFound from pyramid.renderers import render_to_response import edbob -from edbob.pyramid.progress import SessionProgress from . import SearchableAlchemyGridView import rattail.labels @@ -52,6 +51,7 @@ from rattail.db.api import get_product_by_upc from ..db import Session from ..forms import GPCFieldRenderer, BrandFieldRenderer, PriceFieldRenderer from . import CrudView +from ..progress import SessionProgress class ProductsGrid(SearchableAlchemyGridView):