From 9191b4e3638f866d256ac3b40a59b6839a6d4ef5 Mon Sep 17 00:00:00 2001 From: Stefan Borer Date: Fri, 4 Oct 2019 13:05:01 +0200 Subject: [PATCH] fix(history): cleanup_history - add force argument --- caluma/core/management/commands/cleanup_history.py | 12 ++++++------ caluma/form/tests/test_command.py | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/caluma/core/management/commands/cleanup_history.py b/caluma/core/management/commands/cleanup_history.py index 55b852dac..7c217e54b 100644 --- a/caluma/core/management/commands/cleanup_history.py +++ b/caluma/core/management/commands/cleanup_history.py @@ -10,12 +10,12 @@ 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." @@ -23,6 +23,8 @@ def add_arguments(self, parser): ) def handle(self, *args, **options): + force = options["force"] + lt = parse(options["keep"]) if lt is not None: lt = timezone.make_aware(lt) @@ -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() diff --git a/caluma/form/tests/test_command.py b/caluma/form/tests/test_command.py index 7dcd84436..941d23e86 100644 --- a/caluma/form/tests/test_command.py +++ b/caluma/form/tests/test_command.py @@ -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 @@ -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