Changeset - fef61b5aa32e
[Not reviewed]
0 2 0
Lance Edgar (lance) - 7 years ago 2017-11-11 09:50:44
lance@edbob.org
Add "re-populate on refresh" flag for batch handlers

to handle the common case where a batch refresh should really be a rebuild,
as opposed to refreshing existing rows in-place
2 files changed with 17 insertions and 9 deletions:
0 comments (0 inline, 0 general)
rattail/batch/handlers.py
Show inline comments
 
@@ -48,14 +48,22 @@ class BatchHandler(object):
 

	
 
       Simple flag to indicate whether any/all batches being handled, will
 
       require initial population from a relevant data source.  Note that this
 
       flag should be set to ``True`` if *any* batches may need population.
 
       Whether or not a given batch actually needs to be populated, is
 
       ultimately determined by the :meth:`should_populate()` method.
 

	
 
    .. attribute:: repopulate_when_refresh
 

	
 
       Flag to indicate that when a batch is refreshed, the first step of that
 
       should be to re-populate the batch.  The flag is ``False`` by default,
 
       in which case the batch is *not* repopulated, i.e. the refresh will work
 
       with existing batch rows.
 
    """
 
    populate_batches = False
 
    repopulate_when_refresh = False
 

	
 
    def __init__(self, config):
 
        self.config = config
 
        self.enum = config.get_enum()
 

	
 
    @property
 
@@ -245,25 +253,31 @@ class BatchHandler(object):
 
        certain data is expected to be "core" to the batch and/or rows, and
 
        such data will be left intact, with all *other* data values being
 
        re-calculated and/or reset etc.
 
        """
 
        session = orm.object_session(batch)
 
        self.setup_refresh(batch, progress=progress)
 
        if self.repopulate_when_refresh:
 
            del batch.data_rows[:]
 
            batch.rowcount = 0
 
            session.flush()
 
            self.populate(batch, progress=progress)
 
        else:
 
            batch.rowcount = 0
 

	
 
            def refresh(row, i):
 
                with session.no_autoflush:
 
                    self.refresh_row(row)
 
                if not row.removed:
 
                    batch.rowcount += 1
 

	
 
        success = self.progress_loop(refresh, batch.active_rows(), progress,
 
            self.progress_loop(refresh, batch.active_rows(), progress,
 
                               message="Refreshing batch data rows")
 
        self.refresh_batch_status(batch)
 
        self.teardown_refresh(batch, progress=progress)
 
        return success
 
        return True
 

	
 
    def refresh_row(self, row):
 
        """
 
        This method will be passed a row object which has already been properly
 
        added to a batch, and which has basic required fields already
 
        populated.  This method is then responsible for further populating all
rattail/data/new-batch/handler.py
Show inline comments
 
@@ -13,19 +13,13 @@ from rattail.batch import BatchHandler
 
class ${model_name}Handler(BatchHandler):
 
    """
 
    Handler for ${model_title} batches.
 
    """
 
    batch_model_class = model.${model_name}
 

	
 
    def refresh_data(self, session, batch, progress=None):
 
        """
 
        Refresh all data for the batch.
 
        """
 
        super(${model_name}Handler, self).refresh_data(session, batch, progress=progress)
 

	
 
    def cognize_row(self, session, row):
 
    def refresh_row(self, row):
 
        """
 
        Inspect a single row from the batch, and set its attributes based on
 
        various lookups and business rules, as needed.
 
        """
 

	
 
    def execute(self, batch, **kwargs):
0 comments (0 inline, 0 general)