Handles both Python 2 & 3, and multiple optimisations & simplifications.
This commit is contained in:
@@ -11,9 +11,9 @@ from wagtail.wagtailadmin.edit_handlers import (
|
||||
FieldPanel, FieldRowPanel, InlinePanel, MultiFieldPanel, ObjectList,
|
||||
PageChooserPanel, TabbedInterface)
|
||||
|
||||
from wagtail_personalisation.forms import AdminPersonalisablePageForm
|
||||
from wagtail_personalisation.rules import AbstractBaseRule
|
||||
from wagtail_personalisation.utils import count_active_days
|
||||
from .forms import AdminPersonalisablePageForm
|
||||
from .rules import AbstractBaseRule
|
||||
from .utils import count_active_days
|
||||
|
||||
|
||||
class SegmentQuerySet(models.QuerySet):
|
||||
@@ -61,9 +61,9 @@ class Segment(ClusterableModel):
|
||||
], heading="Segment"),
|
||||
MultiFieldPanel([
|
||||
InlinePanel(
|
||||
"{}_related".format(rule._meta.db_table),
|
||||
label=rule.__str__,
|
||||
) for rule in AbstractBaseRule.__subclasses__()
|
||||
"{}_related".format(rule_model._meta.db_table),
|
||||
label=rule_model._meta.verbose_name,
|
||||
) for rule_model in AbstractBaseRule.__subclasses__()
|
||||
], heading="Rules"),
|
||||
]
|
||||
|
||||
@@ -82,10 +82,10 @@ class Segment(ClusterableModel):
|
||||
|
||||
def get_rules(self):
|
||||
"""Retrieve all rules in the segment."""
|
||||
rules = AbstractBaseRule.__subclasses__()
|
||||
segment_rules = []
|
||||
for rule in rules:
|
||||
segment_rules += rule.objects.filter(segment=self)
|
||||
for rule_model in AbstractBaseRule.get_descendant_models():
|
||||
segment_rules.extend(
|
||||
rule_model._default_manager.filter(segment=self))
|
||||
return segment_rules
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ class PersonalisablePageMixin(models.Model):
|
||||
blank=True, null=True
|
||||
)
|
||||
segment = models.ForeignKey(
|
||||
Segment, related_name='segments', on_delete=models.PROTECT,
|
||||
Segment, related_name='pages', on_delete=models.PROTECT,
|
||||
blank=True, null=True
|
||||
)
|
||||
is_segmented = models.BooleanField(default=False)
|
||||
@@ -117,9 +117,6 @@ class PersonalisablePageMixin(models.Model):
|
||||
|
||||
base_form_class = AdminPersonalisablePageForm
|
||||
|
||||
def __str__(self):
|
||||
return "{}".format(self.title)
|
||||
|
||||
@cached_property
|
||||
def has_variations(self):
|
||||
"""Return a boolean indicating whether or not the personalisable page
|
||||
@@ -137,12 +134,20 @@ class PersonalisablePageMixin(models.Model):
|
||||
"""Return a boolean indicating whether or not the personalisable page
|
||||
is a canonical page.
|
||||
|
||||
:returns: A boolean indicating whether or not the personalisable page
|
||||
:returns: A boolean indicating whether or not the personalisable
|
||||
page
|
||||
is a canonical page.
|
||||
:rtype: bool
|
||||
|
||||
"""
|
||||
return not self.canonical_page and self.has_variations
|
||||
return self.canonical_page_id is None
|
||||
|
||||
def get_unused_segments(self):
|
||||
if not hasattr(self, '_unused_segments'):
|
||||
self._unused_segments = (
|
||||
Segment.objects.exclude(pages__canonical_page=self)
|
||||
if self.is_canonical else Segment.objects.none())
|
||||
return self._unused_segments
|
||||
|
||||
def copy_for_segment(self, segment):
|
||||
slug = "{}-{}".format(self.slug, segment.encoded_name())
|
||||
|
Reference in New Issue
Block a user