Changeset d0deae5 in rattail


Ignore:
Timestamp:
07/29/2020 01:22:50 AM (3 years ago)
Author:
Lance Edgar <lance@…>
Branches:
master
Children:
f7aa709
Parents:
e16175f
Message:

Add BatchHandler.delete_extra_data() method

and update related logic to use it etc.

Location:
rattail
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • rattail/batch/handlers.py

    re16175f rd0deae5  
    3131import datetime
    3232import warnings
     33import logging
    3334
    3435from sqlalchemy import orm
     
    4142from rattail.time import localtime, make_utc
    4243from rattail.util import progress_loop, load_object
     44
     45
     46log = logging.getLogger(__name__)
    4347
    4448
     
    185189
    186190    def purge_batches(self, session, before=None, before_days=90,
    187                       delete_all_data=True, progress=None, **kwargs):
     191                      dry_run=False, delete_all_data=None,
     192                      progress=None, **kwargs):
    188193        """
    189194        Purge all batches which were executed prior to a given date.
     
    196201           as the cutoff date if ``before`` is not specified.
    197202
     203        :param dry_run: Flag indicating that this is a "dry run" and all logic
     204           involved should be (made) aware of that fact.
     205
    198206        :param delete_all_data: Flag indicating whether *all* data should be
    199207           deleted for each batch being purged.  This flag is passed along to
    200            :meth:`delete()`; see that for more info.
     208           :meth:`delete()`; see that for more info.  NOTE: This flag should
     209           probably be deprecated, but so far has not been...but ``dry_run``
     210           should be preferred for readability etc.
    201211
    202212        :returns: Integer indicating the number of batches purged.
    203213        """
     214        if delete_all_data and dry_run:
     215            raise ValueError("You can enable (n)either of `dry_run` or "
     216                             "`delete_all_data` but both cannot be True")
     217        delete_all_data = not dry_run
     218
    204219        if not before:
    205220            before = localtime(self.config).date() - datetime.timedelta(days=before_days)
     
    207222            before = localtime(self.config, before)
    208223
     224        log.info("will purge '%s' batches, executed before %s",
     225                 self.batch_key, before.date())
     226
    209227        old_batches = session.query(self.batch_model_class)\
    210228                             .filter(self.batch_model_class.executed < before)\
    211                              .options(orm.joinedload(self.batch_model_class.data_rows))
     229                             .options(orm.joinedload(self.batch_model_class.data_rows))\
     230                             .all()
     231        log.info("found %s batches to purge", len(old_batches))
    212232        result = Object()
    213233        result.purged = 0
     
    224244
    225245        session.flush()
     246        if old_batches:
     247            log.info("%spurged %s '%s' batches",
     248                     "(would have) " if dry_run else "",
     249                     result.purged, self.batch_key)
    226250        return result.purged
    227251
     
    721745        """
    722746        if delete_all_data:
    723             if hasattr(batch, 'delete_data'):
    724                 batch.delete_data(self.config)
     747            self.delete_extra_data(batch, progress=progress)
    725748
    726749        # delete all rows from batch, one by one.  maybe would be nicer if we
     
    745768            # rows being deleted
    746769            del batch.data_rows[:]
     770
     771    def delete_extra_data(self, batch, progress=None, **kwargs):
     772        """
     773        Delete all "extra" data for the batch.  This method should *not* bother
     774        trying to delete the batch itself, or rows thereof.  It typically is
     775        only concerned with deleting extra files on disk, related to the batch.
     776        """
     777        path = self.config.batch_filepath(self.batch_key, batch.uuid)
     778        if os.path.exists(path):
     779            shutil.rmtree(path)
    747780
    748781    def setup_clone(self, oldbatch, progress=None):
  • rattail/commands/batch.py

    re16175f rd0deae5  
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2019 Lance Edgar
     5#  Copyright © 2010-2020 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    224224
    225225        handler = self.get_handler(args)
    226         log.info("purging batches of type: %s", args.batch_type)
    227226        session = self.make_session()
    228227
    229228        kwargs = {
    230             'delete_all_data': not args.dry_run,
     229            'dry_run': args.dry_run,
    231230            'progress': self.progress,
    232231        }
     
    239238
    240239        purged = handler.purge_batches(session, **kwargs)
    241         log.info("%spurged %s batches", "(would have) " if args.dry_run else "", purged)
    242240
    243241        if args.dry_run:
  • rattail/db/model/batch/core.py

    re16175f rd0deae5  
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2018 Lance Edgar
     5#  Copyright © 2010-2020 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    3333import datetime
    3434import shutil
     35import warnings
    3536
    3637import six
     
    258259        Delete the data folder for the batch
    259260        """
     261        warnings.warn("This method has been deprecated; please see/use "
     262                      "BatchHandler.delete_extra_data() instead",
     263                      DeprecationWarning)
     264
    260265        # TODO: should this logic be in the handler instead?
    261266        path = config.batch_filepath(self.batch_key, self.uuid)
Note: See TracChangeset for help on using the changeset viewer.