From 6640bf8d74f7d9b8aa7902fbedf1044772663d07 Mon Sep 17 00:00:00 2001 From: Jasper Berghoef Date: Tue, 25 Apr 2017 16:15:33 +0200 Subject: [PATCH] Splits test factories and updates documentation --- .gitignore | 1 + README.rst | 6 ++++- docs/usage_guide.rst | 44 +++++++++++++++++++++----------- tests/factories/rule.py | 48 +++++++++++++++++++++++++++++++++++ tests/factories/segment.py | 44 +------------------------------- tests/unit/test_factories.py | 8 ++++-- tests/unit/test_middleware.py | 7 +++-- tests/unit/test_utils.py | 2 ++ 8 files changed, 97 insertions(+), 63 deletions(-) create mode 100644 tests/factories/rule.py diff --git a/.gitignore b/.gitignore index 7515591..ca18203 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ docs/_build coverage.xml db.sqlite3 .vscode/settings.json +.DS_Store diff --git a/README.rst b/README.rst index 276628b..457d053 100644 --- a/README.rst +++ b/README.rst @@ -2,7 +2,9 @@ Wagtail personalisation ======================= -Wagtail personalisation enables simple content personalisation through segmenting for Wagtail. +Wagtail personalisation enables simple content personalisation through segmentation for the `Wagtail CMS`_. + +.. _Wagtail CMS: http://wagtail.io/ Instructions @@ -24,6 +26,7 @@ Next, include the ``personalisation`` and ``wagtail.contrib.modeladmin`` app in Make sure that ``django.contrib.sessions.middleware.SessionMiddleware`` has been added in first, this is a prerequisite for this project. + Changing segments adapter ------------------------- To change the segments adapter, first make a new one based on the ``BaseSegmentsAdapter`` @@ -35,6 +38,7 @@ To change the segments adapter, first make a new one based on the ``BaseSegments Add the ``PERSONALISATION_SEGMENTS_ADAPTER`` setting to your settings.py and choose your own adapter. + Roadmap ------------------------- .. image:: roadmap.png diff --git a/docs/usage_guide.rst b/docs/usage_guide.rst index c75136b..d88fe67 100644 --- a/docs/usage_guide.rst +++ b/docs/usage_guide.rst @@ -4,36 +4,44 @@ Usage guide Creating a segment ------------------ -As soon as the installation is completed and configured, the module will be visible in the Wagtail administrative area. +As soon as the installation is completed and configured, the module will be +visible in the Wagtail administrative area. -To create a segment, go to the "Segments" page. Click on "Add a new segment". +To create a segment, go to the "Segments" page and click on "Add a new segment". -On this page you will be presented with a form. Follow the steps to create a segment: +On this page you will be presented with a form. Follow these steps to create a +new segment: 1. Enter a name for your segment. 2. (Optional) Select whether to match any or all defined rules. - ``match any`` will result in a segment that is applied as soon as one of your rules matches the visitor. - When ``match all`` is selected, all rules must match before the segment is applied. + ``match any`` will result in a segment that is applied as soon as one of + your rules matches the visitor. When ``match all`` is selected, all rules + must match before the segment is applied. 3. (Optional) Set the segment persistence. - When persistence is enabled, your segment will stick to the visitor once applied, even if the rules no longer match on the next visit. + When persistence is enabled, your segment will stick to the visitor once + applied, even if the rules no longer match on the next visit. 4. Define your segment rules. - Wagxperience comes with a basic set of rules :doc:`default_rules` that allow you to get started quickly. The rules you define will be evaluated once a visitor makes a request to your application. + Wagxperience comes with a basic set of :doc:`default_rules` that allow + you to get started quickly. The rules you define will be evaluated once a + visitor makes a request to your application. 5. Save your segment. - Click "save" to store your segment. It will be enabled by default, unless otherwise defined. + Click "save" to store your segment. It will be enabled by default, + unless otherwise defined. -Creating content for your segment ---------------------------------- +Creating personalized content +----------------------------- -Once you've created a segment you can start serving these visitors with personalised content. To do this, you can go one of two directions. +Once you've created a segment you can start serving these visitors with +personalised content. To do this, you can go one of two directions. 1. Create a copy of a page for your segment. @@ -43,13 +51,19 @@ Once you've created a segment you can start serving these visitors with personal Method 1: Create a copy ^^^^^^^^^^^^^^^^^^^^^^^ -To create a copy from a page for a specific Segment (which you can change to your liking after copying it) simply go to the Explorer section and find the page you'd wish to personalize. +To create a copy from a page for a specific Segment (which you can change to +your liking after copying it) simply go to the Explorer section and find the +page you'd wish to personalize. -You'll notice a new "Variants" dropdown button has appeared. Click the button and select the segment you'd like to create personalized content for. +You'll notice a new "Variants" dropdown button has appeared. Click the button +and select the segment you'd like to create personalized content for. -Once you've selected the segment, a copy of the page will be created with a title that includes the segment. Don't worry, you'r visitors won't be able to see this title. +Once you've selected the segment, a copy of the page will be created with a +title that includes the segment. Don't worry, you'r visitors won't be able to +see this title. -You can change everything on this page you'd like. Visitors that are included in your segment, will automatically see the new page you've created for them. +You can change everything on this page you'd like. Visitors that are included in +your segment, will automatically see the new page you've created for them. Method 2: Create a block diff --git a/tests/factories/rule.py b/tests/factories/rule.py new file mode 100644 index 0000000..9d6dd30 --- /dev/null +++ b/tests/factories/rule.py @@ -0,0 +1,48 @@ +from __future__ import absolute_import, unicode_literals + +import datetime +import factory + +from personalisation import rules + + +class DayRuleFactory(factory.DjangoModelFactory): + + class Meta: + model = rules.DayRule + + +class DeviceRuleFactory(factory.DjangoModelFactory): + + class Meta: + model = rules.DeviceRule + + +class QueryRuleFactory(factory.DjangoModelFactory): + + class Meta: + model = rules.QueryRule + + +class ReferralRuleFactory(factory.DjangoModelFactory): + regex_string = "test.test" + + class Meta: + model = rules.ReferralRule + + +class TimeRuleFactory(factory.DjangoModelFactory): + start_time = datetime.time(8, 0, 0) + end_time = datetime.time(23, 0, 0) + + class Meta: + model = rules.TimeRule + + +class VisitCountRuleFactory(factory.DjangoModelFactory): + operator = "more_than" + count = 0 + + class Meta: + model = rules.VisitCountRule + diff --git a/tests/factories/segment.py b/tests/factories/segment.py index 10527a4..c9a7667 100644 --- a/tests/factories/segment.py +++ b/tests/factories/segment.py @@ -1,10 +1,8 @@ from __future__ import absolute_import, unicode_literals -import datetime - import factory -from personalisation import models, rules +from personalisation import models class SegmentFactory(factory.DjangoModelFactory): @@ -13,43 +11,3 @@ class SegmentFactory(factory.DjangoModelFactory): class Meta: model = models.Segment - - -class TimeRuleFactory(factory.DjangoModelFactory): - start_time = datetime.time(8, 0, 0) - end_time = datetime.time(23, 0, 0) - - class Meta: - model = rules.TimeRule - - -class DayRuleFactory(factory.DjangoModelFactory): - - class Meta: - model = rules.DayRule - - -class ReferralRuleFactory(factory.DjangoModelFactory): - regex_string = "test.test" - - class Meta: - model = rules.ReferralRule - - -class VisitCountRuleFactory(factory.DjangoModelFactory): - operator = "more_than" - count = 0 - - class Meta: - model = rules.VisitCountRule - - -class QueryRuleFactory(factory.DjangoModelFactory): - - class Meta: - model = rules.QueryRule - -class DeviceRuleFactory(factory.DjangoModelFactory): - - class Meta: - model = rules.DeviceRule diff --git a/tests/unit/test_factories.py b/tests/unit/test_factories.py index 2f4e1cc..37a5d8f 100644 --- a/tests/unit/test_factories.py +++ b/tests/unit/test_factories.py @@ -6,8 +6,12 @@ import pytest from personalisation.models import Segment from personalisation.rules import TimeRule -from tests.factories.segment import ( - ReferralRuleFactory, SegmentFactory, TimeRuleFactory, DayRuleFactory, DeviceRuleFactory) + +from tests.factories.segment import SegmentFactory +from tests.factories.rule import ( + QueryRuleFactory, ReferralRuleFactory, TimeRuleFactory, + DayRuleFactory, VisitCountRuleFactory, DeviceRuleFactory) + """Factory tests""" @pytest.mark.django_db diff --git a/tests/unit/test_middleware.py b/tests/unit/test_middleware.py index 650016f..6400eda 100644 --- a/tests/unit/test_middleware.py +++ b/tests/unit/test_middleware.py @@ -8,8 +8,9 @@ from freezegun import freeze_time from wagtail.wagtailcore.models import Page from wagtail_factories import SiteFactory -from tests.factories.segment import ( - QueryRuleFactory, ReferralRuleFactory, SegmentFactory, TimeRuleFactory, +from tests.factories.segment import SegmentFactory +from tests.factories.rule import ( + QueryRuleFactory, ReferralRuleFactory, TimeRuleFactory, DayRuleFactory, VisitCountRuleFactory, DeviceRuleFactory) @@ -22,11 +23,13 @@ class TestUserSegmenting(object): """ self.site = SiteFactory(is_default_site=True) + def test_no_segments(self, client): request = client.get('/') assert client.session['segments'] == [] + @freeze_time("10:00:00") def test_time_segment(self, client): time_only_segment = SegmentFactory(name='Time only') diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index bd1456f..9bcb959 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -2,6 +2,7 @@ import pytest from personalisation.utils import impersonate_other_page + class Page(object): def __init__(self, path, depth, url_path, title): self.path = path @@ -12,6 +13,7 @@ class Page(object): def __eq__(self, other): return self.__dict__ == other.__dict__ + def test_impersonate_other_page(): page = Page(path="/", depth=0, url_path="/", title="Hoi") other_page = Page(path="/other", depth=1, url_path="/other", title="Doei")