diff --git a/src/personalisation/adapters.py b/src/personalisation/adapters.py index bce7669..bd73dc8 100644 --- a/src/personalisation/adapters.py +++ b/src/personalisation/adapters.py @@ -1,4 +1,5 @@ from __future__ import absolute_import, unicode_literals + import time from django.db.models import F @@ -43,6 +44,9 @@ class BaseSegmentsAdapter(object): class SessionSegmentsAdapter(BaseSegmentsAdapter): + """ + Segment adapter that uses Django's session backend. + """ def setup(self, request): self.request = request @@ -96,4 +100,3 @@ class SessionSegmentsAdapter(BaseSegmentsAdapter): self.request.session['segments'] = new_segments update_visit_count(self) - diff --git a/src/personalisation/app_settings.py b/src/personalisation/app_settings.py index a64ceec..2d85063 100644 --- a/src/personalisation/app_settings.py +++ b/src/personalisation/app_settings.py @@ -1,6 +1,4 @@ from django.conf import settings from django.utils.module_loading import import_string - segments_adapter = import_string(getattr(settings, 'PERSONALISATION_SEGMENTS_ADAPTER', 'personalisation.adapters.SessionSegmentsAdapter'))() - diff --git a/src/personalisation/blocks.py b/src/personalisation/blocks.py index b5fa739..48b390c 100644 --- a/src/personalisation/blocks.py +++ b/src/personalisation/blocks.py @@ -1,6 +1,7 @@ from __future__ import absolute_import, unicode_literals from wagtail.wagtailcore import blocks + from personalisation.models import Segment diff --git a/src/personalisation/migrations/0005_userisloggedinrule.py b/src/personalisation/migrations/0005_userisloggedinrule.py index 20e4873..ae75e96 100644 --- a/src/personalisation/migrations/0005_userisloggedinrule.py +++ b/src/personalisation/migrations/0005_userisloggedinrule.py @@ -2,9 +2,9 @@ # Generated by Django 1.10.1 on 2016-12-11 12:15 from __future__ import unicode_literals -from django.db import migrations, models import django.db.models.deletion import modelcluster.fields +from django.db import migrations, models class Migration(migrations.Migration): diff --git a/src/personalisation/migrations/0007_dayrule.py b/src/personalisation/migrations/0007_dayrule.py index b020b08..7d40cfb 100644 --- a/src/personalisation/migrations/0007_dayrule.py +++ b/src/personalisation/migrations/0007_dayrule.py @@ -2,9 +2,9 @@ # Generated by Django 1.10.5 on 2017-01-10 14:35 from __future__ import unicode_literals -from django.db import migrations, models import django.db.models.deletion import modelcluster.fields +from django.db import migrations, models class Migration(migrations.Migration): diff --git a/src/personalisation/migrations/0008_devicerule.py b/src/personalisation/migrations/0008_devicerule.py index df03c18..8f79c9b 100644 --- a/src/personalisation/migrations/0008_devicerule.py +++ b/src/personalisation/migrations/0008_devicerule.py @@ -2,9 +2,9 @@ # Generated by Django 1.10.7 on 2017-04-20 15:47 from __future__ import unicode_literals -from django.db import migrations, models import django.db.models.deletion import modelcluster.fields +from django.db import migrations, models class Migration(migrations.Migration): diff --git a/src/personalisation/rules.py b/src/personalisation/rules.py index ad2b5f0..c61aab3 100644 --- a/src/personalisation/rules.py +++ b/src/personalisation/rules.py @@ -2,13 +2,13 @@ from __future__ import absolute_import, unicode_literals import re from datetime import datetime -from user_agents import parse from django.db import models from django.template.defaultfilters import slugify from django.utils.encoding import python_2_unicode_compatible from django.utils.translation import ugettext_lazy as _ from modelcluster.fields import ParentalKey +from user_agents import parse from wagtail.wagtailadmin.edit_handlers import ( FieldPanel, FieldRowPanel, PageChooserPanel) diff --git a/src/personalisation/utils.py b/src/personalisation/utils.py index 8f80c2b..52c584e 100644 --- a/src/personalisation/utils.py +++ b/src/personalisation/utils.py @@ -1,6 +1,13 @@ import time + def impersonate_other_page(page, other_page): + """ + Function to change the page metadata so the user gets to see the + non-personalized path and page. + :param page: The page to be impersonated + :param other_page: The page it should impersonate + """ page.path = other_page.path page.depth = other_page.depth page.url_path = other_page.url_path @@ -8,6 +15,12 @@ def impersonate_other_page(page, other_page): def create_segment_dictionary(segment): + """ + Creates a dictionary with all the required segment + information. + :param segment: Segment object + :return: Dictionary with name, id, timestamp and persistent state. + """ return { "encoded_name": segment.encoded_name(), "id": segment.pk, diff --git a/src/personalisation/views.py b/src/personalisation/views.py index 8be471e..1500c58 100644 --- a/src/personalisation/views.py +++ b/src/personalisation/views.py @@ -2,6 +2,7 @@ from __future__ import absolute_import, unicode_literals from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, reverse + from personalisation.models import PersonalisablePage, Segment diff --git a/tests/factories/rule.py b/tests/factories/rule.py index 9d6dd30..b4afe87 100644 --- a/tests/factories/rule.py +++ b/tests/factories/rule.py @@ -1,6 +1,7 @@ from __future__ import absolute_import, unicode_literals import datetime + import factory from personalisation import rules @@ -45,4 +46,3 @@ class VisitCountRuleFactory(factory.DjangoModelFactory): class Meta: model = rules.VisitCountRule - diff --git a/tests/unit/test_factories.py b/tests/unit/test_factories.py index 37a5d8f..4c79088 100644 --- a/tests/unit/test_factories.py +++ b/tests/unit/test_factories.py @@ -6,12 +6,10 @@ import pytest from personalisation.models import Segment from personalisation.rules import TimeRule - -from tests.factories.segment import SegmentFactory from tests.factories.rule import ( - QueryRuleFactory, ReferralRuleFactory, TimeRuleFactory, - DayRuleFactory, VisitCountRuleFactory, DeviceRuleFactory) - + DayRuleFactory, DeviceRuleFactory, QueryRuleFactory, ReferralRuleFactory, + TimeRuleFactory, VisitCountRuleFactory) +from tests.factories.segment import SegmentFactory """Factory tests""" @pytest.mark.django_db diff --git a/tests/unit/test_middleware.py b/tests/unit/test_middleware.py index 6400eda..e16748d 100644 --- a/tests/unit/test_middleware.py +++ b/tests/unit/test_middleware.py @@ -8,10 +8,10 @@ from freezegun import freeze_time from wagtail.wagtailcore.models import Page from wagtail_factories import SiteFactory -from tests.factories.segment import SegmentFactory from tests.factories.rule import ( - QueryRuleFactory, ReferralRuleFactory, TimeRuleFactory, - DayRuleFactory, VisitCountRuleFactory, DeviceRuleFactory) + DayRuleFactory, DeviceRuleFactory, QueryRuleFactory, ReferralRuleFactory, + TimeRuleFactory, VisitCountRuleFactory) +from tests.factories.segment import SegmentFactory @pytest.mark.django_db