Skip to content

Commit

Permalink
Added a factory for Redirect objects
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubMastalerz committed Jan 5, 2024
1 parent b4d53cf commit 41c036c
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/wagtail_factories/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from factory import errors, utils
from factory.declarations import ParameteredAttribute
from factory.django import DjangoModelFactory
from wagtail.contrib.redirects.models import Redirect
from wagtail.documents import get_document_model
from wagtail.images import get_image_model
from wagtail.models import Collection, Page, Site
Expand Down Expand Up @@ -146,3 +147,31 @@ class Meta:

title = "A document"
file = factory.django.FileField()


class RedirectFactory(DjangoModelFactory):
old_path = factory.Faker("slug")
site = factory.SubFactory(SiteFactory)
redirect_page = factory.SubFactory(PageFactory)
redirect_link = factory.Faker("url")
is_permanent = factory.Faker("boolean")

class Meta:
model = Redirect

@classmethod
def _create(cls, model_class, *args, **kwargs):
"""
Override _create() to ensure that Redirect.clean() is run
in order to normalise `old_path`.
"""
obj = model_class(*args, **kwargs)
try:
obj.clean()
except:
raise RuntimeError(
"Error occured when validating the `Redirect` object "
"created by this factory."
)
obj.save()
return obj

0 comments on commit 41c036c

Please sign in to comment.