8

add custom model manager

This commit is contained in:
Paul J Stevens
2018-05-26 12:01:26 +02:00
parent 9710d3b479
commit bb34bddaf4
2 changed files with 19 additions and 10 deletions

View File

@@ -1,10 +1,12 @@
from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals
import random import random
from django import forms from django import forms
from django.conf import settings from django.conf import settings
from django.core.validators import MaxValueValidator, MinValueValidator from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models, transaction from django.db import models, transaction
from django.db.models import F
from django.template.defaultfilters import slugify from django.template.defaultfilters import slugify
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible
from django.utils.functional import cached_property from django.utils.functional import cached_property
@@ -199,6 +201,16 @@ class Segment(ClusterableModel):
return False return False
class PersonalisablePageManager(models.Manager):
def canonicals(self):
return (
self.filter(
personalisable_canonical_metadata__canonical_page_id=F(
'personalisable_canonical_metadata__variant__id'))
)
class PersonalisablePageMetadata(ClusterableModel): class PersonalisablePageMetadata(ClusterableModel):
"""The personalisable page model. Allows creation of variants with linked """The personalisable page model. Allows creation of variants with linked
segments. segments.
@@ -219,6 +231,8 @@ class PersonalisablePageMetadata(ClusterableModel):
on_delete=models.SET_NULL, on_delete=models.SET_NULL,
null=True, blank=True) null=True, blank=True)
objects = PersonalisablePageManager()
@cached_property @cached_property
def has_variants(self): def has_variants(self):
"""Return a boolean indicating whether or not the personalisable page """Return a boolean indicating whether or not the personalisable page

View File

@@ -1,6 +1,5 @@
import time import time
from django.db.models import F
from django.template.base import FilterExpression, kwarg_re from django.template.base import FilterExpression, kwarg_re
from django.utils import timezone from django.utils import timezone
@@ -99,13 +98,9 @@ def parse_tag(token, parser):
def exclude_variants(pages): def exclude_variants(pages):
"""Checks if page is not a variant """Checks if page is not a variant
:param pages: List of pages to check :param pages: Set of pages to check
:type pages: list :type pages: QuerySet
:return: List of pages that aren't variants :return: Queryset of pages that aren't variants
:rtype: list :rtype: QuerySet
""" """
return ( return pages.canonicals()
pages.filter(
personalisable_canonical_metadata__canonical_page_id=F(
'personalisable_canonical_metadata__variant__id'))
)