Skip to content

Commit

Permalink
Replace workflow support check with WAGTAIL_WORKFLOW_ENABLED setting …
Browse files Browse the repository at this point in the history
…check

The workflow models can always be imported via wagtail.models even if workflow is disabled.

Instead of checking the import with try/except, we should check the setting instead.
  • Loading branch information
laymonage committed Jul 14, 2023
1 parent 690d0a7 commit 2a888bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
12 changes: 3 additions & 9 deletions wagtail/management/commands/purge_revisions.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 29 additions & 20 deletions wagtail/tests/test_management_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 2a888bc

Please sign in to comment.