fix: add rattail purge-reports command
This commit is contained in:
parent
d23b539439
commit
5439c7ba3b
3 changed files with 70 additions and 7 deletions
|
|
@ -49,7 +49,7 @@ dependencies = [
|
|||
"texttable",
|
||||
"typer",
|
||||
"typing-extensions",
|
||||
"WuttJamaican>=0.16.1",
|
||||
"WuttJamaican>=0.20.4",
|
||||
"xlrd",
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
################################################################################
|
||||
#
|
||||
# Rattail -- Retail Software Framework
|
||||
# Copyright © 2010-2024 Lance Edgar
|
||||
# Copyright © 2010-2025 Lance Edgar
|
||||
#
|
||||
# This file is part of Rattail.
|
||||
#
|
||||
|
|
@ -48,6 +48,7 @@ from . import postfix
|
|||
from . import problems
|
||||
from . import products
|
||||
from . import projects
|
||||
from . import purging
|
||||
from . import run_n_mail
|
||||
from . import runsql
|
||||
from . import settings
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
################################################################################
|
||||
#
|
||||
# Rattail -- Retail Software Framework
|
||||
# Copyright © 2010-2024 Lance Edgar
|
||||
# Copyright © 2010-2025 Lance Edgar
|
||||
#
|
||||
# This file is part of Rattail.
|
||||
#
|
||||
|
|
@ -29,6 +29,11 @@ import datetime
|
|||
import shutil
|
||||
import logging
|
||||
|
||||
import typer
|
||||
from typing_extensions import Annotated
|
||||
|
||||
from rattail.commands import rattail_typer
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -40,10 +45,6 @@ def run_purge(config, purge_title, purge_title_plural, thing_finder, thing_purge
|
|||
|
||||
log.info("will purge things of type: %s", purge_title)
|
||||
|
||||
if before and before_days:
|
||||
log.warning("specifying both --before and --before-days is "
|
||||
"redundant; --before will take precedence.")
|
||||
|
||||
app = config.get_app()
|
||||
session = app.make_session()
|
||||
|
||||
|
|
@ -84,3 +85,64 @@ def purge_things(config, session, things, purger, cutoff, purge_title_plural,
|
|||
app.progress_loop(purge, things, progress,
|
||||
message=f"Purging {purge_title_plural}")
|
||||
return result.purged
|
||||
|
||||
|
||||
@rattail_typer.command()
|
||||
def purge_reports(
|
||||
ctx: typer.Context,
|
||||
before: Annotated[
|
||||
datetime.datetime,
|
||||
typer.Option(formats=['%Y-%m-%d'],
|
||||
help="Use this date as cutoff, i.e. purge all data "
|
||||
"*before* this date. If not specified, will use "
|
||||
"--before-days to calculate instead.")] = None,
|
||||
before_days: Annotated[
|
||||
int,
|
||||
typer.Option(help="Calculate the cutoff date by subtracting this "
|
||||
"number of days from the current date, i.e. purge all "
|
||||
"data *before* the resulting date. Note that if you "
|
||||
"specify --before then that date will be used instead "
|
||||
"of calculating one from --before-days. If neither is "
|
||||
"specified then --before-days is used, with its default "
|
||||
"value.")] = 90,
|
||||
dry_run: Annotated[
|
||||
bool,
|
||||
typer.Option('--dry-run',
|
||||
help="Go through the full motions and allow logging "
|
||||
"etc. to occur, but rollback (abort) the transaction "
|
||||
"at the end.")] = False,
|
||||
):
|
||||
"""
|
||||
Purge generated reports older than a cutoff
|
||||
"""
|
||||
config = ctx.parent.rattail_config
|
||||
progress = ctx.parent.rattail_progress
|
||||
app = config.get_app()
|
||||
model = app.model
|
||||
|
||||
def finder(session, cutoff, dry_run=False):
|
||||
return session.query(model.ReportOutput)\
|
||||
.filter(model.ReportOutput.created < app.make_utc(cutoff))\
|
||||
.all()
|
||||
|
||||
def purger(session, output, cutoff, dry_run=False):
|
||||
uuid = output.uuid
|
||||
log.debug("purging ReportOutput object %s: %s", uuid, output)
|
||||
session.delete(output)
|
||||
|
||||
# maybe delete associated files
|
||||
if not dry_run:
|
||||
session.flush()
|
||||
key = model.ReportOutput.export_key
|
||||
path = config.export_filepath(key, uuid)
|
||||
if os.path.exists(path):
|
||||
shutil.rmtree(path)
|
||||
|
||||
return True
|
||||
|
||||
run_purge(config, "Generated Report", "Generated Reports",
|
||||
finder, purger,
|
||||
before=before.date() if before else None,
|
||||
before_days=before_days,
|
||||
default_before_days=90,
|
||||
dry_run=dry_run, progress=progress)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue