Created
April 27, 2015 12:44
-
-
Save balazs-endresz/65b1e65be74e05dca22c to your computer and use it in GitHub Desktop.
Wagtail page type validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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