Skip to content

Instantly share code, notes, and snippets.

@balazs-endresz
Created April 27, 2015 12:44
Show Gist options
  • Save balazs-endresz/65b1e65be74e05dca22c to your computer and use it in GitHub Desktop.
Save balazs-endresz/65b1e65be74e05dca22c to your computer and use it in GitHub Desktop.
Wagtail page type validator
from django.core.exceptions import ValidationError
from django.utils.deconstruct import deconstructible
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.utils import resolve_model_string
@deconstructible
class PageTypeValidator(object):
"""
Usage:
class HomePage(Page):
related_page = models.ForeignKey(
'wagtailcore.Page', null=True, blank=True, on_delete=models.SET_NULL, related_name='+',
validators=[PageTypeValidator('blog.BlogIndex', 'blog.BlogPost')]
)
"""
def __init__(self, *models):
self.models = models
def __call__(self, page_id):
page = Page.objects.get(id=page_id).specific
if not any(isinstance(page, resolve_model_string(model)) for model in self.models):
raise ValidationError("Please select a valid page type")
def __eq__(self, other):
return self.models == other.models
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment