7

make personalisedpage class abstract

This commit is contained in:
Paul J Stevens
2017-05-31 11:11:19 +02:00
committed by Michael van Tellingen
parent 59b6e7f31e
commit 66ed40f8ec
3 changed files with 27 additions and 7 deletions

View File

@ -83,11 +83,15 @@ class Segment(ClusterableModel):
return segment_rules return segment_rules
class PersonalisablePage(Page): class AbstractPersonalisablePage(models.Model):
"""The personalisable page model. Allows creation of variants with linked """The personalisable page model. Allows creation of variants with linked
segments. segments.
""" """
class Meta:
abstract = True
canonical_page = models.ForeignKey( canonical_page = models.ForeignKey(
'self', related_name='variations', on_delete=models.SET_NULL, 'self', related_name='variations', on_delete=models.SET_NULL,
blank=True, null=True blank=True, null=True
@ -110,6 +114,14 @@ class PersonalisablePage(Page):
def __str__(self): def __str__(self):
return "{}".format(self.title) 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 @cached_property
def has_variations(self): def has_variations(self):
"""Return a boolean indicating whether or not the personalisable page """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 return not self.canonical_page and self.has_variations
class PersonalisablePage(AbstractPersonalisablePage, Page):
""" """
@cached_classmethod @cached_classmethod
def get_edit_handler(cls): def get_edit_handler(cls):
"""Add additional edit handlers to pages that are allowed to have """Add additional edit handlers to pages that are allowed to have

View File

@ -9,7 +9,7 @@ from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from wagtail.contrib.modeladmin.views import IndexView from wagtail.contrib.modeladmin.views import IndexView
from wagtail.wagtailcore.models import Page from wagtail.wagtailcore.models import Page
from wagtail_personalisation.models import PersonalisablePage, Segment from wagtail_personalisation.models import AbstractPersonalisablePage, Segment
class SegmentModelIndexView(IndexView): class SegmentModelIndexView(IndexView):
@ -123,9 +123,10 @@ def copy_page_view(request, page_id, segment_id):
:rtype: django.http.HttpResponseRedirect :rtype: django.http.HttpResponseRedirect
""" """
model = AbstractPersonalisablePage.get_model()
if request.user.has_perm('wagtailadmin.access_admin'): if request.user.has_perm('wagtailadmin.access_admin'):
segment = get_object_or_404(Segment, pk=segment_id) 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()) slug = "{}-{}".format(page.slug, segment.encoded_name())
title = "{} ({})".format(page.title, segment.name) title = "{} ({})".format(page.title, segment.name)

View File

@ -12,7 +12,7 @@ from wagtail.wagtailcore import hooks
from wagtail_personalisation import admin_urls from wagtail_personalisation import admin_urls
from wagtail_personalisation.adapters import get_segment_adapter 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 from wagtail_personalisation.utils import impersonate_other_page
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -107,9 +107,10 @@ def _check_for_variations(segments, page):
:rtype: wagtail_personalisation.models.PersonalisablePage or None :rtype: wagtail_personalisation.models.PersonalisablePage or None
""" """
model = AbstractPersonalisablePage.get_model()
for segment in segments: for segment in segments:
page_class = page.__class__ 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( variation = page_class.objects.filter(
canonical_page=page, segment=segment) 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. 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() segments = Segment.objects.all()
if personalisable_page and len(segments) > 0 and not ( 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. create a new variant for the selected segment.
""" """
model = AbstractPersonalisablePage.get_model()
segments = Segment.objects.all() segments = Segment.objects.all()
available_segments = [ available_segments = [
item for item in 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: for segment in available_segments: