fix: add command to purge shopfoo exports
This commit is contained in:
parent
cc9ec47857
commit
5e466be42e
|
@ -3,13 +3,22 @@
|
||||||
Rattail Demo Commands
|
Rattail Demo Commands
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
import typer
|
import typer
|
||||||
|
from typing_extensions import Annotated
|
||||||
|
|
||||||
from rattail.commands.typer import (typer_callback, typer_get_runas_user,
|
from rattail.commands.typer import (typer_callback, typer_get_runas_user,
|
||||||
importer_command, file_exporter_command)
|
importer_command, file_exporter_command)
|
||||||
from rattail.commands.importing import ImportCommandHandler
|
from rattail.commands.importing import ImportCommandHandler
|
||||||
|
from rattail.commands.purging import run_purge
|
||||||
|
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
# nb. this is the top-level command for titeship
|
# nb. this is the top-level command for titeship
|
||||||
rattail_demo_typer = typer.Typer(
|
rattail_demo_typer = typer.Typer(
|
||||||
callback=typer_callback,
|
callback=typer_callback,
|
||||||
|
@ -52,6 +61,67 @@ def import_self(
|
||||||
handler.run(kwargs, progress=progress)
|
handler.run(kwargs, progress=progress)
|
||||||
|
|
||||||
|
|
||||||
|
@rattail_demo_typer.command()
|
||||||
|
def purge_shopfoo(
|
||||||
|
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 old Shopfoo export data
|
||||||
|
"""
|
||||||
|
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.ShopfooProductExport)\
|
||||||
|
.filter(model.ShopfooProductExport.created < app.make_utc(cutoff))\
|
||||||
|
.all()
|
||||||
|
|
||||||
|
def purger(session, export, cutoff, dry_run=False):
|
||||||
|
uuid = export.uuid
|
||||||
|
log.debug("purging export object %s: %s", uuid, export)
|
||||||
|
session.delete(export)
|
||||||
|
|
||||||
|
# maybe delete associated files
|
||||||
|
if not dry_run:
|
||||||
|
session.flush()
|
||||||
|
key = model.ShopfooProductExport.export_key
|
||||||
|
path = config.export_filepath(key, uuid)
|
||||||
|
if os.path.exists(path):
|
||||||
|
shutil.rmtree(path)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
run_purge(config, "Shopfoo Export", "Shopfoo Exports",
|
||||||
|
finder, purger,
|
||||||
|
before=before.date() if before else None,
|
||||||
|
before_days=before_days,
|
||||||
|
default_before_days=90,
|
||||||
|
dry_run=dry_run, progress=progress)
|
||||||
|
|
||||||
|
|
||||||
@rattail_demo_typer.command()
|
@rattail_demo_typer.command()
|
||||||
def install(
|
def install(
|
||||||
ctx: typer.Context,
|
ctx: typer.Context,
|
||||||
|
|
Loading…
Reference in a new issue