7

Compare commits

...

71 Commits

Author SHA1 Message Date
acd273c06c Add reverse code 2018-07-19 16:09:55 +02:00
4b3af020fd Fix flake8 errors 2018-07-19 14:33:38 +02:00
05afea8d68 Convert old value status to enabled 2018-07-19 14:26:03 +02:00
c31415b484 Fix migration ordering 2018-07-19 14:17:45 +02:00
4a596d62f2 Fix tests for static/dynamic segments 2018-07-19 14:17:45 +02:00
3c1c0c3306 Fixes tests 2018-07-19 14:17:45 +02:00
937c06cf32 More simple status toggle 2018-07-19 14:17:44 +02:00
d7fac2607b Better migration name 2018-07-19 14:17:44 +02:00
be672f6fde Replaces choice field segment status with boolean 2018-07-19 14:17:44 +02:00
a47803eca5 Delete related variants when deleting the segment (#183)
* Delete related variants when deleting the segment

Closes #155

* Fix typo

* Fix migration ordering

* Split metadata migrations
2018-07-19 12:59:46 +02:00
e42e1a865b Add width and height to logo in README 2018-07-17 17:14:44 +02:00
293004fdc6 Feature/documentation (#170)
* Splits documentation over multiple folders

* Editor guide documentation intro

* Adds segment dashboard documentation

* Adds editor documenation regarding segment creation

* Adds logo with padding for the documentation

* Updates usage guide documentation

* Splits sandbox and custom rules documentation

* Improves ‘Create a variant’ documentation

* Adds documentation regarding streamfield and template tags

* Consistent StreamField references

* Feedback from M. Dingjan

* Remove ‘coming soon’ section

* Enable sandbox debug toolbar

* Updated documentation
2018-07-17 16:59:32 +02:00
6f0425cd5f Merge pull request #184 from wagtail/182-deleting-the-page-deletes-its-variants
Delete page variants when deleting the page
2018-07-09 08:05:26 +02:00
0fd6d4d2e5 Delete variants of a page that is being deleted 2018-07-06 15:11:03 +01:00
e0fbefd53f Update contributors list with all the committers 2018-06-08 18:03:50 +02:00
c87b2936e9 Merge pull request #179 from wagtail/feature/fix-page-summary-item-page-count
Feature/fix page summary item page count
2018-05-31 13:54:58 +02:00
7010f5acea Move comment to top of function 2018-05-31 11:02:39 +02:00
d94890848d Add description of logic 2018-05-31 10:26:17 +02:00
f8d226efaf Prevent corrected summary item from counting the root page 2018-05-31 10:22:22 +02:00
037381f79f Merge pull request #178 from tm-kn/use-the-same-way-as-wagtail-to-count-pages
Use Wagtail's logic in the page count in the dash
2018-05-30 22:26:25 +02:00
0f5501ceef Merge pull request #177 from tm-kn/django-get-field-bug
Fix bug on visiting a segment page in the admin
2018-05-30 22:25:08 +02:00
4e5454b348 Use Wagtail's logic in the page count in the dash
This adds a check if root page is root page in order to calculate the
count properly the same way as Wagtail does [1].

[1] 5c9ff23e22/wagtail/admin/site_summary.py (L38)
2018-05-30 20:13:04 +01:00
9919d76741 Merge pull request #176 from tm-kn/fix-excluding-pages-without-variant
Fix excluding pages without variant
2018-05-30 21:07:32 +02:00
7e9dd8624b Fix bug on visiting a segment page in the admin
Wagtail's InlinePanel's relies on
django.db.models.options.Options.get_field -
bcf6b6da77/wagtail/admin/edit_handlers.py (L688)
which seems to have a bug of using related_query_name rather than
related_name naming and therefore trying to edit segment always
fails - https://code.djangoproject.com/ticket/29458. The bug has not
yet been confirmed by others but it's impossible to edit segments.

Therefore this change deletes related_query_name from relations from
rules to the Segment model.
2018-05-30 20:00:53 +01:00
6514bc1763 Fix excluding pages without variant
Currently count in the admin dashboard shows "-1" and no pages are
displayed in the explorer.
2018-05-30 19:47:28 +01:00
65a46f2bd9 Fix segment model naming for forced_segment and the userbar 2018-05-26 16:37:53 +02:00
f1b62a7546 add missing migration 2018-05-26 16:27:20 +02:00
03e02e8b91 Change all mentions of LabD urls to wagtail urls 2018-05-26 16:26:59 +02:00
8d8975ac36 Merge pull request #126 from wagtail/feature/force-as-segment
adds simple segment forcing for superusers
2018-05-26 16:24:44 +02:00
2324a30afd Merge branch 'feature/djangoconf-sprint' 2018-05-26 16:11:27 +02:00
0bdb80f25a improve SessionSegmentAdapter 2018-05-26 16:04:11 +02:00
1c1a7ce1b8 Remove wagtail icon from segment link in user bar 2018-05-26 15:08:17 +02:00
2a48eb3498 fix django version in tox.ini 2018-05-26 14:59:25 +02:00
4ad097b4fa include wagtail-2.1 in test matrix 2018-05-26 14:57:03 +02:00
939247c147 Add force as segment to the Wagtail user bar 2018-05-26 14:52:09 +02:00
12f110d913 remove customer manager again for now 2018-05-26 14:35:53 +02:00
c8fe62d2b1 remove praekholt deployment target from travis setup 2018-05-26 14:35:53 +02:00
83c2a4289e Adjust README for ordering 2018-05-26 12:56:18 +02:00
84ac76f33e Adjust tox.ini for wagtail 2.1 2018-05-26 12:56:02 +02:00
f6598ca1f7 Adjust requirements 2018-05-26 12:55:29 +02:00
726c0cd70f update travis setup 2018-05-26 12:32:39 +02:00
4f3f9a4d40 lint 2018-05-26 12:28:01 +02:00
3a378830e0 fix basepython 2018-05-26 12:27:52 +02:00
8a151e3bab python2 cleanups 2018-05-26 12:06:35 +02:00
bb34bddaf4 add custom model manager 2018-05-26 12:01:26 +02:00
9710d3b479 post-merge cleanups 2018-05-26 11:45:28 +02:00
5536adc3ec Merge branch 'develop' into feature/djangoconf-sprint 2018-05-26 10:48:33 +02:00
5b8d578493 only test for wagtail2 and django2 on python3 2018-05-26 09:54:56 +02:00
bdba6b65cf use new wagtail_factories package 2018-03-22 14:41:54 +01:00
cbcd80d248 update tox.ini 2018-03-17 11:56:14 +01:00
9b1c5a6ab6 fixes test runs
added dependency link to Makefile until Michael releases new
wagtail-factories
2018-03-17 11:37:11 +01:00
62d258fd9e fixes wagtail2 compatibility
return QuerySets instead of lists
2018-03-17 11:26:56 +01:00
32e73329c3 Revert wagtail-factories setting 2018-03-16 11:51:25 +01:00
fde53ea0ef Fix all tests for django and wagtail 2 2018-03-16 11:45:07 +01:00
22a7367211 Update module paths for tests 2018-03-16 11:16:47 +01:00
0d89d47735 Prevent webpack copy error from img dir 2018-03-16 11:14:19 +01:00
92189a3be8 Fix dashboard edit links 2018-03-16 11:14:19 +01:00
6c9d8b2730 remove typo 2018-03-16 11:14:19 +01:00
e141e5396e make Makefile more portable 2018-03-16 11:14:19 +01:00
c0e2b969e8 Set site ID in sandbox settings 2018-03-16 11:14:19 +01:00
7b5e3d4c9d Fix exampledata 2018-03-16 11:14:19 +01:00
6b7a1ed591 Updated requirements and module paths 2018-03-16 11:14:19 +01:00
9b25cd2a94 Add missing dependency `pytest-pythonpath` 2018-03-16 11:10:45 +01:00
3a86c189dc Merge tag '0.11.3' into develop
Bugfix: Handle errors when testing an invalid visit count rule
2018-03-09 20:35:33 +02:00
82c26f9772 Merge branch 'release/0.11.3' 2018-03-09 20:35:20 +02:00
03eb812e45 Version 0.11.3 2018-03-09 20:35:08 +02:00
e3522d0acb Merge pull request #26 from praekeltfoundation/feature/catch-exceptions-when-visit-count-rule-is-blank
Handle exceptions for empty VisitCountRule
2018-03-09 20:32:05 +02:00
7f5e958ee3 Catch the exception if the visit count rule doesn't have a page 2018-03-09 19:20:30 +02:00
241bfb5240 Merge tag '0.11.2' into develop
Bugfix: Stop populating static segments when the count is reached
2018-03-08 14:00:58 +02:00
9c88ec1582 fixes segment adapter add syntax for segment forcing 2017-06-12 11:12:33 +02:00
785d1486e4 adds simple segment forcing for superusers 2017-06-12 07:56:38 +02:00
84 changed files with 1392 additions and 566 deletions

2
.gitignore vendored
View File

@ -23,3 +23,5 @@ tests/sandbox/assets
node_modules
.DS_Store
.pytest_cache/

View File

@ -4,11 +4,12 @@ language: python
matrix:
include:
- python: 2.7
- python: 3.6
env: lint
- python: 2.7
env: TOXENV=py27-django111-wagtail113
- python: 3.6
env: TOXENV=py36-django20-wagtail20
- python: 3.6
env: TOXENV=py36-django20-wagtail21
install:
- pip install tox codecov
@ -19,13 +20,3 @@ script:
after_success:
- tox -e coverage-report
- codecov
deploy:
provider: pypi
distributions: sdist bdist_wheel
user: praekelt.org
password:
secure: IxPnc95OFNQsl7kFfLlLc38KfHh/W79VXnhEkdb2x1GZznOsG167QlhpAnyXIJysCQxgMMwLMuPOOdk1WIxOpGNM1/M80hNzPAfxMYWPuwposDdwoIc8SyREPJt16BXWAY+rAH8SHxld9p1YdRbOEPSSglODe4dCmQWsCbKjV3aKv+gZxBvf6pMxUglp2fBIlCwrE77MyMYh9iW0AGzD6atC2xN9NgAm4f+2WFlKCUL/MztLhNWdvWEiibQav6Tts7p8tWrsBVzywDW2IOy3O0ihPgRdISZ7QrxhiJTjlHYPAy4eRGOnYSQXvp6Dy8ONE5a6Uv5g3Xw2UmQo85sSMUs2VxR0G7d+PQgL0A7ENBQ5i7eSAFHYs8EswiGilW2A7mM4qRXwg9URLelYSdkM+aNXvR+25dCsXakiO4NjCz/BPgNzxPeQLlBdxR5vHugeM/XYuhy6CHlZrR/uueaO0X8RyhJcqeDjBy58IrwYS3Mpj7QCfBpQ9PqsqXEWV9BKwKiBXM2+3hkhawFDWa0GW2PDbORKtSLy/ORfGLx5Y9qxQYKEGvFQA3iqkTjrLj+KeUziKtuvEAcvsdBIJVIxeoHwdl+qqxEm8A7YuRBnWVxWc3jE6wBXboeFP92gVe/ueoXmY22riK9Ja0pli3TyNga8by9WM8Qp4D2ZqkVXHwg=
on:
tags: true
condition: $TOXENV = py27-django111-wagtail113

View File

@ -1,3 +1,7 @@
0.11.3
==================
- Bugfix: Handle errors when testing an invalid visit count rule
0.11.2
==================
- Bugfix: Stop populating static segments when the count is reached

View File

@ -8,3 +8,13 @@ Contributors
* Michael van Tellingen
* Pim Vernooij
* Tomasz Knapik
* Kaitlyn Crawford
* Todd Dembrey
* Nathan Begbie
* Rob Moorman
* Tom Dyson
* Bertrand Bordage
* Alex Muller
* Saeed Marzban
* Milton Madanda
* Mike Dingjan

View File

@ -1,13 +1,13 @@
.PHONY: all clean requirements develop test lint flake8 isort dist sandbox docs
all: clean requirements dist
default: develop
all: clean requirements dist
clean:
find src -name '*.pyc' -delete
find tests -name '*.pyc' -delete
find . -name '*.egg-info' -delete
find . -name '*.egg-info' |xargs rm -rf
requirements:
pip install --upgrade -e .[docs,test]
@ -38,7 +38,8 @@ isort:
isort --recursive src tests
dist:
./setup.py sdist bdist_wheel
pip install wheel
python ./setup.py sdist bdist_wheel
sandbox:
pip install -r sandbox/requirements.txt

View File

@ -3,17 +3,24 @@
.. image:: https://readthedocs.org/projects/wagtail-personalisation/badge/?version=latest
:target: http://wagtail-personalisation.readthedocs.io/en/latest/?badge=latest
.. image:: https://travis-ci.org/LabD/wagtail-personalisation.svg?branch=master
:target: https://travis-ci.org/LabD/wagtail-personalisation
.. image:: https://travis-ci.org/wagtail/wagtail-personalisation.svg?branch=master
:target: https://travis-ci.org/wagtail/wagtail-personalisation
.. image:: http://codecov.io/github/LabD/wagtail-personalisation/coverage.svg?branch=master
:target: http://codecov.io/github/LabD/wagtail-personalisation?branch=master
.. image:: http://codecov.io/github/wagtail/wagtail-personalisation/coverage.svg?branch=master
:target: http://codecov.io/github/wagtail/wagtail-personalisation?branch=master
.. image:: https://img.shields.io/pypi/v/wagtail-personalisation.svg
:target: https://pypi.python.org/pypi/wagtail-personalisation/
.. end-no-pypi
.. image:: logo.png
:height: 261
:width: 300
:scale: 50
:alt: Wagxperience
:align: center
Wagtail Personalisation
=======================
@ -24,20 +31,17 @@ in the admin interface.
.. _Wagtail CMS: http://wagtail.io/
.. image:: logo.png
:scale: 50 %
:alt: Wagxperience
:align: center
.. image:: screenshot.png
Instructions
------------
Wagtail Personalisation requires Wagtail 1.9 or 1.10 and Django 1.9, 1.10 or 1.11.
Wagtail Personalisation requires Wagtail 2.0 or 2.1 and Django 1.11 or 2.0.
To install the package with pip::
To install the package with pip:
.. code-block:: console
pip install wagtail-personalisation
@ -64,6 +68,16 @@ been added in first, this is a prerequisite for this project.
# ...
]
Documentation
-------------
You can find more information about installing, extending and using this module
on `Read the Docs`_.
.. _Read the Docs: http://wagtail-personalisation.readthedocs.io
Sandbox
-------

BIN
docs/_static/images/dual_streamfield.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

BIN
docs/_static/images/editing_variant.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

BIN
docs/_static/images/variants_button.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

View File

@ -17,10 +17,17 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))
import os
import sys
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
if not on_rtd: # only import and set the theme if we're building docs locally
import sphinx_rtd_theme
html_theme = 'sphinx_rtd_theme'
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
sys.path.insert(0, os.path.abspath('.'))
# -- General configuration ------------------------------------------------
@ -47,7 +54,7 @@ master_doc = 'index'
# General information about the project.
project = 'wagtail-personalisation'
copyright = '2017, Lab Digital BV'
copyright = '2018, Lab Digital BV'
author = 'Lab Digital BV'
# The version info for the project you're documenting, acts as replacement for
@ -55,17 +62,17 @@ author = 'Lab Digital BV'
# built documents.
#
# The short X.Y version.
version = '0.11.2'
version = '0.12.0'
# The full version, including alpha/beta/rc tags.
release = '0.11.2'
release = '0.12.0'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None
# language = None
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
@ -84,7 +91,7 @@ todo_include_todos = False
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'
# html_theme = 'alabaster'
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
@ -92,14 +99,11 @@ html_theme = 'alabaster'
#
# html_theme_options = {}
html_theme_options = {
'github_user': 'LabD',
'github_banner': True,
'github_repo': 'wagtail-personalisation',
'travis_button': True,
'codecov_button': True,
'analytics_id': 'UA-100203499-2',
}
html_logo = 'logo.png'
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".

View File

@ -1,6 +1,10 @@
Included rules
==============
Wagxperience comes with a base set of rules that allow you to start segmenting
your visitors quickly.
Time rule
---------
@ -16,11 +20,12 @@ End time The end time of your time frame.
``wagtail_personalisation.rules.TimeRule``
Day rule
--------
The day rule allows you to segment visitors based on the day of their visit.
Select one or multiple days on which you'd like your segment to be applied.
Select one or multiple days on which you would like your segment to be applied.
================== ==========================================================
Option Description
@ -36,6 +41,7 @@ Sunday Matches when the visitors visits on a sunday.
``wagtail_personalisation.rules.DayRule``
Referral rule
-------------
@ -54,6 +60,7 @@ Regex string The regex string to match the referral header to.
``wagtail_personalisation.rules.ReferralRule``
Visit count rule
----------------
@ -72,6 +79,7 @@ Operator Whether to match for more than, less than or equal to the
``wagtail_personalisation.rules.VisitCountRule``
Query rule
----------
@ -92,6 +100,7 @@ Value The second part of the query ('ourbestoffer').
``wagtail_personalisation.rules.QueryRule``
Device rule
-----------
@ -108,6 +117,7 @@ Desktop Matches when the visitor uses a desktop.
``wagtail_personalisation.rules.DeviceRule``
User is logged in rule
----------------------

View File

@ -0,0 +1,91 @@
Creating personalised content
=============================
Once you've created a segment you can start serving personalised content to your
visitors. To do this, you can choose one of three methods.
1. Create a page variant for a segment.
2. Use StreamField blocks visible for a segment only.
3. Use a template block visible for a segment only.
Method 1: Create a page variant
-------------------------------
**Why you would want to use this method**
* It has absolutely no restrictions, you can change anything you want.
* That's pretty much it.
**Why you would want to use a different method**
* You are editing a page that changes often. You would probably rather not
change the variation(s) every time the original page changes.
To create a variant of a page for a specific Segment (which you can change to
your liking after creating it), simply go to the Explorer section and find the
page you would like to personalize.
.. figure:: ../_static/images/variants_button.png
:alt: The variants button that appears on personalisable pages.
When you hover over a page, you'll notice a "Variants" dropdown button appears.
Click the button and select the segment you would like to create personalised
content for.
Once you've selected the segment, a copy of the original page will be created
with a title that includes the segment. Don't worry, your visitors won't be able
to see this title. It's only there for your reference.
.. figure:: ../_static/images/editing_variant.png
:alt: The newly created page allowing you to change anything you want.
You can change everything on this page you would like. Visitors that are appointed
to your segment will automatically see the new variant you've created for them
when attempting to visit the original page.
Method 2: Use a StreamField block
---------------------------------
Preparing a page and it's StreamField blocks for this method is described in the
Usage guide for developers. Please refer to
:ref:`implementing_streamfield_blocks` for more information.
**Why you would want to use this method**
* Allows you to create personalised content in the original page (without
creating a variant).
* Create multiple StreamField blocks for different segments inline.
**Why you would want to use a different method**
* You need someone tech savvy to change the back-end implementation.
To create personalised StreamField blocks, first select the page you wan't to
create the content for. Note that the personalisable StreamField blocks must be
activated on the page by your developer.
Scroll down to the block containing the StreamField and add a personalisable
block. The first input field in the block is a dropdown allowing you to select
the segment this StreamField block is ment for.
.. figure:: ../_static/images/single_streamfield.png
:alt: Create a new StreamField block and select the segment.
If you want, you can even add multiple blocks and change the segment to show
different content between segments!
.. figure:: ../_static/images/dual_streamfield.png
:alt: You can even create multiple variations of the same block!
Once saved, the page will selectively show StreamField blocks based on the
visitor's segment.
Method 3: Use a template block
------------------------------
Setting up content in this manner is described in the Usage guide for
developers. Please refer to :ref:`implementing_template_blocks` for more
information.

View File

@ -0,0 +1,84 @@
Creating a segment
==================
To create a segment, go to the "Segments dashboard" and click "Add segment".
You can find the segments dashboard in the administration menu on the left of
the page.
.. figure:: ../_static/images/segment_dashboard_header.png
:alt: The segment dashboard header containing the "Add segment" button.
On this page you will be presented with two forms. One with specific information
about your segment, the other allowing you to choose and configure your
rules.
Set segment specific options
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. figure:: ../_static/images/edit_segment_specifics.png
:alt: The form that allows you to configure specific segment options.
1. Enter a name for your segment
Choose something meaningful like "Newsletter campaign visitors". This will
ensure you'll have a general idea which visitors are in this segment in
other parts of the administration interface.
2. Select the status of the segment *Optional*
You will generally keep this one **enabled**. If for some reason you want
to disable the segment, you can change this to **disabled**.
3. Set the segment persistence. *Optional*
When persistence is **enabled**, your segment will stick to the visitor once
applied, even if the rules no longer match the next visit.
4. Select whether to match any or all defined rules. *Optional*
**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.
5. The segment type *Required*
**Dynamic**: Users in this segment will change as more or less meet the
rules specified in the segment.
**Static**: If the segment contains only static compatible rules the segment
will contain the members that pass those rules when the segment is created.
Mixed static segments or those containing entirely non static compatible
rules will be populated using the count variable.
6. The segment count *Optional*
If this number is set for a static segment users will be added to the set
until the number is reached. After this no more users will be added.
7. Randomisation percentage *Optional*
If this number is set each user matching the rules will have this percentage
chance of being placed in the segment.
Defining rules
^^^^^^^^^^^^^^
.. figure:: ../_static/images/edit_segment_rules.png
:alt: The form that allows you to set the rules for a segment.
5. Choose the rules you want to use.
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.
The rules that come with Wagxperience are as follows:
.. toctree::
:maxdepth: 2
../default_rules
Click "save" to store your segment. It will be enabled by default, unless
otherwise defined.

View File

@ -0,0 +1,13 @@
Editor Guide
============
The editor guide is meant for content editors and marketers using Wagxperience
to offer a personalised experience to their visitors.
.. toctree::
:maxdepth: 2
introduction
segments_dashboard
creating_segments
creating_personalised_content

View File

@ -0,0 +1,22 @@
Introduction
============
Wagxperience_ is an open source module developed by `Lab Digital`_ for the
Wagtail_ content management system. It allows editors and marketeers to create
personalised experiences by harnessing the power of segmentation and rules.
.. _Wagxperience: http://wagxperience.io
.. _Wagtail: https://wagtail.io
.. _Lab Digital: http://labdigital.nl
In this guide, we'll take you step by step through the process of offering your
visitors a tailor made online experience. The subjects covered are:
* Using the segments dashboard
* Defining a new segment
* Setting up rules used to match visitors to a segment
* Personalize a page by creating a variant
* Using the StreamField to personalize content blocks
* And even more helpful stuff...
So without further ado, let's get started!

View File

@ -0,0 +1,84 @@
The segments dashboard
======================
Wagxperience comes with two different views for it's segment dashboard. A "list
view" and a "dashboard view". Where the dashboard view attempts to show all
relevant information and statistics in a visually pleasing manner, the list view
is more fitted for sites using large amounts of segments, as it may be
considered more clear in these cases.
Switching between views
-----------------------
By default, Wagxperience's "dashboard view" is active on the segment dashboard.
If you would like to switch between the dashboard view and list view, open the
segment dashboard and click the "Switch view" button in the green header at the
top of the page.
.. figure:: ../_static/images/segment_dashboard_header.png
:alt: The header containing the "Switch view" button.
Using the list view
-------------------
Advantages of using the list view:
* Uses the familiar table view that is used on many other parts of the Wagtail
administration interface.
* Offers a better overview for large amounts of segments.
* Allows for reordering based on fields, such as name or status.
.. figure:: ../_static/images/segment_list_view.png
:alt: The segment list view.
Definitions
^^^^^^^^^^^
Name
The name of your segment.
Persistent
If this is disabled (default), whenever a visitor requests a page, the rules
of this segment are reevaluated. This means that when the rules no longer
match, the visitor is no longer a part of this segment. However, if
persistence is enabled, this segment will "stick" with the visitor, even when
the rules no longer apply.
Match any
If this is disabled (default) all rules of this segment must match a visitor
before the visitor is appointed to this segment. If this is enabled, only 1
rule has to match before the visitor is appointed.
Status
Indicates whether this segment is active (default) or inactive. If it has
been set to 'inactive', visitors will not be appointed to this segment and no
personalised content for this segment will be shown to visitors.
Page count
The amount of pages that have variants using this segment.
Variant count
The total amount of variants for this segment. Does not yet apply, as this
will always match the amount of pages in the "Page count".
Statistics
Shows the amount of visits of this segment and the days it has been
enabled. If the segment is disabled and then re-enabled, these statistics
will reset.
Using the dashboard view
------------------------
Advantages of using the dashboard view:
* Offers a more pleasing visual representation of segments.
* Focused on giving insights about your segments at a glance.
* Shows the actual rules of a segment.
* Gives more wordy explanation about the information shown.
.. figure:: ../_static/images/segment_dashboard_view.png
:alt: The segment dashboard view.

View File

@ -1,32 +0,0 @@
Getting started
===============
Installing Wagxperience
-----------------------
Installing the module
^^^^^^^^^^^^^^^^^^^^^
The Wagxperience app runs in the Wagtail CMS. You can find out more here_.
.. _here: http://docs.wagtail.io/en/latest/getting_started/tutorial.html
1. Install the module::
pip install wagtail-personalisation
2. Add the module and ``wagtail.contrib.modeladmin`` to your ``INSTALLED_APPS``::
INSTALLED_APPS = [
# ...
'wagtail.contrib.modeladmin',
'wagtail_personalisation',
# ...
]
3. Update your database::
python manage.py migrate
Continue reading: :doc:`implementation`

View File

@ -0,0 +1,8 @@
Getting Started
===============
.. toctree::
:maxdepth: 3
installation
sandbox

View File

@ -0,0 +1,37 @@
Installing Wagxperience
=======================
Wagtail Personalisation requires Wagtail_ 2.0 or 2.1 and Django_ 1.11 or 2.0.
.. _Wagtail: https://github.com/wagtail/wagtail
.. _Django: https://github.com/django/django
To install the package with pip:
.. code-block:: console
pip install wagtail-personalisation
Next, include the ``wagtail_personalisation``, ``wagtail.contrib.modeladmin``
and ``wagtailfontawesome`` apps in your project's ``INSTALLED_APPS``:
.. code-block:: python
INSTALLED_APPS = [
# ...
'wagtail.contrib.modeladmin',
'wagtail_personalisation',
'wagtailfontawesome',
# ...
]
Make sure that ``django.contrib.sessions.middleware.SessionMiddleware`` has
been added in first, this is a prerequisite for this project.
.. code-block:: python
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
# ...
]

View File

@ -0,0 +1,14 @@
Using the sandbox
=================
To experiment with the package you can use the sandbox provided in
the repository_. It includes a couple of segments with rules, a personalisable
page with a variant and a personalisable StreamField block.
To install this you will need to create and activate a
virtualenv and then run ``make sandbox``. This will start a fresh Wagtail
install, with the personalisation module enabled, on http://localhost:8000
and http://localhost:8000/cms/. The superuser credentials are
``superuser@example.com`` with the password ``testing``.
.. _repository: https://github.com/LabD/wagtail-personalisation

View File

@ -1,87 +0,0 @@
Implementation
===============
Extending a page to be personalisable
-------------------------------------
Wagxperience offers a ``PersonalisablePage`` base class to extend from.
This is a standard ``Page`` class with personalisation options added.
Creating a new personalisable page
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Import and extend the ``personalisation.models.PersonalisablePage`` class to create a personalisable page.
A very simple example for a personalisable homepage::
from wagtail_personalisation.models import PersonalisablePage
class HomePage(PersonalisablePage):
subtitle = models.CharField(max_length=255)
body = RichTextField(blank=True, default='')
content_panels = PersonalisablePage.content_panels + [
FieldPanel('subtitle'),
FieldPanel('body'),
]
It's just as simple as extending a standard ``Page`` class.
Migrating an existing page to be personalisable
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Creating custom rules
---------------------
Rules consist of two important elements, the model's fields and the ``test_user`` function.
A very simple example of a rule would look something like this::
from django.db import models
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from personalisation import AbstractBaseRule
class MyNewRule(AbstractBaseRule):
field = models.BooleanField(default=False)
panels = [
FieldPanel('field'),
]
def __init__(self, *args, **kwargs):
super(MyNewRule, self).__init__(*args, **kwargs)
def test_user(self, request):
return self.field
As you can see, the only real requirement is the ``test_user`` function that will either return
``True`` or ``False`` based on the model's fields and optionally the request object.
Below is the "Time rule" model included with the module, which offers more complex functionality::
@python_2_unicode_compatible
class TimeRule(AbstractBaseRule):
"""Time rule to segment users based on a start and end time"""
start_time = models.TimeField(_("Starting time"))
end_time = models.TimeField(_("Ending time"))
panels = [
FieldRowPanel([
FieldPanel('start_time'),
FieldPanel('end_time'),
]),
]
def __init__(self, *args, **kwargs):
super(TimeRule, self).__init__(*args, **kwargs)
def test_user(self, request=None):
current_time = datetime.now().time()
starting_time = self.start_time
ending_time = self.end_time
return starting_time <= current_time <= ending_time
def __str__(self):
return 'Time Rule'
Continue reading: :doc:`usage_guide`

View File

@ -3,22 +3,49 @@
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to the Wagxperience documentation!
==========================================
Welcome to the Wagxperience documentation
=========================================
.. image:: https://readthedocs.org/projects/wagtail-personalisation/badge/?version=latest
:target: http://wagtail-personalisation.readthedocs.io/en/latest/?badge=latest
.. image:: https://travis-ci.org/wagtail/wagtail-personalisation.svg?branch=master
:target: https://travis-ci.org/wagtail/wagtail-personalisation
.. image:: http://codecov.io/github/wagtail/wagtail-personalisation/coverage.svg?branch=master
:target: http://codecov.io/github/wagtail/wagtail-personalisation?branch=master
.. image:: https://img.shields.io/pypi/v/wagtail-personalisation.svg
:target: https://pypi.python.org/pypi/wagtail-personalisation/
Wagxperience is a fully-featured personalisation module for Wagtail.
It enables editors to create customised pages - or parts of pages - based on
segments whose rules are configured directly in the admin interface.
* **Get up and running**
* :doc:`getting_started/index`
* **For developers**
* :doc:`usage_guide/index`
* **For editors & marketeers**
* :doc:`editor_guide/index`
Index
-----
.. toctree::
:maxdepth: 2
:caption: Contents:
getting_started
implementation
usage_guide
getting_started/index
usage_guide/index
editor_guide/index
default_rules
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

BIN
docs/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 110 KiB

View File

@ -1,95 +0,0 @@
Usage guide
===========
Creating a segment
------------------
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 and click on "Add a new 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.
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.
4. Define your segment rules.
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.
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.
1. Create a copy of a page for your segment.
2. Create StreamField blocks only visible to your segment.
3. Create a template block only visible to your segment.
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.
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, your 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.
Method 2: Create a StreamField block
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Method 3: Create a template block
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You can add a template block that only shows its contents to users of a
specific segment. This is done using the "segment" block.
When editing templates make sure to load the ``wagtail_personalisation_tags``
tags library in the template::
{% load wagtail_personalisation_tags %}
After that you can add a template block with the name of the segment you want
the content to show up for::
{% segment name="My Segment" %}
<p>Only users within "My Segment" see this!</p>
{% endsegment %}
The template block currently only supports one segment at a time. If you want
to target multiple segments you will have to make multiple blocks with the
same content.

View File

@ -0,0 +1,17 @@
Creating custom rules
=====================
Rules consist of two important elements, the model fields and the
``test_user`` function. They should inherit the ``AbstractBaseRule`` class from
``wagtail_personalisation.rules``.
A simple example of a rule could look something like this:
.. literalinclude:: ../../src/wagtail_personalisation/rules.py
:pyobject: UserIsLoggedInRule
As you can see, the only real requirement is the ``test_user`` function that
will either return ``True`` or ``False`` based on the model fields and
optionally the request object.
That's it!

View File

@ -0,0 +1,71 @@
Implementation
==============
Extending a page to be personalisable
-------------------------------------
Wagxperience offers a ``PersonalisablePage`` base class to extend from.
This is a standard ``Page`` class with personalisation options added.
Creating a new personalisable page
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Import and extend the ``personalisation.models.PersonalisablePage`` class to
create a personalisable page.
A very simple example for a personalisable homepage:
.. code-block:: python
from wagtail.wagtailcore.models import Page
from wagtail_personalisation.models import PersonalisablePageMixin
class HomePage(PersonalisablePageMixin, Page):
pass
All you need is the ``PersonalisablePageMixin`` mixin and a Wagtail ``Page``
class of your liking.
.. _implementing_streamfield_blocks:
Adding personalisable StreamField blocks
----------------------------------------
Taking things a step further, you can also add personalisable StreamField blocks
to your page models. Below is the full Homepage model used in the sandbox.
.. literalinclude:: ../../sandbox/sandbox/apps/home/models.py
.. _implementing_template_blocks:
Using template blocks for personalisation
-----------------------------------------
*Please note that using the personalisable template tag is not the recommended
method for adding personalisation to your content, as it is largely decoupled
from the administration interface. Use responsibly.*
You can add a template block that only shows its contents to users of a
specific segment. This is done using the "segment" block.
When editing templates make sure to load the ``wagtail_personalisation_tags``
tags library in the template:
.. code-block:: jinja
{% load wagtail_personalisation_tags %}
After that you can add a template block with the name of the segment you want
the content to show up for:
.. code-block:: jinja
{% segment name="My Segment" %}
<p>Only users within "My Segment" see this!</p>
{% endsegment %}
The template block currently only supports one segment at a time. If you want
to target multiple segments you will have to make multiple blocks with the
same content.

View File

@ -0,0 +1,8 @@
Usage Guide
===========
.. toctree::
:maxdepth: 3
implementation
custom_rules

0
frontend/img/.gitkeep Normal file
View File

BIN
logo.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 103 KiB

BIN
logo_bw.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

View File

@ -42,12 +42,12 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/LabD/wagtail-personalisation.git"
"url": "git+https://github.com/wagtail/wagtail-personalisation.git"
},
"author": "Lab Digital",
"license": "ISC",
"bugs": {
"url": "https://github.com/LabD/wagtail-personalisation/issues"
"url": "https://github.com/wagtail/wagtail-personalisation/issues"
},
"homepage": "https://github.com/LabD/wagtail-personalisation#readme"
"homepage": "https://github.com/wagtail/wagtail-personalisation#readme"
}

View File

@ -25,7 +25,7 @@
"enable_date": "2017-06-02T10:58:39.389Z",
"disable_date": "2017-06-02T10:34:51.722Z",
"visit_count": 0,
"status": "enabled",
"enabled": true,
"persistent": false,
"match_any": false
}
@ -39,7 +39,7 @@
"enable_date": "2017-06-02T10:57:44.497Z",
"disable_date": "2017-06-02T10:57:39.984Z",
"visit_count": 1,
"status": "enabled",
"enabled": true,
"persistent": false,
"match_any": false
}

View File

@ -1,4 +1,4 @@
Django>=1.11,<1.12
wagtail>=1.10,<1.11
django-debug-toolbar==1.8
Django>=2.0,<2.1
wagtail>=2.1,<2.2
django-debug-toolbar==1.9.1
-e .[docs,test]

View File

@ -3,7 +3,7 @@
from __future__ import unicode_literals
from django.db import migrations
import wagtail.wagtailcore.fields
import wagtail.core.fields
import wagtail_personalisation
@ -17,14 +17,14 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='homepage',
name='intro',
field=wagtail.wagtailcore.fields.RichTextField(
field=wagtail.core.fields.RichTextField(
default='<p>Thank you for trying <a href="http://wagxperience.io" target="_blank">Wagxperience</a>!</p>'),
preserve_default=False,
),
migrations.AddField(
model_name='homepage',
name='body',
field=wagtail.wagtailcore.fields.StreamField((('personalisable_paragraph', wagtail.wagtailcore.blocks.StructBlock((('segment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=wagtail_personalisation.blocks.list_segment_choices, help_text='Only show this content block for users in this segment', label='Personalisation segment', required=False)), ('paragraph', wagtail.wagtailcore.blocks.RichTextBlock())), icon='pilcrow')),), default=''),
field=wagtail.core.fields.StreamField((('personalisable_paragraph', wagtail.core.blocks.StructBlock((('segment', wagtail.core.blocks.ChoiceBlock(choices=wagtail_personalisation.blocks.list_segment_choices, help_text='Only show this content block for users in this segment', label='Personalisation segment', required=False)), ('paragraph', wagtail.core.blocks.RichTextBlock())), icon='pilcrow')),), default=''),
preserve_default=False,
),
]

View File

@ -1,9 +1,9 @@
from __future__ import absolute_import, unicode_literals
from wagtail.wagtailadmin.edit_handlers import RichTextFieldPanel, StreamFieldPanel
from wagtail.wagtailcore import blocks
from wagtail.wagtailcore.fields import RichTextField, StreamField
from wagtail.wagtailcore.models import Page
from wagtail.admin.edit_handlers import RichTextFieldPanel, StreamFieldPanel
from wagtail.core import blocks
from wagtail.core.fields import RichTextField, StreamField
from wagtail.core.models import Page
from wagtail_personalisation.models import PersonalisablePageMixin
from wagtail_personalisation.blocks import PersonalisedStructBlock

View File

@ -3,8 +3,8 @@ from __future__ import absolute_import, unicode_literals
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.shortcuts import render
from wagtail.wagtailcore.models import Page
from wagtail.wagtailsearch.models import Query
from wagtail.core.models import Page
from wagtail.search.models import Query
def search(request):

View File

@ -29,21 +29,30 @@ EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
SITE_ID = 1
# Application definition
INSTALLED_APPS = [
'wagtail.wagtailforms',
'wagtail.wagtailredirects',
'wagtail.wagtailembeds',
'wagtail.wagtailsites',
'wagtail.wagtailusers',
'wagtail.wagtailsnippets',
'wagtail.wagtaildocs',
'wagtail.wagtailimages',
'wagtail.wagtailsearch',
'wagtail.wagtailadmin',
'wagtail.wagtailcore',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.staticfiles',
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',
'wagtail.snippets',
'wagtail.documents',
'wagtail.images',
'wagtail.search',
'wagtail.admin',
'wagtail.core',
'wagtail.contrib.modeladmin',
'wagtailfontawesome',
@ -51,13 +60,6 @@ INSTALLED_APPS = [
'taggit',
'debug_toolbar',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'wagtail_personalisation',
'sandbox.apps.home',
@ -68,17 +70,17 @@ INSTALLED_APPS = [
MIDDLEWARE = [
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.http.ConditionalGetMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'wagtail.wagtailcore.middleware.SiteMiddleware',
'wagtail.wagtailredirects.middleware.RedirectMiddleware',
'wagtail.core.middleware.SiteMiddleware',
'wagtail.contrib.redirects.middleware.RedirectMiddleware',
]
ROOT_URLCONF = 'sandbox.urls'

View File

@ -4,14 +4,14 @@ import debug_toolbar
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtailcore import urls as wagtail_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
from sandbox.apps.search import views as search_views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/', admin.site.urls),
url(r'^cms/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),

View File

@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.11.2
current_version = 0.12.0
commit = true
tag = true
tag_name = {new_version}
@ -21,9 +21,9 @@ exclude =
[wheel]
universal = 1
[coverage:run]
omit =
src/**/migrations/*.py
[coverage]
include = src/**/
omit = src/**/migrations/*.py
[bumpversion:file:setup.py]

View File

@ -2,28 +2,30 @@ import re
from setuptools import find_packages, setup
install_requires = [
'wagtail>=1.10,<1.14',
'user-agents>=1.0.1',
'wagtailfontawesome>=1.0.6',
'wagtail>=2.0,<2.2',
'user-agents>=1.1.0',
'wagtailfontawesome>=1.1.3',
]
tests_require = [
'factory_boy==2.8.1',
'flake8',
'flake8-blind-except',
'flake8-debugger',
'flake8-imports',
'flake8',
'freezegun==0.3.8',
'pytest-cov==2.4.0',
'pytest-cov==2.5.1',
'pytest-django==3.1.2',
'pytest-sugar==0.7.1',
'pytest-pythonpath==0.7.2',
'pytest-sugar==0.9.1',
'pytest==3.4.2',
'wagtail_factories==1.0.0',
'pytest-mock==1.6.3',
'pytest==3.1.0',
'wagtail_factories==0.3.0',
]
docs_require = [
'sphinx>=1.4.0',
'sphinx>=1.7.6',
'sphinx_rtd_theme>=0.4.0',
]
with open('README.rst') as fh:
@ -31,12 +33,12 @@ with open('README.rst') as fh:
'^.. start-no-pypi.*^.. end-no-pypi', '', fh.read(), flags=re.M | re.S)
setup(
name='wagtail-personalisation-molo',
version='0.11.2',
description='A forked version of Wagtail add-on for showing personalized content',
author='Praekelt.org',
author_email='dev@praekeltfoundation.org',
url='https://github.com/praekeltfoundation/wagtail-personalisation/',
name='wagtail-personalisation',
version='0.12.0',
description='A Wagtail add-on for showing personalized content',
author='Lab Digital BV and others',
author_email='opensource@labdigital.nl',
url='https://labdigital.nl/',
install_requires=install_requires,
tests_require=tests_require,
extras_require={
@ -54,16 +56,10 @@ setup(
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Framework :: Django',
'Framework :: Django :: 1.9',
'Framework :: Django :: 1.10',
'Framework :: Django :: 1.11',
'Framework :: Django :: 2',
'Topic :: Internet :: WWW/HTTP :: Site Management',
],
)

View File

@ -9,7 +9,7 @@ from wagtail_personalisation.rules import AbstractBaseRule
from wagtail_personalisation.utils import create_segment_dictionary
class BaseSegmentsAdapter(object):
class BaseSegmentsAdapter:
"""Base segments adapter."""
def __init__(self, request):
@ -66,6 +66,17 @@ class SessionSegmentsAdapter(BaseSegmentsAdapter):
self.request.session.setdefault('segments', [])
self._segment_cache = None
def _segments(self, ids=None):
if not ids:
ids = []
segments = (
Segment.objects
.enabled()
.filter(persistent=True)
.filter(pk__in=ids)
)
return segments
def get_segments(self, key="segments"):
"""Return the persistent segments stored in the request session.
@ -83,16 +94,12 @@ class SessionSegmentsAdapter(BaseSegmentsAdapter):
raw_segments = self.request.session[key]
segment_ids = [segment['id'] for segment in raw_segments]
segments = (
Segment.objects
.enabled()
.filter(persistent=True)
.in_bulk(segment_ids))
segments = self._segments(ids=segment_ids)
retval = [segments[pk] for pk in segment_ids if pk in segments]
result = list(segments)
if key == "segments":
self._segment_cache = retval
return retval
self._segment_cache = result
return result
def set_segments(self, segments, key="segments"):
"""Set the currently active segments
@ -128,9 +135,9 @@ class SessionSegmentsAdapter(BaseSegmentsAdapter):
:rtype: wagtail_personalisation.models.Segment or None
"""
for segment in self.get_segments():
if segment.pk == segment_id:
return segment
segments = self._segments(ids=[segment_id])
if segments.exists():
return segments.get()
def add_page_visit(self, page):
"""Mark the page as visited by the user"""
@ -180,6 +187,9 @@ class SessionSegmentsAdapter(BaseSegmentsAdapter):
current_segments = self.get_segments()
excluded_segments = self.get_segments("excluded_segments")
current_segments = list(
set(current_segments) - set(excluded_segments)
)
# Run tests on all remaining enabled segments to verify applicability.
additional_segments = []
@ -199,11 +209,11 @@ class SessionSegmentsAdapter(BaseSegmentsAdapter):
if result and segment.randomise_into_segment():
if segment.is_static and not segment.is_full:
if self.request.user.is_authenticated():
if self.request.user.is_authenticated:
segment.static_users.add(self.request.user)
additional_segments.append(segment)
elif result:
if segment.is_static and self.request.user.is_authenticated():
if segment.is_static and self.request.user.is_authenticated:
segment.excluded_users.add(self.request.user)
else:
excluded_segments += [segment]

View File

@ -1,7 +1,7 @@
from __future__ import absolute_import, unicode_literals
from django.utils.translation import ugettext_lazy as _
from wagtail.wagtailcore import blocks
from wagtail.core import blocks
from wagtail_personalisation.adapters import get_segment_adapter
from wagtail_personalisation.models import Segment

View File

@ -10,7 +10,7 @@ from django.contrib.staticfiles.templatetags.staticfiles import static
from django.test.client import RequestFactory
from django.utils.lru_cache import lru_cache
from django.utils.translation import ugettext_lazy as _
from wagtail.wagtailadmin.forms import WagtailAdminModelForm
from wagtail.admin.forms import WagtailAdminModelForm
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore

View File

@ -0,0 +1,19 @@
# Generated by Django 2.0.5 on 2018-05-26 14:25
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('wagtail_personalisation', '0018_segment_excluded_users'),
]
operations = [
migrations.AlterField(
model_name='personalisablepagemetadata',
name='segment',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='page_metadata', to='wagtail_personalisation.Segment'),
),
]

View File

@ -0,0 +1,50 @@
# Generated by Django 2.0.5 on 2018-05-30 18:51
import django.db.models.deletion
import modelcluster.fields
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('wagtail_personalisation', '0019_auto_20180526_1425'),
]
operations = [
migrations.AlterField(
model_name='dayrule',
name='segment',
field=modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_dayrules', to='wagtail_personalisation.Segment'),
),
migrations.AlterField(
model_name='devicerule',
name='segment',
field=modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_devicerules', to='wagtail_personalisation.Segment'),
),
migrations.AlterField(
model_name='queryrule',
name='segment',
field=modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_queryrules', to='wagtail_personalisation.Segment'),
),
migrations.AlterField(
model_name='referralrule',
name='segment',
field=modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_referralrules', to='wagtail_personalisation.Segment'),
),
migrations.AlterField(
model_name='timerule',
name='segment',
field=modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_timerules', to='wagtail_personalisation.Segment'),
),
migrations.AlterField(
model_name='userisloggedinrule',
name='segment',
field=modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_userisloggedinrules', to='wagtail_personalisation.Segment'),
),
migrations.AlterField(
model_name='visitcountrule',
name='segment',
field=modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_visitcountrules', to='wagtail_personalisation.Segment'),
),
]

View File

@ -0,0 +1,19 @@
# Generated by Django 2.0.7 on 2018-07-04 15:26
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('wagtail_personalisation', '0020_rules_delete_relatedqueryname'),
]
operations = [
migrations.AlterField(
model_name='personalisablepagemetadata',
name='segment',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, related_name='page_metadata', to='wagtail_personalisation.Segment'),
),
]

View File

@ -0,0 +1,19 @@
# Generated by Django 2.0.7 on 2018-07-05 13:25
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('wagtail_personalisation', '0021_personalisablepagemetadata_segment_set_on_delete_protect'),
]
operations = [
migrations.AlterField(
model_name='personalisablepagemetadata',
name='canonical_page',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, related_name='personalisable_canonical_metadata', to='wagtailcore.Page'),
),
]

View File

@ -0,0 +1,19 @@
# Generated by Django 2.0.5 on 2018-07-19 09:57
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('wagtail_personalisation', '0022_personalisablepagemetadata_canonical_protect'),
]
operations = [
migrations.AlterField(
model_name='personalisablepagemetadata',
name='variant',
field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='_personalisable_page_metadata', to='wagtailcore.Page'),
),
]

View File

@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-08-10 13:48
from __future__ import unicode_literals
from django.db import migrations, models
def forward(apps, schema_editor):
Segment = apps.get_model('wagtail_personalisation', 'Segment')
for segment in Segment.objects.all():
segment.enabled = segment.status == 'enabled'
segment.save()
def backward(apps, schema_editor):
Segment = apps.get_model('wagtail_personalisation', 'Segment')
for segment in Segment.objects.all():
if segment.enabled:
segment.status = 'enabled'
else:
segment.status = 'disabled'
segment.save()
class Migration(migrations.Migration):
dependencies = [
('wagtail_personalisation', '0023_personalisablepagemetadata_variant_cascade'),
]
operations = [
migrations.AddField(
model_name='segment',
name='enabled',
field=models.BooleanField(default=True, help_text='Should the segment be active?'),
),
migrations.RunPython(forward, reverse_code=backward),
migrations.RemoveField(
model_name='segment',
name='status',
),
]

View File

@ -1,4 +1,3 @@
from __future__ import absolute_import, unicode_literals
import random
from django import forms
@ -11,9 +10,9 @@ from django.utils.functional import cached_property
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from modelcluster.models import ClusterableModel
from wagtail.wagtailadmin.edit_handlers import (
from wagtail.admin.edit_handlers import (
FieldPanel, FieldRowPanel, InlinePanel, MultiFieldPanel)
from wagtail.wagtailcore.models import Page
from wagtail.core.models import Page
from wagtail_personalisation.rules import AbstractBaseRule
from wagtail_personalisation.utils import count_active_days
@ -23,20 +22,12 @@ from .forms import SegmentAdminForm
class SegmentQuerySet(models.QuerySet):
def enabled(self):
return self.filter(status=self.model.STATUS_ENABLED)
return self.filter(enabled=True)
@python_2_unicode_compatible
class Segment(ClusterableModel):
"""The segment model."""
STATUS_ENABLED = 'enabled'
STATUS_DISABLED = 'disabled'
STATUS_CHOICES = (
(STATUS_ENABLED, _('Enabled')),
(STATUS_DISABLED, _('Disabled')),
)
TYPE_DYNAMIC = 'dynamic'
TYPE_STATIC = 'static'
@ -51,8 +42,8 @@ class Segment(ClusterableModel):
enable_date = models.DateTimeField(null=True, editable=False)
disable_date = models.DateTimeField(null=True, editable=False)
visit_count = models.PositiveIntegerField(default=0, editable=False)
status = models.CharField(
max_length=20, choices=STATUS_CHOICES, default=STATUS_ENABLED)
enabled = models.BooleanField(
default=True, help_text=_("Should the segment be active?"))
persistent = models.BooleanField(
default=False, help_text=_("Should the segment persist between visits?"))
match_any = models.BooleanField(
@ -112,7 +103,7 @@ class Segment(ClusterableModel):
MultiFieldPanel([
FieldPanel('name', classname="title"),
FieldRowPanel([
FieldPanel('status'),
FieldPanel('enabled'),
FieldPanel('persistent'),
]),
FieldPanel('match_any'),
@ -122,7 +113,7 @@ class Segment(ClusterableModel):
], heading="Segment"),
MultiFieldPanel([
InlinePanel(
"{}_related".format(rule_model._meta.db_table),
"{}s".format(rule_model._meta.db_table),
label='{}{}'.format(
rule_model._meta.verbose_name,
' ({})'.format(_('Static compatible')) if rule_model.static else ''
@ -163,15 +154,11 @@ class Segment(ClusterableModel):
def get_used_pages(self):
"""Return the pages that have variants using this segment."""
pages = list(PersonalisablePageMetadata.objects.filter(segment=self))
return pages
return PersonalisablePageMetadata.objects.filter(segment=self)
def get_created_variants(self):
"""Return the variants using this segment."""
pages = Page.objects.filter(_personalisable_page_metadata__segment=self)
return pages
return Page.objects.filter(_personalisable_page_metadata__segment=self)
def get_rules(self):
"""Retrieve all rules in the segment."""
@ -183,9 +170,7 @@ class Segment(ClusterableModel):
return segment_rules
def toggle(self, save=True):
self.status = (
self.STATUS_ENABLED if self.status == self.STATUS_DISABLED
else self.STATUS_DISABLED)
self.enabled = not self.enabled
if save:
self.save()
@ -208,17 +193,21 @@ class PersonalisablePageMetadata(ClusterableModel):
segments.
"""
# Canonical pages should not ever be deleted if they have variants
# because the variants will be orphaned.
canonical_page = models.ForeignKey(
Page, related_name='personalisable_canonical_metadata',
on_delete=models.SET_NULL,
blank=True, null=True
Page, models.PROTECT, related_name='personalisable_canonical_metadata',
null=True
)
# Delete metadata of the variant if its page gets deleted.
variant = models.OneToOneField(
Page, related_name='_personalisable_page_metadata')
Page, models.CASCADE, related_name='_personalisable_page_metadata',
null=True
)
segment = models.ForeignKey(
Segment, related_name='page_metadata', null=True, blank=True)
segment = models.ForeignKey(Segment, models.PROTECT, null=True,
related_name='page_metadata')
@cached_property
def has_variants(self):
@ -289,7 +278,7 @@ class PersonalisablePageMetadata(ClusterableModel):
return Segment.objects.none()
class PersonalisablePageMixin(object):
class PersonalisablePageMixin:
"""The personalisable page model. Allows creation of variants with linked
segments.

View File

@ -7,16 +7,16 @@ from wagtail_personalisation.models import Segment
def check_status_change(sender, instance, *args, **kwargs):
"""Check if the status has changed. Alter dates accordingly."""
try:
original_status = sender.objects.get(pk=instance.id).status
original_status = sender.objects.get(pk=instance.id).enabled
except sender.DoesNotExist:
original_status = ""
original_status = None
if original_status != instance.status:
if instance.status == instance.STATUS_ENABLED:
if original_status != instance.enabled:
if instance.enabled is True:
instance.enable_date = timezone.now()
instance.visit_count = 0
return instance
if instance.status == instance.STATUS_DISABLED:
if instance.enabled is False:
instance.disable_date = timezone.now()

View File

@ -7,14 +7,15 @@ from importlib import import_module
from django.apps import apps
from django.conf import settings
from django.contrib.sessions.models import Session
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.template.defaultfilters import slugify
from django.test.client import RequestFactory
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from django.test.client import RequestFactory
from modelcluster.fields import ParentalKey
from user_agents import parse
from wagtail.wagtailadmin.edit_handlers import (
from wagtail.admin.edit_handlers import (
FieldPanel, FieldRowPanel, PageChooserPanel)
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
@ -28,8 +29,7 @@ class AbstractBaseRule(models.Model):
segment = ParentalKey(
'wagtail_personalisation.Segment',
related_name="%(app_label)s_%(class)s_related",
related_query_name="%(app_label)s_%(class)ss"
related_name="%(app_label)s_%(class)ss",
)
class Meta:
@ -239,6 +239,12 @@ class VisitCountRule(AbstractBaseRule):
from wagtail_personalisation.adapters import (
get_segment_adapter, SessionSegmentsAdapter, SEGMENT_ADAPTER_CLASS)
# Django formsets don't honour 'required' fields so check rule is valid
try:
self.counted_page
except ObjectDoesNotExist:
return False
if user:
# Create a fake request so we can use the adapter
request = RequestFactory().get('/')

File diff suppressed because one or more lines are too long

View File

@ -22,8 +22,9 @@
<div class="nice-padding block_container">
{% if all_count %}
{% for segment in object_list %}
<div class="block block--{{ segment.status }}" onclick="location.href = '{% url 'wagtail_personalisation_segment_modeladmin_edit' segment.pk %}'">
<div class="block block--{{ segment.enabled|yesno:"enabled,disabled" }}" onclick="location.href = '{% url 'wagtail_personalisation_segment_modeladmin_edit' segment.pk %}'">
<h2>{{ segment }}</h2>
<div class="inspect_container">
<ul class="inspect segment_stats">
<li class="stat_card">
@ -97,10 +98,10 @@
{% if user_can_create %}
<ul class="block_actions">
{% if segment.status == segment.STATUS_DISABLED %}
<li><a href="{% url 'segment:toggle' segment.pk %}" title="{% trans "Enable this segment" %}">enable</a></li>
{% elif segment.status == segment.STATUS_ENABLED %}
{% if segment.enabled %}
<li><a href="{% url 'segment:toggle' segment.pk %}" title="{% trans "Disable this segment" %}">disable</a></li>
{% else %}
<li><a href="{% url 'segment:toggle' segment.pk %}" title="{% trans "Enable this segment" %}">enable</a></li>
{% endif %}
<li><a href="{% url 'wagtail_personalisation_segment_modeladmin_edit' segment.pk %}" title="{% trans "Configure this segment" %}">configure this</a></li>
{% if segment.is_static %}

View File

@ -0,0 +1,49 @@
{% extends "modeladmin/delete.html" %}
{% load i18n modeladmin_tags %}
{% block content_main %}
<div class="nice-padding">
{% if protected_error %}
<h2>{% blocktrans with view.verbose_name|capfirst as model_name %}{{ model_name }} could not be deleted{% endblocktrans %}</h2>
<p>{% blocktrans with instance as instance_name %}'{{ instance_name }}' is currently referenced by other objects, and cannot be deleted without jeopardising data integrity. To delete it successfully, first remove references from the following objects, then try again:{% endblocktrans %}</p>
<ul>
{% for obj in linked_objects %}<li><b>{{ obj|get_content_type_for_obj|title }}:</b> {{ obj }}</li>{% endfor %}
</ul>
<p><a href="{{ view.index_url }}" class="button">{% trans 'Go back to listing' %}</a></p>
{% elif cannot_delete_page_variants_error %}
<h2>{% blocktrans %}Cannot delete all the page variants{% endblocktrans %}</h2>
<p>{% blocktrans %}You need to have permissions to delete the page variants associated with this segment.{% endblocktrans %}
{% else %}
{% with page_variants=view.get_affected_page_objects %}
{% if page_variants %}
<p>
{% blocktrans %}Deleting the segment will also mean deleting all the page variants associated with it. Do you want to continue?{% endblocktrans %}
</p>
<p>
{% blocktrans %}The page objects that <strong>will be deleted</strong> are:{% endblocktrans %}
</p>
<ul>
{% for variant in page_variants %}
<li>
<a href="{% url 'wagtailadmin_explore' variant.pk %}">
{{ variant }}
</a>
</li>
{% endfor %}
</ul>
{% trans 'Yes, delete the segment and associated page variants' as submit_button_value %}
{% else %}
<p>
{% blocktrans %}Do you want to continue deleting this segment?{% endblocktrans %}
</p>
{% trans 'Yes, delete the segment' as submit_button_value %}
{% endif %}
<form action="{{ view.delete_url }}" method="POST">
{% csrf_token %}
<input type="submit" value="{{ submit_button_value }}" class="button serious" />
</form>
{% endwith %}
{% endif %}
</div>
{% endblock %}

View File

@ -0,0 +1,57 @@
{% extends "wagtailadmin/base.html" %}
{% load i18n wagtailadmin_tags %}
{% block content %}
{% trans "Delete" as del_str %}
{% include "wagtailadmin/shared/header.html" with title=del_str subtitle=page.get_admin_display_title icon="doc-empty-inverse" %}
<div class="nice-padding">
<p>
{% trans 'Are you sure you want to delete this page?' %}
{% if descendant_count %}
{% blocktrans count counter=descendant_count %}
This will also delete one more subpage.
{% plural %}
This will also delete {{ descendant_count }} more subpages.
{% endblocktrans %}
{% endif %}
</p>
{% if variants %}
<p>
{% blocktrans count counter=variants|length %}
This page is personalisable. Deleting it will delete its variant:
{% plural %}
This page is personalisable. Deleting it will delete all of its variants:
{% endblocktrans %}
</p>
<ul>
{% for variant in variants %}
<li>
<a href="{% url 'wagtailadmin_explore' variant.pk %}">
{{ variant }}
</a>
</li>
{% endfor %}
</ul>
{% endif %}
<form action="{% url 'wagtailadmin_pages:delete' page.id %}" method="POST">
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">
{% if variants %}
{% trans 'Yes, delete the page and its variants' as submit_button_value %}
{% else %}
{% trans 'Yes, delete it' as submit_button_value %}
{% endif %}
<input type="submit" value="{{ submit_button_value }}" class="button serious">
<a href="{% if next %}{{ next }}{% else %}{% url 'wagtailadmin_explore' page.get_parent.id %}{% endif %}" class="button button-secondary">{% trans "No, don't delete it" %}</a>
</form>
{% page_permissions page as page_perms %}
{% if page_perms.can_unpublish %}
{% url 'wagtailadmin_pages:unpublish' page.id as unpublish_url %}
<p style="margin-top: 1em">{% blocktrans %}Alternatively you can <a href="{{ unpublish_url }}">unpublish the page</a>. This removes the page from public view and you can edit or publish it again later.{% endblocktrans %}</p>
{% endif %}
</div>
{% endblock %}

View File

@ -1,5 +1,6 @@
import time
from django.db.models import F
from django.template.base import FilterExpression, kwarg_re
from django.utils import timezone
@ -98,22 +99,20 @@ 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: Set of pages to check
:type pages: QuerySet
:return: Queryset of pages that aren't variants
:rtype: QuerySet
"""
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
)
)
]
from wagtail_personalisation.models import PersonalisablePageMetadata
excluded_variant_pages = PersonalisablePageMetadata.objects.exclude(
canonical_page_id=F('variant_id')
).values_list('variant_id')
return pages.exclude(pk__in=excluded_variant_pages)
def can_delete_pages(pages, user):
for variant in pages:
if not variant.permissions_for_user(user).can_delete():
return False
return True

View File

@ -1,16 +1,21 @@
from __future__ import absolute_import, unicode_literals
import csv
from django import forms
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseForbidden, HttpResponseRedirect
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.http import (
HttpResponse, HttpResponseForbidden, HttpResponseRedirect)
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from wagtail.contrib.modeladmin.views import IndexView
from wagtail.wagtailcore.models import Page
from wagtail.contrib.modeladmin.views import DeleteView, IndexView
from wagtail.core.models import Page
from wagtail_personalisation.models import Segment
from wagtail_personalisation.utils import can_delete_pages
class SegmentModelIndexView(IndexView):
@ -33,15 +38,52 @@ class SegmentModelDashboardView(IndexView):
]
class SegmentModelDeleteView(DeleteView):
def get_affected_page_objects(self):
return Page.objects.filter(pk__in=(
self.instance.get_used_pages().values_list('variant_id', flat=True)
))
def get_template_names(self):
return [
'modeladmin/wagtail_personalisation/segment/delete.html',
'modeladmin/delete.html',
]
def delete_instance(self):
page_variants = self.get_affected_page_objects()
if not can_delete_pages(page_variants, self.request.user):
raise PermissionDenied(
'User has no permission to delete variant page objects.'
)
# Deleting page objects triggers deletion of the personalisation
# metadata too because of models.CASCADE.
with transaction.atomic():
for variant in page_variants.iterator():
# Delete each one separately so signals are called.
variant.delete()
super().delete_instance()
def post(self, request, *args, **kwargs):
if not can_delete_pages(self.get_affected_page_objects(),
self.request.user):
context = self.get_context_data(
cannot_delete_page_variants_error=True,
)
return self.render_to_response(context)
return super().post(request, *args, **kwargs)
@modeladmin_register
class SegmentModelAdmin(ModelAdmin):
"""The model admin for the Segments administration interface."""
model = Segment
index_view_class = SegmentModelIndexView
dashboard_view_class = SegmentModelDashboardView
delete_view_class = SegmentModelDeleteView
menu_icon = 'fa-snowflake-o'
add_to_settings_menu = False
list_display = ('name', 'persistent', 'match_any', 'status',
list_display = ('name', 'persistent', 'match_any', 'enabled',
'page_count', 'variant_count', 'statistics')
index_view_extra_js = ['js/commons.js', 'js/index.js']
index_view_extra_css = ['css/index.css']

View File

@ -3,14 +3,18 @@ from __future__ import absolute_import, unicode_literals
import logging
from django.conf.urls import include, url
from django.core.urlresolvers import reverse
from django.db import transaction
from django.shortcuts import redirect, render
from django.template.defaultfilters import pluralize
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from wagtail.wagtailadmin.site_summary import PagesSummaryItem, SummaryItem
from wagtail.wagtailadmin.widgets import Button, ButtonWithDropdownFromHook
from wagtail.wagtailcore import hooks
from wagtail.wagtailcore.models import Page
from wagtail.admin import messages
from wagtail.admin.site_summary import PagesSummaryItem, SummaryItem
from wagtail.admin.views.pages import get_valid_next_url_from_request
from wagtail.admin.widgets import Button, ButtonWithDropdownFromHook
from wagtail.core import hooks
from wagtail.core.models import Page
from wagtail_personalisation import admin_urls, models, utils
from wagtail_personalisation.adapters import get_segment_adapter
@ -23,9 +27,7 @@ def register_admin_urls():
"""Adds the administration urls for the personalisation apps."""
return [
url(r'^personalisation/', include(
admin_urls,
app_name='wagtail_personalisation',
namespace='wagtail_personalisation')),
admin_urls, namespace='wagtail_personalisation')),
]
@ -35,7 +37,7 @@ def set_visit_count(page, request, serve_args, serve_kwargs):
to a segment.
:param page: The page being served
:type page: wagtail.wagtailcore.models.Page
:type page: wagtail.core.models.Page
:param request: The http request
:type request: django.http.HttpRequest
@ -49,7 +51,7 @@ def segment_user(page, request, serve_args, serve_kwargs):
"""Apply a segment to a visitor before serving the page.
:param page: The page being served
:type page: wagtail.wagtailcore.models.Page
:type page: wagtail.core.models.Page
:param request: The http request
:type request: django.http.HttpRequest
@ -57,18 +59,42 @@ def segment_user(page, request, serve_args, serve_kwargs):
adapter = get_segment_adapter(request)
adapter.refresh()
forced_segment = request.GET.get('segment', None)
if request.user.is_superuser and forced_segment is not None:
segment = models.Segment.objects.filter(pk=forced_segment).first()
if segment:
adapter.set_segments([segment])
class UserbarSegmentedLinkItem:
def __init__(self, segment):
self.segment = segment
def render(self, request):
return f"""<div class="wagtail-userbar__item">
<a href="{request.path}?segment={self.segment.pk}"
class="wagtail-action">
Show as segment: {self.segment.name}</a></div>"""
@hooks.register('construct_wagtail_userbar')
def add_segment_link_items(request, items):
for item in models.Segment.objects.enabled():
items.append(UserbarSegmentedLinkItem(item))
return items
@hooks.register('before_serve_page')
def serve_variant(page, request, serve_args, serve_kwargs):
"""Apply a segment to a visitor before serving the page.
:param page: The page being served
:type page: wagtail.wagtailcore.models.Page
:type page: wagtail.core.models.Page
:param request: The http request
:type request: django.http.HttpRequest
:returns: A variant if one is available for the visitor's segment,
otherwise the original page
:rtype: wagtail.wagtailcore.models.Page
:rtype: wagtail.core.models.Page
"""
user_segments = []
@ -146,13 +172,24 @@ def page_listing_more_buttons(page, page_perms, is_parent=False):
priority=200)
class CorrectedPagesSummaryPanel(PagesSummaryItem):
class CorrectedPagesSummaryItem(PagesSummaryItem):
def get_context(self):
context = super(CorrectedPagesSummaryPanel, self).get_context()
# Perform the same check as Wagtail to get the correct count.
# Only correct the count when a root page is available to the user.
# The `PagesSummaryItem` will return a page count of 0 otherwise.
# https://github.com/wagtail/wagtail/blob/5c9ff23e229acabad406c42c4e13cbaea32e6c15/wagtail/admin/site_summary.py#L38
context = super().get_context()
root_page = context.get('root_page', None)
if root_page:
pages = utils.exclude_variants(
Page.objects.descendant_of(root_page, inclusive=True))
page_count = pages.count()
pages = utils.exclude_variants(Page.objects.all().specific())
if root_page.is_root():
page_count -= 1
context['total_pages'] = page_count
context['total_pages'] = len(pages) - 1
return context
@ -161,7 +198,7 @@ def add_corrected_pages_summary_panel(request, items):
"""Replaces the Pages summary panel to hide variants."""
for index, item in enumerate(items):
if item.__class__ is PagesSummaryItem:
items[index] = CorrectedPagesSummaryPanel(request)
items[index] = CorrectedPagesSummaryItem(request)
class SegmentSummaryPanel(SummaryItem):
@ -217,3 +254,54 @@ def add_personalisation_summary_panels(request, items):
items.append(SegmentSummaryPanel(request))
items.append(PersonalisedPagesSummaryPanel(request))
items.append(VariantPagesSummaryPanel(request))
@hooks.register('before_delete_page')
def delete_related_variants(request, page):
if not isinstance(page, models.PersonalisablePageMixin) \
or not page.personalisation_metadata.is_canonical:
return
# Get a list of related personalisation metadata for all the related
# variants.
variants_metadata = (
page.personalisation_metadata.variants_metadata
.select_related('variant')
)
next_url = get_valid_next_url_from_request(request)
if request.method == 'POST':
parent_id = page.get_parent().id
variants_metadata = variants_metadata.select_related('variant')
with transaction.atomic():
for metadata in variants_metadata.iterator():
# Call delete() on objects to trigger any signals or hooks.
metadata.variant.delete()
# Delete the page's main variant and the page itself.
page.personalisation_metadata.delete()
page.delete()
msg = _("Page '{0}' and its variants deleted.")
messages.success(
request,
msg.format(page.get_admin_display_title())
)
for fn in hooks.get_hooks('after_delete_page'):
result = fn(request, page)
if hasattr(result, 'status_code'):
return result
if next_url:
return redirect(next_url)
return redirect('wagtailadmin_explore', parent_id)
return render(
request,
'wagtailadmin/pages/wagtail_personalisation/confirm_delete.html', {
'page': page,
'descendant_count': page.get_descendant_count(),
'next': next_url,
'variants': Page.objects.filter(
pk__in=variants_metadata.values_list('variant_id')
)
}
)

View File

@ -9,7 +9,7 @@ pytest_plugins = [
@pytest.fixture(scope='session')
def django_db_setup(django_db_setup, django_db_blocker):
from wagtail.wagtailcore.models import Page, Site
from wagtail.core.models import Page, Site
with django_db_blocker.unblock():
# Remove some initial data that is brought by the tests.site module

View File

@ -8,6 +8,7 @@ from tests.site.pages import models
class ContentPageFactory(PageFactory):
parent = None
title = 'Test page'
slug = factory.LazyAttribute(lambda obj: slugify(obj.title))

View File

@ -7,7 +7,7 @@ from wagtail_personalisation import models
class SegmentFactory(factory.DjangoModelFactory):
name = 'TestSegment'
status = models.Segment.STATUS_ENABLED
enabled = True
class Meta:
model = models.Segment

View File

@ -1,5 +1,5 @@
import factory
from wagtail.wagtailcore.models import Site
from wagtail.core.models import Site
from tests.factories.page import ContentPageFactory

View File

@ -2,9 +2,6 @@ from __future__ import absolute_import, unicode_literals
import os
import django
from pkg_resources import parse_version as V
DATABASES = {
'default': {
'ENGINE': os.environ.get('DATABASE_ENGINE', 'django.db.backends.sqlite3'),
@ -55,38 +52,28 @@ TEMPLATES = [
},
]
MIDDLEWARE = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
def get_middleware_settings():
return (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'wagtail.wagtailcore.middleware.SiteMiddleware',
)
# Django 1.10 started to use "MIDDLEWARE" instead of "MIDDLEWARE_CLASSES".
if V(django.get_version()) < V('1.10'):
MIDDLEWARE_CLASSES = get_middleware_settings()
else:
MIDDLEWARE = get_middleware_settings()
'wagtail.core.middleware.SiteMiddleware',
)
INSTALLED_APPS = (
'wagtail_personalisation',
'wagtail.contrib.modeladmin',
'wagtail.wagtailsearch',
'wagtail.wagtailsites',
'wagtail.wagtailusers',
'wagtail.wagtailimages',
'wagtail.wagtaildocs',
'wagtail.wagtailadmin',
'wagtail.wagtailcore',
'wagtail.search',
'wagtail.sites',
'wagtail.users',
'wagtail.images',
'wagtail.documents',
'wagtail.admin',
'wagtail.core',
'taggit',

View File

@ -3,7 +3,7 @@
from __future__ import unicode_literals
import django.db.models.deletion
import wagtail.wagtailcore.fields
import wagtail.core.fields
from django.db import migrations, models
import wagtail_personalisation.models
@ -23,7 +23,7 @@ class Migration(migrations.Migration):
fields=[
('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), # noqa: E501
('subtitle', models.CharField(blank=True, default='', max_length=255)),
('body', wagtail.wagtailcore.fields.RichTextField(blank=True, default='')),
('body', wagtail.core.fields.RichTextField(blank=True, default='')),
],
options={
'abstract': False,

View File

@ -1,9 +1,8 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-06-02 04:26
from __future__ import unicode_literals
import django.db.models.deletion
import wagtail.wagtailcore.fields
import wagtail.core.fields
from django.db import migrations, models
@ -20,7 +19,7 @@ class Migration(migrations.Migration):
fields=[
('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), # noqa: E501
('subtitle', models.CharField(blank=True, default='', max_length=255)),
('body', wagtail.wagtailcore.fields.RichTextField(blank=True, default='')),
('body', wagtail.core.fields.RichTextField(blank=True, default='')),
],
options={
'abstract': False,

View File

@ -1,9 +1,7 @@
from __future__ import absolute_import, unicode_literals
from django.db import models
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailcore.models import Page
from wagtail.admin.edit_handlers import FieldPanel
from wagtail.core.fields import RichTextField
from wagtail.core.models import Page
from wagtail_personalisation.models import PersonalisablePageMixin

View File

@ -2,12 +2,12 @@ from __future__ import absolute_import, unicode_literals
from django.conf.urls import include, url
from django.contrib import admin
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtailcore import urls as wagtail_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
urlpatterns = [
url(r'^django-admin/', include(admin.site.urls)),
url(r'^django-admin/', admin.site.urls),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),

View File

@ -20,6 +20,23 @@ def test_get_segments(rf):
assert segments == [segment_1, segment_2]
@pytest.mark.django_db
def test_get_segments_session(rf):
request = rf.get('/')
adapter = adapters.SessionSegmentsAdapter(request)
segment_1 = SegmentFactory(name='segment-1', persistent=True)
segment_2 = SegmentFactory(name='segment-2', persistent=True)
adapter.set_segments([segment_1, segment_2])
assert len(request.session['segments']) == 2
adapter._segment_cache = None
segments = adapter.get_segments()
assert segments == [segment_1, segment_2]
@pytest.mark.django_db
def test_get_segment_by_id(rf):
request = rf.get('/')
@ -47,7 +64,7 @@ def test_refresh_removes_disabled(rf):
adapter.set_segments([segment_1, segment_2])
adapter = adapters.SessionSegmentsAdapter(request)
segment_1.status = segment_1.STATUS_DISABLED
segment_1.enabled = False
segment_1.save()
adapter.refresh()

View File

@ -4,7 +4,7 @@ import datetime
import pytest
from tests.factories.rule import ReferralRuleFactory, QueryRuleFactory
from tests.factories.rule import QueryRuleFactory, ReferralRuleFactory
from tests.factories.segment import SegmentFactory
from wagtail_personalisation.models import Segment
from wagtail_personalisation.rules import TimeRule
@ -15,14 +15,14 @@ from wagtail_personalisation.rules import TimeRule
@pytest.mark.django_db
def test_segment_create():
factoried_segment = SegmentFactory()
segment = Segment(name='TestSegment', status='enabled')
segment = Segment(name='TestSegment', enabled=True)
TimeRule(
start_time=datetime.time(8, 0, 0),
end_time=datetime.time(23, 0, 0),
segment=segment)
assert factoried_segment.name == segment.name
assert factoried_segment.status == segment.status
assert factoried_segment.enabled == segment.enabled
@pytest.mark.django_db

View File

@ -3,8 +3,12 @@ from __future__ import absolute_import, unicode_literals
import datetime
import pytest
from django.db.models import ProtectedError
from tests.factories.page import ContentPageFactory
from tests.factories.segment import SegmentFactory
from tests.site.pages import models
from wagtail_personalisation.models import PersonalisablePageMetadata
from wagtail_personalisation.rules import TimeRule
@ -25,3 +29,34 @@ def test_metadata_page_has_variants(segmented_page):
canonical = segmented_page.personalisation_metadata.canonical_page
assert canonical.personalisation_metadata.is_canonical
assert canonical.personalisation_metadata.has_variants
@pytest.mark.django_db
def test_content_page_model():
page = ContentPageFactory()
qs = models.ContentPage.objects.all()
assert page in qs
@pytest.mark.django_db
def test_variant_can_be_deleted_without_error(segmented_page):
segmented_page.delete()
# Make sure the metadata gets deleted because of models.CASCADE.
with pytest.raises(PersonalisablePageMetadata.DoesNotExist):
segmented_page._personalisable_page_metadata.refresh_from_db()
@pytest.mark.django_db
def test_canonical_page_deletion_is_protected(segmented_page):
# When deleting canonical page without deleting variants, it should return
# an error. All variants should be deleted beforehand.
with pytest.raises(ProtectedError):
segmented_page.personalisation_metadata.canonical_page.delete()
@pytest.mark.django_db
def test_page_protection_when_deleting_segment(segmented_page):
segment = segmented_page.personalisation_metadata.segment
assert len(segment.get_used_pages())
with pytest.raises(ProtectedError):
segment.delete()

View File

@ -2,6 +2,7 @@ import pytest
from tests.factories.rule import VisitCountRuleFactory
from tests.factories.segment import SegmentFactory
from wagtail_personalisation.rules import VisitCountRule
@pytest.mark.django_db
@ -25,6 +26,12 @@ def test_visit_count(site, client):
assert visit_count[1]['count'] == 1
@pytest.mark.django_db
def test_call_test_user_on_invalid_rule_fails(site, user, mocker):
rule = VisitCountRule()
assert not (rule.test_user(None, user))
@pytest.mark.django_db
def test_visit_count_call_test_user_with_user(site, client, user):
segment = SegmentFactory(name='VisitCount')

View File

@ -12,7 +12,7 @@ from wagtail_personalisation.rules import TimeRule, VisitCountRule
def form_with_data(segment, *rules):
model_fields = ['type', 'status', 'count', 'name', 'match_any', 'randomisation_percent']
model_fields = ['type', 'enabled', 'count', 'name', 'match_any', 'randomisation_percent']
class TestSegmentAdminForm(SegmentAdminForm):
class Meta:

View File

@ -1,59 +1,38 @@
import pytest
from tests.factories.page import ContentPageFactory
from wagtail_personalisation.utils import (
exclude_variants, impersonate_other_page)
can_delete_pages, impersonate_other_page)
class Page(object):
def __init__(self, path, depth, url_path, title):
self.path = path
self.depth = depth
self.url_path = url_path
self.title = title
def __eq__(self, other):
return self.__dict__ == other.__dict__
@pytest.fixture
def rootpage():
return ContentPageFactory(parent=None, path='/', depth=0, title='root')
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")
impersonate_other_page(page, other_page)
assert page == other_page
@pytest.fixture
def page(rootpage):
return ContentPageFactory(parent=rootpage, path='/hi', title='Hi')
class Metadata(object):
def __init__(self, is_canonical=True):
self.is_canonical = is_canonical
@pytest.fixture
def otherpage(rootpage):
return ContentPageFactory(parent=rootpage, path='/bye', title='Bye')
class PersonalisationMetadataPage(object):
def __init__(self):
self.personalisation_metadata = Metadata()
@pytest.mark.django_db
def test_impersonate_other_page(page, otherpage):
impersonate_other_page(page, otherpage)
assert page.title == otherpage.title == 'Bye'
assert page.path == otherpage.path
def test_exclude_variants_includes_pages_with_no_metadata_property():
page = PersonalisationMetadataPage()
del page.personalisation_metadata
result = exclude_variants([page])
assert result == [page]
@pytest.mark.django_db
def test_can_delete_pages_with_superuser(rf, user, segmented_page):
user.is_superuser = True
assert can_delete_pages([segmented_page], user)
def test_exclude_variants_includes_pages_with_metadata_none():
page = PersonalisationMetadataPage()
page.personalisation_metadata = None
result = exclude_variants([page])
assert result == [page]
def test_exclude_variants_includes_pages_with_metadata_canonical():
page = PersonalisationMetadataPage()
result = exclude_variants([page])
assert result == [page]
def test_exclude_variants_excludes_pages_with_metadata_not_canonical():
page = PersonalisationMetadataPage()
page.personalisation_metadata.is_canonical = False
result = exclude_variants([page])
assert result == []
@pytest.mark.django_db
def test_cannot_delete_pages_with_standard_user(user, segmented_page):
assert not can_delete_pages([segmented_page], user)

View File

@ -1,9 +1,12 @@
import pytest
from django.core.exceptions import PermissionDenied
from django.urls import reverse
from wagtail.core.models import Page
from django.contrib.auth.models import Permission
from django.core.urlresolvers import reverse
from wagtail_personalisation.models import Segment
from wagtail_personalisation.rules import VisitCountRule
from wagtail_personalisation.views import (
SegmentModelDeleteView, SegmentModelAdmin)
@pytest.mark.django_db
@ -25,9 +28,8 @@ def test_segment_user_data_view_requires_admin_access(site, client, django_user_
def test_segment_user_data_view(site, client, mocker, django_user_model):
user1 = django_user_model.objects.create(username='first')
user2 = django_user_model.objects.create(username='second')
admin_user = django_user_model.objects.create(username='admin')
permission = Permission.objects.get(codename='access_admin')
admin_user.user_permissions.add(permission)
admin_user = django_user_model.objects.create(
username='admin', is_superuser=True)
segment = Segment(type=Segment.TYPE_STATIC, count=1)
segment.save()
@ -53,3 +55,56 @@ def test_segment_user_data_view(site, client, mocker, django_user_model):
assert data_lines[0] == 'Username,Visit count - Test page,Visit count - Regular page\r'
assert data_lines[1] == 'first,3,9\r'
assert data_lines[2] == 'second,0,1\r'
@pytest.mark.django_db
def test_segment_delete_view_delete_instance(rf, segmented_page, user):
user.is_superuser = True
user.save()
segment = segmented_page.personalisation_metadata.segment
canonical_page = segmented_page.personalisation_metadata.canonical_page
variants_metadata = segment.get_used_pages()
page_variants = Page.objects.filter(pk__in=(
variants_metadata.values_list('variant_id', flat=True)
))
# Make sure all canonical page, variants and variants metadata exist
assert canonical_page
assert page_variants
assert variants_metadata
# Delete the segment via the method on the view.
request = rf.get('/'.format(segment.pk))
request.user = user
view = SegmentModelDeleteView(
instance_pk=str(segment.pk),
model_admin=SegmentModelAdmin()
)
view.request = request
view.delete_instance()
# Segment has been deleted.
with pytest.raises(segment.DoesNotExist):
segment.refresh_from_db()
# Canonical page stayed intact.
canonical_page.refresh_from_db()
# Variant pages and their metadata have been deleted.
assert not page_variants.all()
assert not variants_metadata.all()
@pytest.mark.django_db
def test_segment_delete_view_raises_permission_denied(rf, segmented_page, user):
segment = segmented_page.personalisation_metadata.segment
request = rf.get('/'.format(segment.pk))
request.user = user
view = SegmentModelDeleteView(
instance_pk=str(segment.pk),
model_admin=SegmentModelAdmin()
)
view.request = request
message = 'User have no permission to delete variant page objects.'
with pytest.raises(PermissionDenied, message=message):
view.delete_instance()

View File

@ -1,4 +1,5 @@
import pytest
from wagtail.core.models import Page
from tests.factories.segment import SegmentFactory
from wagtail_personalisation import adapters, wagtail_hooks
@ -60,3 +61,54 @@ def test_page_listing_more_buttons(site, rf, segmented_page):
result = wagtail_hooks.page_listing_more_buttons(page, [])
items = list(result)
assert len(items) == 3
@pytest.mark.django_db
def test_custom_delete_page_view_does_not_trigger_for_variants(
rf,
segmented_page
):
assert (
wagtail_hooks.delete_related_variants(rf.get('/'), segmented_page)
) is None
@pytest.mark.django_db
def test_custom_delete_page_view_triggers_for_canonical_pages(
rf,
segmented_page
):
assert (
wagtail_hooks.delete_related_variants(
rf.get('/'),
segmented_page.personalisation_metadata.canonical_page
)
) is not None
@pytest.mark.django_db
def test_custom_delete_page_view_deletes_variants(rf, segmented_page, user):
post_request = rf.post('/')
user.is_superuser = True
rf.user = user
canonical_page = segmented_page.personalisation_metadata.canonical_page
canonical_page_variant = canonical_page.personalisation_metadata
assert canonical_page_variant
variants = Page.objects.filter(pk__in=(
canonical_page.personalisation_metadata.variants_metadata.values_list('variant_id', flat=True)
))
variants_metadata = canonical_page.personalisation_metadata.variants_metadata
# Make sure there are variants that exist in the database.
assert len(variants.all())
assert len(variants_metadata.all())
wagtail_hooks.delete_related_variants(
post_request, segmented_page.personalisation_metadata.canonical_page
)
with pytest.raises(canonical_page.DoesNotExist):
canonical_page.refresh_from_db()
with pytest.raises(canonical_page_variant.DoesNotExist):
canonical_page_variant.refresh_from_db()
# Make sure all the variant pages have been deleted.
assert not len(variants.all())
assert not len(variants_metadata.all())

18
tox.ini
View File

@ -1,26 +1,26 @@
[tox]
envlist = py{27}-django{111}-wagtail{113},lint
envlist = py{36}-django{20}-wagtail{20,21},lint
[testenv]
basepython = python3.6
commands = coverage run --parallel -m pytest {posargs}
extras = test
deps =
django111: django>=1.11,<1.12
wagtail19: wagtail>=1.13,<1.14
django20: django>=2.0,<2.1
wagtail20: wagtail>=2.0,<2.1
wagtail21: wagtail>=2.1,<2.2
[testenv:coverage-report]
basepython = python2.7
basepython = python3.6
deps = coverage
pip_pre = true
skip_install = true
commands =
coverage combine
coverage report
coverage report --include="src/**/" --omit="src/**/migrations/*.py"
[testenv:lint]
basepython = python2.7
deps = flake8==3.5.0
basepython = python3.6
deps = flake8
commands =
flake8 src tests setup.py
isort -q --recursive --diff src/ tests/