From 66ed40f8ec0d16aaeaa58e21e083728cf2a73826 Mon Sep 17 00:00:00 2001 From: Paul J Stevens Date: Wed, 31 May 2017 11:11:19 +0200 Subject: [PATCH] make personalisedpage class abstract --- src/wagtail_personalisation/models.py | 18 +++++++++++++++++- src/wagtail_personalisation/views.py | 5 +++-- src/wagtail_personalisation/wagtail_hooks.py | 11 +++++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/wagtail_personalisation/models.py b/src/wagtail_personalisation/models.py index 6b6453b..cdf871e 100644 --- a/src/wagtail_personalisation/models.py +++ b/src/wagtail_personalisation/models.py @@ -83,11 +83,15 @@ class Segment(ClusterableModel): return segment_rules -class PersonalisablePage(Page): +class AbstractPersonalisablePage(models.Model): """The personalisable page model. Allows creation of variants with linked segments. """ + + class Meta: + abstract = True + canonical_page = models.ForeignKey( 'self', related_name='variations', on_delete=models.SET_NULL, blank=True, null=True @@ -110,6 +114,14 @@ class PersonalisablePage(Page): def __str__(self): return "{}".format(self.title) + @classmethod + def get_model(cls): + try: + cls.__subclasses__()[0] + except IndexError: + raise Exception("Unable to find non-abstract subclass for %s" % + cls.__name) + @cached_property def has_variations(self): """Return a boolean indicating whether or not the personalisable page @@ -135,6 +147,10 @@ class PersonalisablePage(Page): return not self.canonical_page and self.has_variations +class PersonalisablePage(AbstractPersonalisablePage, Page): + """ """ + + @cached_classmethod def get_edit_handler(cls): """Add additional edit handlers to pages that are allowed to have diff --git a/src/wagtail_personalisation/views.py b/src/wagtail_personalisation/views.py index ee419e5..3d13602 100644 --- a/src/wagtail_personalisation/views.py +++ b/src/wagtail_personalisation/views.py @@ -9,7 +9,7 @@ from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register from wagtail.contrib.modeladmin.views import IndexView from wagtail.wagtailcore.models import Page -from wagtail_personalisation.models import PersonalisablePage, Segment +from wagtail_personalisation.models import AbstractPersonalisablePage, Segment class SegmentModelIndexView(IndexView): @@ -123,9 +123,10 @@ def copy_page_view(request, page_id, segment_id): :rtype: django.http.HttpResponseRedirect """ + model = AbstractPersonalisablePage.get_model() if request.user.has_perm('wagtailadmin.access_admin'): segment = get_object_or_404(Segment, pk=segment_id) - page = get_object_or_404(PersonalisablePage, pk=page_id) + page = get_object_or_404(model, pk=page_id) slug = "{}-{}".format(page.slug, segment.encoded_name()) title = "{} ({})".format(page.title, segment.name) diff --git a/src/wagtail_personalisation/wagtail_hooks.py b/src/wagtail_personalisation/wagtail_hooks.py index a8f1175..7443a42 100644 --- a/src/wagtail_personalisation/wagtail_hooks.py +++ b/src/wagtail_personalisation/wagtail_hooks.py @@ -12,7 +12,7 @@ from wagtail.wagtailcore import hooks from wagtail_personalisation import admin_urls from wagtail_personalisation.adapters import get_segment_adapter -from wagtail_personalisation.models import PersonalisablePage, Segment +from wagtail_personalisation.models import AbstractPersonalisablePage, Segment from wagtail_personalisation.utils import impersonate_other_page logger = logging.getLogger(__name__) @@ -107,9 +107,10 @@ def _check_for_variations(segments, page): :rtype: wagtail_personalisation.models.PersonalisablePage or None """ + model = AbstractPersonalisablePage.get_model() for segment in segments: page_class = page.__class__ - if any(item == PersonalisablePage for item in page_class.__bases__): + if any(item == model for item in page_class.__bases__): variation = page_class.objects.filter( canonical_page=page, segment=segment) @@ -126,7 +127,8 @@ def page_listing_variant_buttons(page, page_perms, is_parent=False): the page (if any) and a 'Create a new variant' button. """ - personalisable_page = PersonalisablePage.objects.filter(pk=page.pk) + model = AbstractPersonalisablePage.get_model() + personalisable_page = model.objects.filter(pk=page.pk) segments = Segment.objects.all() if personalisable_page and len(segments) > 0 and not ( @@ -147,10 +149,11 @@ def page_listing_more_buttons(page, page_perms, is_parent=False): create a new variant for the selected segment. """ + model = AbstractPersonalisablePage.get_model() segments = Segment.objects.all() available_segments = [ item for item in segments - if not PersonalisablePage.objects.filter(segment=item, pk=page.pk) + if not model.objects.filter(segment=item, pk=page.pk) ] for segment in available_segments: