From 4c09ad4ca7721d38ecb6edab8a19f51673f2c1fa Mon Sep 17 00:00:00 2001 From: sewagodimo Date: Wed, 9 Jan 2019 16:52:26 +0200 Subject: [PATCH 01/14] fix flake error W504 --- src/wagtail_personalisation/adapters.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 = [] From 875d8302deb220c188ba7608f4dcfa758137ccbd Mon Sep 17 00:00:00 2001 From: sewagodimo Date: Wed, 9 Jan 2019 16:54:04 +0200 Subject: [PATCH 02/14] exclude variants should return a list when a list if given or a queryset --- src/wagtail_personalisation/utils.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/wagtail_personalisation/utils.py b/src/wagtail_personalisation/utils.py index 49bea40..bdc3c31 100644 --- a/src/wagtail_personalisation/utils.py +++ b/src/wagtail_personalisation/utils.py @@ -103,17 +103,13 @@ def exclude_variants(pages): :return: List of pages that aren't variants :rtype: list """ - 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 not ((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)): + if (type(pages) == list): + pages.remove(page) + else: + pages.exclude(pk=page.pk) + return pages From 94a5c6b289fc179ada8100d68813a55d40327eaf Mon Sep 17 00:00:00 2001 From: sewagodimo Date: Wed, 9 Jan 2019 16:54:28 +0200 Subject: [PATCH 03/14] tests for querysets in variant_exclude --- tests/unit/test_utils.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index f5cee8a..c5f4af5 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,15 @@ 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 too + ''' + page1 = WagtailPage(path="/", depth=0, url_path="/", title="Hoi") + page2 = WagtailPage(path="/", depth=0, url_path="/", title="Hoi") + pages = WagtailPage.objects.all().order_by('id') + print(pages) + result = exclude_variants(pages) + assert result == pages From 28266c450080a91a85bc1caacdc3ce011d9b7761 Mon Sep 17 00:00:00 2001 From: sewagodimo Date: Wed, 9 Jan 2019 16:54:49 +0200 Subject: [PATCH 04/14] fix flake error --- src/wagtail_personalisation/wagtail_hooks.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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("""
  • From c07b28027670174e19da055180451257517bbf25 Mon Sep 17 00:00:00 2001 From: sewagodimo Date: Wed, 9 Jan 2019 16:55:06 +0200 Subject: [PATCH 05/14] allow database access for tests --- tests/conftest.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 77e07e7..1f95a98 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,6 +6,10 @@ pytest_plugins = [ 'tests.fixtures' ] +@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 5cd87514505e31335d4f0ee0a78742a3951e07d1 Mon Sep 17 00:00:00 2001 From: sewagodimo Date: Wed, 9 Jan 2019 16:56:38 +0200 Subject: [PATCH 06/14] add ve to the gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) 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 From b135e79c7743d476ed1b02352f4644ef57ac8af3 Mon Sep 17 00:00:00 2001 From: sewagodimo Date: Wed, 9 Jan 2019 17:04:00 +0200 Subject: [PATCH 07/14] remove trailing print --- tests/unit/test_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index c5f4af5..73cc51d 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -68,6 +68,5 @@ def test_exclude_variants_with_pages_querysets(): page1 = WagtailPage(path="/", depth=0, url_path="/", title="Hoi") page2 = WagtailPage(path="/", depth=0, url_path="/", title="Hoi") pages = WagtailPage.objects.all().order_by('id') - print(pages) result = exclude_variants(pages) assert result == pages From cbb56847ae9dd5407aef006933e9aaf1adee9129 Mon Sep 17 00:00:00 2001 From: sewagodimo Date: Wed, 9 Jan 2019 17:17:51 +0200 Subject: [PATCH 08/14] fix flake8 error --- tests/unit/test_utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 73cc51d..0f04056 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -65,8 +65,9 @@ def test_exclude_variants_with_pages_querysets(): ''' Test that excludes variant works for querysets too ''' - page1 = WagtailPage(path="/", depth=0, url_path="/", title="Hoi") - page2 = WagtailPage(path="/", depth=0, url_path="/", title="Hoi") + 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 result == pages From f054b86e071b2d669ab11226dbe072ff8f42f20d Mon Sep 17 00:00:00 2001 From: sewagodimo Date: Wed, 9 Jan 2019 17:25:56 +0200 Subject: [PATCH 09/14] fix flake error in conftest --- tests/conftest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/conftest.py b/tests/conftest.py index 1f95a98..7a4081a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,6 +6,7 @@ pytest_plugins = [ 'tests.fixtures' ] + @pytest.fixture(autouse=True) def enable_db_access(db): pass From 9235932f00b3acc858b938d61471b142a10f285e Mon Sep 17 00:00:00 2001 From: sewagodimo Date: Thu, 10 Jan 2019 12:41:53 +0200 Subject: [PATCH 10/14] update exclude varient format and add variants to tests --- src/wagtail_personalisation/utils.py | 22 +++++++++++----------- tests/unit/test_utils.py | 7 ++++++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/wagtail_personalisation/utils.py b/src/wagtail_personalisation/utils.py index bdc3c31..3579911 100644 --- a/src/wagtail_personalisation/utils.py +++ b/src/wagtail_personalisation/utils.py @@ -98,18 +98,18 @@ 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) """ for page in pages: - if not ((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)): - if (type(pages) == list): - pages.remove(page) - else: - pages.exclude(pk=page.pk) + 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.exclude(pk=page.pk) return pages diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 0f04056..1c4a6a0 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -69,5 +69,10 @@ def test_exclude_variants_with_pages_querysets(): 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) + pages = WagtailPage.objects.all().order_by('id') result = exclude_variants(pages) - assert result == pages + assert type(result) == type(pages) + assert result != pages From 650e061f9165f3e61fbc98adae722052473f83a6 Mon Sep 17 00:00:00 2001 From: sewagodimo Date: Thu, 10 Jan 2019 14:47:01 +0200 Subject: [PATCH 11/14] assign pages on exclude --- src/wagtail_personalisation/utils.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/wagtail_personalisation/utils.py b/src/wagtail_personalisation/utils.py index 3579911..2cd1951 100644 --- a/src/wagtail_personalisation/utils.py +++ b/src/wagtail_personalisation/utils.py @@ -103,7 +103,6 @@ def exclude_variants(pages): :return: List|Queryset of pages that aren't variants :rtype: list or queryset (depending on the param type) """ - for page in pages: if hasattr(page, 'personalisation_metadata') is not False and \ page.personalisation_metadata is not None and \ @@ -111,5 +110,5 @@ def exclude_variants(pages): if (type(pages) == list): pages.remove(page) else: - pages.exclude(pk=page.pk) + pages = pages.exclude(pk=page.pk) return pages From ceef8063014936c778e1c5f4df92fe90e4fc31cc Mon Sep 17 00:00:00 2001 From: sewagodimo Date: Thu, 10 Jan 2019 14:47:18 +0200 Subject: [PATCH 12/14] add tests for varient exclusion use cases --- tests/unit/test_utils.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 1c4a6a0..30f3fbb 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -69,10 +69,43 @@ def test_exclude_variants_with_pages_querysets(): 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 too + ''' + 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) - pages = WagtailPage.objects.all().order_by('id') + page.save() + result = exclude_variants(pages) assert type(result) == type(pages) - assert result != pages + assert result.count() == 0 + + +def test_exclude_variants_with_pages_querysets_meta_none(): + ''' + Test that excludes variant works for querysets too + ''' + 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 From 60675203c60dac8ed020a5fd6116626cbe0160b1 Mon Sep 17 00:00:00 2001 From: sewagodimo Date: Thu, 10 Jan 2019 14:51:43 +0200 Subject: [PATCH 13/14] fixed some comments --- tests/unit/test_utils.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 30f3fbb..2b3fa15 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -63,7 +63,7 @@ def test_exclude_variants_excludes_pages_with_metadata_not_canonical(): def test_exclude_variants_with_pages_querysets(): ''' - Test that excludes variant works for querysets too + 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)) @@ -77,7 +77,8 @@ def test_exclude_variants_with_pages_querysets(): def test_exclude_variants_with_pages_querysets_not_canonical(): ''' - Test that excludes variant works for querysets too + 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)) @@ -95,7 +96,7 @@ def test_exclude_variants_with_pages_querysets_not_canonical(): def test_exclude_variants_with_pages_querysets_meta_none(): ''' - Test that excludes variant works for querysets too + 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)) From 9cc6e966ba18529ad0c8508a9f035e3f3a0f7b22 Mon Sep 17 00:00:00 2001 From: sewagodimo Date: Thu, 10 Jan 2019 16:12:01 +0200 Subject: [PATCH 14/14] bugfix:exclude variant returns queryset when params is queryset --- CHANGES | 4 ++++ VERSON | 1 + 2 files changed, 5 insertions(+) create mode 100644 VERSON 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