Skip to content

Instantly share code, notes, and snippets.

@nimasmi
Last active May 6, 2016 13:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nimasmi/edd58573e7f8c49bc2ff to your computer and use it in GitHub Desktop.
Save nimasmi/edd58573e7f8c49bc2ff to your computer and use it in GitHub Desktop.
from wagtail.wagtailcore.models import get_page_models
from wagtail.wagtailcore.utils import resolve_model_string
class SinglePageInstanceMixin(object):
@classmethod
def clean_parent_page_types(cls):
"""
Rewrite wagtailcore.models.Page.clean_parent_page_models to ensure that
this is recalculated every time it's needed, rather than only the first
time. It returns parent_page_models, normalised as model classes, if so
defined on the model, otherwise all page-derived models, but only if no
instance of this Page-derived class currently exists. Otherwise returns
[], thus enforcing (in view logic) that only one instance of this class
can exist at a time.
"""
if cls.objects.all().exists():
return []
else:
parent_page_types = getattr(cls, 'parent_page_types', None)
if parent_page_types is None:
# if parent_page_types is not specified on the Page class, allow all page types as subpages
res = get_page_models()
else:
res = [
resolve_model_string(model_string, cls._meta.app_label)
for model_string in parent_page_types
]
for model in res:
if not issubclass(model, Page):
raise LookupError("%s is not a Page subclass" % model)
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment