diff --git a/.gitignore b/.gitignore index 16914b0..df72266 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ .vscode/ build/ +ve/ dist/ htmlcov/ docs/_build diff --git a/CHANGES b/CHANGES index 744d2e7..27dfc33 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +1.0.3 +================== +- bugfix:exclude variant returns queryset when params is queryset + 1.0.2 ================== - Upgrade to Wagtail 2.2 diff --git a/VERSON b/VERSON new file mode 100644 index 0000000..21e8796 --- /dev/null +++ b/VERSON @@ -0,0 +1 @@ +1.0.3 diff --git a/src/wagtail_personalisation/adapters.py b/src/wagtail_personalisation/adapters.py index 7c84f42..ad2971e 100644 --- a/src/wagtail_personalisation/adapters.py +++ b/src/wagtail_personalisation/adapters.py @@ -186,8 +186,7 @@ class SessionSegmentsAdapter(BaseSegmentsAdapter): for segment in enabled_segments: if segment.is_static and segment.static_users.filter(id=self.request.user.id).exists(): additional_segments.append(segment) - elif (segment.excluded_users.filter(id=self.request.user.id).exists() or - segment in excluded_segments): + elif (segment.excluded_users.filter(id=self.request.user.id).exists() or segment in excluded_segments): continue elif not segment.is_static or not segment.is_full: segment_rules = [] diff --git a/src/wagtail_personalisation/utils.py b/src/wagtail_personalisation/utils.py index 49bea40..2cd1951 100644 --- a/src/wagtail_personalisation/utils.py +++ b/src/wagtail_personalisation/utils.py @@ -98,22 +98,17 @@ def parse_tag(token, parser): def exclude_variants(pages): """Checks if page is not a variant - :param pages: List of pages to check - :type pages: list - :return: List of pages that aren't variants - :rtype: list + :param pages: List | Queryset of pages to check + :type pages: list or querset + :return: List|Queryset of pages that aren't variants + :rtype: list or queryset (depending on the param type) """ - return [ - page for page in pages - if ( - ( - hasattr(page, 'personalisation_metadata') is False - ) or - ( - hasattr(page, 'personalisation_metadata') and page.personalisation_metadata is None - ) or - ( - hasattr(page, 'personalisation_metadata') and page.personalisation_metadata.is_canonical - ) - ) - ] + for page in pages: + if hasattr(page, 'personalisation_metadata') is not False and \ + page.personalisation_metadata is not None and \ + page.personalisation_metadata.is_canonical is not True: + if (type(pages) == list): + pages.remove(page) + else: + pages = pages.exclude(pk=page.pk) + return pages diff --git a/src/wagtail_personalisation/wagtail_hooks.py b/src/wagtail_personalisation/wagtail_hooks.py index 6c91c99..b7e1ff3 100644 --- a/src/wagtail_personalisation/wagtail_hooks.py +++ b/src/wagtail_personalisation/wagtail_hooks.py @@ -185,8 +185,7 @@ class PersonalisedPagesSummaryPanel(PagesSummaryItem): order = 2100 def render(self): - page_count = models.PersonalisablePageMetadata.objects.filter( - segment__isnull=True).count() + page_count = models.PersonalisablePageMetadata.objects.filter(segment__isnull=True).count() title = _("Personalised Page") return mark_safe("""
  • diff --git a/tests/conftest.py b/tests/conftest.py index 77e07e7..7a4081a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,6 +7,11 @@ pytest_plugins = [ ] +@pytest.fixture(autouse=True) +def enable_db_access(db): + pass + + @pytest.fixture(scope='session') def django_db_setup(django_db_setup, django_db_blocker): from wagtail.core.models import Page, Site diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index f5cee8a..2b3fa15 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1,6 +1,8 @@ from wagtail_personalisation.utils import ( exclude_variants, impersonate_other_page) +from wagtail.core.models import Page as WagtailPage + class Page(object): def __init__(self, path, depth, url_path, title): @@ -57,3 +59,54 @@ def test_exclude_variants_excludes_pages_with_metadata_not_canonical(): page.personalisation_metadata.is_canonical = False result = exclude_variants([page]) assert result == [] + + +def test_exclude_variants_with_pages_querysets(): + ''' + Test that excludes variant works for querysets + ''' + for i in range(5): + page = WagtailPage(path="/" + str(i), depth=0, url_path="/", title="Hoi " + str(i)) + page.save() + pages = WagtailPage.objects.all().order_by('id') + + result = exclude_variants(pages) + assert type(result) == type(pages) + assert result == pages + + +def test_exclude_variants_with_pages_querysets_not_canonical(): + ''' + Test that excludes variant works for querysets with + personalisation_metadata canonical False + ''' + for i in range(5): + page = WagtailPage(path="/" + str(i), depth=0, url_path="/", title="Hoi " + str(i)) + page.save() + pages = WagtailPage.objects.all().order_by('id') + # add variants + for page in pages: + page.personalisation_metadata = Metadata(is_canonical=False) + page.save() + + result = exclude_variants(pages) + assert type(result) == type(pages) + assert result.count() == 0 + + +def test_exclude_variants_with_pages_querysets_meta_none(): + ''' + Test that excludes variant works for querysets with meta as none + ''' + for i in range(5): + page = WagtailPage(path="/" + str(i), depth=0, url_path="/", title="Hoi " + str(i)) + page.save() + pages = WagtailPage.objects.all().order_by('id') + # add variants + for page in pages: + page.personalisation_metadata = None + page.save() + + result = exclude_variants(pages) + assert type(result) == type(pages) + assert result == pages