예제 #1
0
def test_cleanup_history_command(db, force, dangling):
    # we need to override the registered models dict in order to get rid of the
    # fake models created in caluma_core tests
    cleanup_history.registered_models = {
        k: v
        for k, v in registered_models.items()
        if not k.startswith("caluma_core_")
    }
    kwargs = {}
    if force:
        kwargs["force"] = force
    if dangling:
        kwargs["dangling"] = dangling

    f1 = Form.objects.create(slug="form 1")

    doc = Document.objects.create(form=f1)
    assert Document.history.count() == 1

    doc.delete()
    f1.delete()

    # created 4 history entries:
    # HistoricalForm: 2 (1 created, 1 deleted)
    # HistoricalDocument: 2 (1 created, 1 deleted)

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

    assert Form.history.count() == 2
    assert Document.history.count() == 0 if (dangling and force) else 2
예제 #2
0
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 caluma_core tests
    cleanup_history.registered_models = {
        k: v for k, v in registered_models.items() if not k.startswith("caluma_core_")
    }
    kwargs = {}
    if force:
        kwargs["force"] = force
    else:
        kept = 3
    if keep:
        kwargs["keep"] = keep

    Form.objects.create(slug="form 1")

    f2 = Form.objects.create(slug="form 2")
    f2_hist = f2.history.first()
    f2_hist.history_date = f2_hist.history_date - timezone.timedelta(days=2)
    f2_hist.save()

    f3 = Form.objects.create(slug="form 3")
    f3_hist = f3.history.first()
    f3_hist.history_date = f3_hist.history_date - timezone.timedelta(days=730)
    f3_hist.save()

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

    assert Form.history.count() == kept
예제 #3
0
    def handle(self, *args, **options):
        force = options["force"]

        lt = parse(options["keep"])
        if lt is not None:
            lt = timezone.make_aware(lt)

        for _, model in registered_models.items():
            qs = model.history.all()
            if lt is not None:
                qs = model.history.filter(history_date__lt=lt)

            action_str = "Deleting" if force else "Would delete"
            self.stdout.write(
                f'{action_str} {qs.count()} historical records from model "{model.__name__}"'
            )
            if force:
                qs.delete()