Skip to content

Commit

Permalink
fix(history): cleanup_history - add force argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Borer committed Oct 8, 2019
1 parent 1519a92 commit 9191b4e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
12 changes: 6 additions & 6 deletions caluma/core/management/commands/cleanup_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ class Command(BaseCommand):
help = "Cleanup historical records."

def add_arguments(self, parser):
parser.add_argument("--dry", dest="dry", action="store_true")
parser.add_argument("--force", dest="force", default=False, action="store_true")
parser.add_argument(
"-k",
"--keep",
dest="keep",
default="",
default="1 year",
help=(
"Duration we want to keep the records. "
"E.g. '6 months', '1 year'. Uses dateparser."
),
)

def handle(self, *args, **options):
force = options["force"]

lt = parse(options["keep"])
if lt is not None:
lt = timezone.make_aware(lt)
Expand All @@ -32,11 +34,9 @@ def handle(self, *args, **options):
if lt is not None:
qs = model.history.filter(history_date__lt=lt)

action_str = "Would delete"
if not options["dry"]:
action_str = "Deleting"
action_str = "Deleting" if force else "Would delete"
self.stdout.write(
f'{action_str} {qs.count()} historical records from model "{model.__name__}"'
)
if not options["dry"]:
if force:
qs.delete()
14 changes: 7 additions & 7 deletions caluma/form/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ def test_create_bucket_command(mocker):
Minio.make_bucket.assert_called_once_with(settings.MINIO_STORAGE_MEDIA_BUCKET_NAME)


@pytest.mark.parametrize("dry", [True, False])
@pytest.mark.parametrize("keep,kept", [("1 year", 2), ("1 day", 1), (None, 0)])
def test_cleanup_history_command(db, dry, keep, kept):
@pytest.mark.parametrize("force", [True, False])
@pytest.mark.parametrize("keep,kept", [("1 year", 2), ("1 day", 1), (None, 2)])
def test_cleanup_history_command(db, force, keep, kept):
# we need to override the registered models dict in order to get rid of the
# fake models created in core tests
cleanup_history.registered_models = {
k: v for k, v in registered_models.items() if not k.startswith("core_")
}
args = []
kwargs = {}
if dry:
args.append("--dry")
if force:
kwargs["force"] = force
else:
kept = 3
if keep:
kwargs["keep"] = keep
Expand All @@ -46,6 +46,6 @@ def test_cleanup_history_command(db, dry, keep, kept):
f3_hist.history_date = f3_hist.history_date - timezone.timedelta(days=730)
f3_hist.save()

call_command("cleanup_history", *args, **kwargs, stdout=open(os.devnull, "w"))
call_command("cleanup_history", **kwargs, stdout=open(os.devnull, "w"))

assert Form.history.count() == kept

0 comments on commit 9191b4e

Please sign in to comment.