diff --git a/wagtail/management/commands/purge_revisions.py b/wagtail/management/commands/purge_revisions.py index 275ead915788..7008fc159296 100644 --- a/wagtail/management/commands/purge_revisions.py +++ b/wagtail/management/commands/purge_revisions.py @@ -1,15 +1,9 @@ +from django.conf import settings from django.core.management.base import BaseCommand from django.db.models import Q from django.utils import timezone -from wagtail.models import Revision - -try: - from wagtail.models import WorkflowState - - workflow_support = True -except ImportError: - workflow_support = False +from wagtail.models import Revision, WorkflowState class Command(BaseCommand): @@ -46,7 +40,7 @@ def purge_revisions(days=None): approved_go_live_at__isnull=False ) - if workflow_support: + if getattr(settings, "WAGTAIL_WORKFLOW_ENABLED", True): purgeable_revisions = purgeable_revisions.exclude( # and exclude revisions linked to an in progress or needs changes workflow state Q(task_states__workflow_state__status=WorkflowState.STATUS_IN_PROGRESS) diff --git a/wagtail/tests/test_management_commands.py b/wagtail/tests/test_management_commands.py index 12952bcf5fde..e2a1c904c4f2 100644 --- a/wagtail/tests/test_management_commands.py +++ b/wagtail/tests/test_management_commands.py @@ -6,11 +6,19 @@ from django.contrib.auth.models import Group from django.core import management from django.db import models -from django.test import TestCase +from django.test import TestCase, override_settings from django.utils import timezone from wagtail.embeds.models import Embed -from wagtail.models import Collection, Page, PageLogEntry, Revision +from wagtail.models import ( + Collection, + Page, + PageLogEntry, + Revision, + Task, + Workflow, + WorkflowTask, +) from wagtail.signals import page_published, page_unpublished, published, unpublished from wagtail.test.testapp.models import ( DraftStateModel, @@ -643,7 +651,7 @@ def test_latest_revision_not_purged(self): self.assertFalse(Revision.objects.filter(id=revision_1.id).exists()) self.assertTrue(Revision.objects.filter(id=revision_2.id).exists()) - def test_revisions_in_moderation_not_purged(self): + def test_revisions_in_moderation_or_workflow_not_purged(self): revision = self.object.save_revision(submitted_for_moderation=True) # Save a new revision to ensure that the moderated revision @@ -654,28 +662,29 @@ def test_revisions_in_moderation_not_purged(self): self.assertTrue(Revision.objects.filter(id=revision.id).exists()) - try: - from wagtail.models import Task, Workflow, WorkflowTask + workflow = Workflow.objects.create(name="test_workflow") + task_1 = Task.objects.create(name="test_task_1") + user = get_user_model().objects.first() + WorkflowTask.objects.create(workflow=workflow, task=task_1, sort_order=1) - workflow = Workflow.objects.create(name="test_workflow") - task_1 = Task.objects.create(name="test_task_1") - user = get_user_model().objects.first() - WorkflowTask.objects.create(workflow=workflow, task=task_1, sort_order=1) + revision = self.object.save_revision() + workflow.start(self.object, user) + + # Save a new revision to ensure that the revision in the workflow + # is not the latest one + self.object.save_revision() - revision = self.object.save_revision() - workflow.start(self.object, user) + self.run_command() - # Save a new revision to ensure that the revision in the workflow - # is not the latest one - self.object.save_revision() + # even though they're no longer the latest revisions, the old revisions + # should stay as they are attached to an in progress workflow + self.assertTrue(Revision.objects.filter(id=revision.id).exists()) + # If workflow is disabled at some point after that, the revision should + # be deleted + with override_settings(WAGTAIL_WORKFLOW_ENABLED=False): self.run_command() - - # even though they're no longer the latest revisions, the old revisions - # should stay as they are attached to an in progress workflow - self.assertTrue(Revision.objects.filter(id=revision.id).exists()) - except ImportError: - pass + self.assertFalse(Revision.objects.filter(id=revision.id).exists()) def test_revisions_with_approve_go_live_not_purged(self): revision = self.object.save_revision(