From 3e37ac909e1fd2c325a4ce335c1260dc6d92237a Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sat, 15 Aug 2015 17:00:01 -0500 Subject: [PATCH] Various tweaks to support "late login" idea when uploading new batch. --- tailbone/views/__init__.py | 2 +- tailbone/views/batch.py | 33 ++++++++++++++++++++------------- tailbone/views/core.py | 25 ++++++++++++++++++------- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/tailbone/views/__init__.py b/tailbone/views/__init__.py index 28b9ab57..4c48fa25 100644 --- a/tailbone/views/__init__.py +++ b/tailbone/views/__init__.py @@ -25,7 +25,7 @@ Pyramid Views """ -from tailbone.views.core import View +from .core import View from tailbone.views.grids import ( GridView, AlchemyGridView, SortableAlchemyGridView, PagedAlchemyGridView, SearchableAlchemyGridView) diff --git a/tailbone/views/batch.py b/tailbone/views/batch.py index 30013ce5..64c0a7d5 100644 --- a/tailbone/views/batch.py +++ b/tailbone/views/batch.py @@ -530,15 +530,18 @@ class BatchCrud(BaseCrud): } return render_to_response('/progress.mako', kwargs, request=self.request) - def refresh_data(self, session, batch, progress=None): + def refresh_data(self, session, batch, cognizer=None, progress=None): """ Instruct the batch handler to refresh all data for the batch. """ self.handler.refresh_data(session, batch, progress=progress) batch.cognized = datetime.datetime.utcnow() - batch.cognized_by = self.request.user + if cognizer is not None: + batch.cognized_by = cognizer + else: + batch.cognized_by = self.request.user - def refresh_thread(self, batch_uuid, progress): + def refresh_thread(self, batch_uuid, cognizer_uuid=None, success_url=None, progress=None): """ Thread target for refreshing batch data with progress indicator. """ @@ -547,16 +550,18 @@ class BatchCrud(BaseCrud): # transaction binding etc. session = RatSession() batch = session.query(self.batch_class).get(batch_uuid) + cognizer = session.query(model.User).get(cognizer_uuid) if cognizer_uuid else None try: - self.refresh_data(session, batch, progress=progress) + self.refresh_data(session, batch, cognizer=cognizer, progress=progress) except Exception as error: session.rollback() log.warning("refreshing data for batch failed: {0}".format(batch), exc_info=True) session.close() - progress.session.load() - progress.session['error'] = True - progress.session['error_msg'] = "Data refresh failed: {0}".format(error) - progress.session.save() + if progress: + progress.session.load() + progress.session['error'] = True + progress.session['error_msg'] = "Data refresh failed: {0}".format(error) + progress.session.save() return session.commit() @@ -564,10 +569,11 @@ class BatchCrud(BaseCrud): session.close() # Finalize progress indicator. - progress.session.load() - progress.session['complete'] = True - progress.session['success_url'] = self.view_url(batch.uuid) - progress.session.save() + if progress: + progress.session.load() + progress.session['complete'] = True + progress.session['success_url'] = success_url or self.view_url(batch.uuid) + progress.session.save() def view_url(self, uuid=None): """ @@ -692,7 +698,8 @@ class FileBatchCrud(BatchCrud): # For new batches, assign current user as creator, save file etc. if self.creating: - batch.created_by = self.request.user + with Session.no_autoflush: + batch.created_by = self.request.user or self.latelogin_user() # Expunge batch from session to prevent it from being flushed # during init. This is done as a convenience to views which diff --git a/tailbone/views/core.py b/tailbone/views/core.py index 3ea681a3..c27a0141 100644 --- a/tailbone/views/core.py +++ b/tailbone/views/core.py @@ -1,9 +1,8 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- ################################################################################ # # Rattail -- Retail Software Framework -# Copyright © 2010-2012 Lance Edgar +# Copyright © 2010-2015 Lance Edgar # # This file is part of Rattail. # @@ -21,18 +20,20 @@ # along with Rattail. If not, see . # ################################################################################ - """ -Core View +Base View Class """ +from __future__ import unicode_literals -__all__ = ['View'] +from rattail.db import model + +from tailbone.db import Session class View(object): """ - Base for all class-based views. + Base class for all class-based views. """ def __init__(self, request): @@ -45,6 +46,16 @@ class View(object): """ return self.request.rattail_config + def late_login_user(self): + """ + Returns the :class:`rattail:rattail.db.model.User` instance + corresponding to the "late login" form data (if any), or ``None``. + """ + if self.request.method == 'POST': + uuid = self.request.POST.get('late-login-user') + if uuid: + return Session.query(model.User).get(uuid) + def fake_error(request): """