diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index a8a04a5d..0c3b0317 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -20,67 +20,22 @@ jobs: test: runs-on: ubuntu-latest strategy: - max-parallel: 4 + max-parallel: 5 matrix: - tox_env: - - py36-dj22-wt211 - - py36-dj22-wt212 - - py36-dj22-wt213 - - py37-dj22-wt211 - - py37-dj22-wt212 - - py37-dj22-wt213 - - py38-dj22-wt211 - - py38-dj22-wt212 - - py38-dj22-wt213 - - py37-dj30-wt211 - - py37-dj30-wt212 - - py37-dj30-wt213 - - py38-dj30-wt211 - - py38-dj30-wt212 - - py38-dj30-wt213 - include: - - python-version: 3.6 - tox_env: py36-dj22-wt211 - - python-version: 3.6 - tox_env: py36-dj22-wt212 - - python-version: 3.6 - tox_env: py36-dj22-wt213 - - python-version: 3.7 - tox_env: py37-dj22-wt211 - - python-version: 3.7 - tox_env: py37-dj22-wt212 - - python-version: 3.7 - tox_env: py37-dj22-wt213 - - python-version: 3.8 - tox_env: py38-dj22-wt211 - - python-version: 3.8 - tox_env: py38-dj22-wt212 - - python-version: 3.8 - tox_env: py38-dj22-wt213 - - python-version: 3.7 - tox_env: py37-dj30-wt211 - - python-version: 3.7 - tox_env: py37-dj30-wt212 - - python-version: 3.7 - tox_env: py37-dj30-wt213 - - python-version: 3.8 - tox_env: py38-dj30-wt211 - - python-version: 3.8 - tox_env: py38-dj30-wt212 - - python-version: 3.8 - tox_env: py38-dj30-wt213 + python: ["3.7", "3.8", "3.9", "3.10", "3.11"] + steps: - - uses: actions/checkout@v2 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v1 + - uses: actions/checkout@v3 + - name: Setup Python + uses: actions/setup-python@v4 with: - python-version: ${{ matrix.python-version }} + python-version: ${{ matrix.python }} - name: Install dependencies run: | python -m pip install --upgrade pip - pip install tox tox-gh-actions + python -m pip install tox tox-gh-actions - name: Test with tox - run: tox -e ${{ matrix.tox_env }} --index-url=https://pypi.python.org/simple/ + run: tox - name: Prepare artifacts run: mkdir -p .coverage-data && mv .coverage.* .coverage-data/ - uses: actions/upload-artifact@master diff --git a/.gitignore b/.gitignore index 6c32372b..7ace8224 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ node_modules .DS_Store .pytest_cache/ + +venv/ diff --git a/Makefile b/Makefile index 7b282823..5ba202aa 100644 --- a/Makefile +++ b/Makefile @@ -17,13 +17,13 @@ install: develop develop: clean requirements test: - py.test --nomigrations --reuse-db tests/ + py.test --reuse-db tests/ retest: - py.test --nomigrations --reuse-db tests/ -vvv + py.test --reuse-db tests/ -vvv coverage: - py.test --nomigrations --reuse-db tests/ --cov=wagtail_personalisation --cov-report=term-missing --cov-report=html + py.test --reuse-db tests/ --cov=wagtail_personalisation --cov-report=term-missing --cov-report=html docs: $(MAKE) -C docs html diff --git a/README.rst b/README.rst index 9fb5dc2f..5e11c50f 100644 --- a/README.rst +++ b/README.rst @@ -37,7 +37,7 @@ in the admin interface. Instructions ------------ -Wagtail Personalisation requires Wagtail 2.0 or 2.1 and Django 1.11 or 2.0. +Wagtail Personalisation requires Wagtail 4.1+ and Django 3.2+ To install the package with pip: diff --git a/docs/conf.py b/docs/conf.py index 485ac198..8f09636f 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -20,14 +20,15 @@ import os import sys -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' +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 = "sphinx_rtd_theme" html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, os.path.abspath(".")) # -- General configuration ------------------------------------------------ @@ -41,31 +42,31 @@ extensions = [] # Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] +templates_path = ["_templates"] # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: # # source_suffix = ['.rst', '.md'] -source_suffix = '.rst' +source_suffix = ".rst" # The master toctree document. -master_doc = 'index' +master_doc = "index" # General information about the project. -project = 'wagtail-personalisation' -copyright = '2019, Lab Digital BV' -author = 'Lab Digital BV' +project = "wagtail-personalisation" +copyright = "2019, Lab Digital BV" +author = "Lab Digital BV" # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. -version = '0.15.3' +version = "0.15.3" # The full version, including alpha/beta/rc tags. -release = '0.15.3' +release = "0.15.3" # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. @@ -77,10 +78,10 @@ # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This patterns also effect to html_static_path and html_extra_path -exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] +exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' +pygments_style = "sphinx" # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = False @@ -99,10 +100,10 @@ # # html_theme_options = {} html_theme_options = { - 'analytics_id': 'UA-100203499-2', + "analytics_id": "UA-100203499-2", } -html_logo = 'logo.png' +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, @@ -113,7 +114,7 @@ # -- Options for HTMLHelp output ------------------------------------------ # Output file base name for HTML help builder. -htmlhelp_basename = 'wagtail-personalisationdoc' +htmlhelp_basename = "wagtail-personalisationdoc" # -- Options for LaTeX output --------------------------------------------- @@ -122,15 +123,12 @@ # The paper size ('letterpaper' or 'a4paper'). # # 'papersize': 'letterpaper', - # The font size ('10pt', '11pt' or '12pt'). # # 'pointsize': '10pt', - # Additional stuff for the LaTeX preamble. # # 'preamble': '', - # Latex figure (float) alignment # # 'figure_align': 'htbp', @@ -140,8 +138,13 @@ # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - (master_doc, 'wagtail-personalisation.tex', 'wagtail-personalisation Documentation', - 'Lab Digital BV', 'manual'), + ( + master_doc, + "wagtail-personalisation.tex", + "wagtail-personalisation Documentation", + "Lab Digital BV", + "manual", + ), ] @@ -150,8 +153,13 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - (master_doc, 'wagtail-personalisation', 'wagtail-personalisation Documentation', - [author], 1) + ( + master_doc, + "wagtail-personalisation", + "wagtail-personalisation Documentation", + [author], + 1, + ) ] @@ -161,7 +169,13 @@ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - (master_doc, 'wagtail-personalisation', 'wagtail-personalisation Documentation', - author, 'wagtail-personalisation', 'One line description of project.', - 'Miscellaneous'), + ( + master_doc, + "wagtail-personalisation", + "wagtail-personalisation Documentation", + author, + "wagtail-personalisation", + "One line description of project.", + "Miscellaneous", + ), ] diff --git a/docs/getting_started/installation.rst b/docs/getting_started/installation.rst index baf08407..e5d37aad 100644 --- a/docs/getting_started/installation.rst +++ b/docs/getting_started/installation.rst @@ -1,7 +1,7 @@ Installing Wagxperience ======================= -Wagtail Personalisation requires Wagtail_ 2.0 or 2.1 and Django_ 1.11 or 2.0. +Wagtail Personalisation requires Wagtail_ 4.1+ and Django_ 3.2+ .. _Wagtail: https://github.com/wagtail/wagtail .. _Django: https://github.com/django/django diff --git a/docs/usage_guide/implementation.rst b/docs/usage_guide/implementation.rst index 3c6bad90..58e92f75 100644 --- a/docs/usage_guide/implementation.rst +++ b/docs/usage_guide/implementation.rst @@ -17,7 +17,7 @@ A very simple example for a personalisable homepage: .. code-block:: python - from wagtail.wagtailcore.models import Page + from wagtail.models import Page from wagtail_personalisation.models import PersonalisablePageMixin class HomePage(PersonalisablePageMixin, Page): diff --git a/sandbox/requirements.txt b/sandbox/requirements.txt index 0daa5008..6338c510 100644 --- a/sandbox/requirements.txt +++ b/sandbox/requirements.txt @@ -1,4 +1,4 @@ -Django>=2.2,<2.3 -wagtail>=2.6,<2.7 -django-debug-toolbar==2.0 +Django>=3.2,<4.2 +wagtail>=4.1,<4.3 +django-debug-toolbar -e .[docs,test] diff --git a/sandbox/sandbox/apps/__init__.py b/sandbox/sandbox/apps/__init__.py index 8b137891..e69de29b 100644 --- a/sandbox/sandbox/apps/__init__.py +++ b/sandbox/sandbox/apps/__init__.py @@ -1 +0,0 @@ - diff --git a/sandbox/sandbox/apps/home/migrations/0001_initial.py b/sandbox/sandbox/apps/home/migrations/0001_initial.py index 294ce49d..1dc62bb4 100644 --- a/sandbox/sandbox/apps/home/migrations/0001_initial.py +++ b/sandbox/sandbox/apps/home/migrations/0001_initial.py @@ -2,9 +2,8 @@ # Generated by Django 1.11.1 on 2017-05-31 16:59 from __future__ import unicode_literals -from django.db import migrations, models import django.db.models.deletion -import modelcluster.fields +from django.db import migrations, models class Migration(migrations.Migration): @@ -12,19 +11,29 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('wagtailcore', '0033_remove_golive_expiry_help_text'), - ('wagtail_personalisation', '0011_personalisablepagemetadata'), + ("wagtailcore", "0033_remove_golive_expiry_help_text"), + ("wagtail_personalisation", "0011_personalisablepagemetadata"), ] operations = [ migrations.CreateModel( - name='HomePage', + name="HomePage", 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')), + ( + "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", + ), + ), ], options={ - 'abstract': False, + "abstract": False, }, - bases=('wagtailcore.page', models.Model), + bases=("wagtailcore.page", models.Model), ), ] diff --git a/sandbox/sandbox/apps/home/migrations/0002_create_homepage.py b/sandbox/sandbox/apps/home/migrations/0002_create_homepage.py index 1fdd8671..dd73806a 100644 --- a/sandbox/sandbox/apps/home/migrations/0002_create_homepage.py +++ b/sandbox/sandbox/apps/home/migrations/0002_create_homepage.py @@ -6,10 +6,10 @@ def create_homepage(apps, schema_editor): # Get models - ContentType = apps.get_model('contenttypes.ContentType') - Page = apps.get_model('wagtailcore.Page') - Site = apps.get_model('wagtailcore.Site') - HomePage = apps.get_model('home.HomePage') + ContentType = apps.get_model("contenttypes.ContentType") + Page = apps.get_model("wagtailcore.Page") + Site = apps.get_model("wagtailcore.Site") + HomePage = apps.get_model("home.HomePage") # Delete the default homepage # If migration is run multiple times, it may have already been deleted @@ -17,41 +17,41 @@ def create_homepage(apps, schema_editor): # Create content type for homepage model homepage_content_type, __ = ContentType.objects.get_or_create( - model='homepage', app_label='home') + model="homepage", app_label="home" + ) # Create a new homepage homepage = HomePage.objects.create( title="Home", - slug='home', + slug="home", content_type=homepage_content_type, - path='00010001', + path="00010001", depth=2, numchild=0, - url_path='/home/', + url_path="/home/", ) # Create a site with the new homepage set as the root - Site.objects.create( - hostname='localhost', root_page=homepage, is_default_site=True) + Site.objects.create(hostname="localhost", root_page=homepage, is_default_site=True) def remove_homepage(apps, schema_editor): # Get models - ContentType = apps.get_model('contenttypes.ContentType') - HomePage = apps.get_model('home.HomePage') + ContentType = apps.get_model("contenttypes.ContentType") + HomePage = apps.get_model("home.HomePage") # Delete the default homepage # Page and Site objects CASCADE - HomePage.objects.filter(slug='home', depth=2).delete() + HomePage.objects.filter(slug="home", depth=2).delete() # Delete content type for homepage model - ContentType.objects.filter(model='homepage', app_label='home').delete() + ContentType.objects.filter(model="homepage", app_label="home").delete() class Migration(migrations.Migration): dependencies = [ - ('home', '0001_initial'), + ("home", "0001_initial"), ] operations = [ diff --git a/sandbox/sandbox/apps/home/migrations/0003_homepage_text_content.py b/sandbox/sandbox/apps/home/migrations/0003_homepage_text_content.py index adb5502d..46dc5395 100644 --- a/sandbox/sandbox/apps/home/migrations/0003_homepage_text_content.py +++ b/sandbox/sandbox/apps/home/migrations/0003_homepage_text_content.py @@ -3,28 +3,55 @@ from __future__ import unicode_literals from django.db import migrations -import wagtail.core.fields + +import wagtail.blocks as wagtail_blocks +import wagtail.fields as wagtail_fields + import wagtail_personalisation class Migration(migrations.Migration): dependencies = [ - ('home', '0002_create_homepage'), + ("home", "0002_create_homepage"), ] operations = [ migrations.AddField( - model_name='homepage', - name='intro', - field=wagtail.core.fields.RichTextField( - default='

Thank you for trying Wagxperience!

'), + model_name="homepage", + name="intro", + field=wagtail_fields.RichTextField( + default='

Thank you for trying Wagxperience!

' + ), preserve_default=False, ), migrations.AddField( - model_name='homepage', - name='body', - 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=''), + model_name="homepage", + name="body", + field=wagtail_fields.StreamField( + ( + ( + "personalisable_paragraph", + wagtail_blocks.StructBlock( + ( + ( + "segment", + wagtail_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_blocks.RichTextBlock()), + ), + icon="pilcrow", + ), + ), + ), + default="", + use_json_field=True, + ), preserve_default=False, ), ] diff --git a/sandbox/sandbox/apps/home/models.py b/sandbox/sandbox/apps/home/models.py index fbd05b01..f4909c87 100644 --- a/sandbox/sandbox/apps/home/models.py +++ b/sandbox/sandbox/apps/home/models.py @@ -1,23 +1,32 @@ from __future__ import absolute_import, unicode_literals -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 import blocks +from wagtail.admin.panels import FieldPanel +from wagtail.fields import RichTextField, StreamField +from wagtail.models import Page -from wagtail_personalisation.models import PersonalisablePageMixin from wagtail_personalisation.blocks import PersonalisedStructBlock +from wagtail_personalisation.models import PersonalisablePageMixin class HomePage(PersonalisablePageMixin, Page): intro = RichTextField() - body = StreamField([ - ('personalisable_paragraph', PersonalisedStructBlock([ - ('paragraph', blocks.RichTextBlock()), - ], icon='pilcrow')) - ]) + body = StreamField( + [ + ( + "personalisable_paragraph", + PersonalisedStructBlock( + [ + ("paragraph", blocks.RichTextBlock()), + ], + icon="pilcrow", + ), + ) + ], + use_json_field=True, + ) content_panels = Page.content_panels + [ - RichTextFieldPanel('intro'), - StreamFieldPanel('body'), + FieldPanel("intro"), + FieldPanel("body"), ] diff --git a/sandbox/sandbox/apps/search/views.py b/sandbox/sandbox/apps/search/views.py index 78b16310..73b28498 100644 --- a/sandbox/sandbox/apps/search/views.py +++ b/sandbox/sandbox/apps/search/views.py @@ -3,13 +3,13 @@ from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator from django.shortcuts import render -from wagtail.core.models import Page +from wagtail.models import Page from wagtail.search.models import Query def search(request): - search_query = request.GET.get('query', None) - page = request.GET.get('page', 1) + search_query = request.GET.get("query", None) + page = request.GET.get("page", 1) # Search if search_query: @@ -30,7 +30,11 @@ def search(request): except EmptyPage: search_results = paginator.page(paginator.num_pages) - return render(request, 'search/search.html', { - 'search_query': search_query, - 'search_results': search_results, - }) + return render( + request, + "search/search.html", + { + "search_query": search_query, + "search_results": search_results, + }, + ) diff --git a/sandbox/sandbox/apps/user/admin.py b/sandbox/sandbox/apps/user/admin.py index 02378cdc..66f2a493 100644 --- a/sandbox/sandbox/apps/user/admin.py +++ b/sandbox/sandbox/apps/user/admin.py @@ -12,31 +12,30 @@ class UserAdmin(BaseUserAdmin): # The fields to be used in displaying the User model. # These override the definitions on the base UserAdmin # that reference specific fields on auth.User. - list_display = ['email'] - list_filter = ['is_superuser'] + list_display = ["email"] + list_filter = ["is_superuser"] fieldsets = ( - (None, { - 'fields': ['email', 'password'] - }), - ('Personal info', { - 'fields': ['first_name', 'last_name'] - }), - ('Permissions', { - 'fields': [ - 'is_active', 'is_staff', 'is_superuser', - 'groups', 'user_permissions' - ] - }), + (None, {"fields": ["email", "password"]}), + ("Personal info", {"fields": ["first_name", "last_name"]}), + ( + "Permissions", + { + "fields": [ + "is_active", + "is_staff", + "is_superuser", + "groups", + "user_permissions", + ] + }, + ), ) # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin # overrides get_fieldsets to use this attribute when creating a user. add_fieldsets = ( - (None, { - 'classes': ('wide',), - 'fields': ['email', 'password1', 'password2'] - }), + (None, {"classes": ("wide",), "fields": ["email", "password1", "password2"]}), ) - search_fields = ['first_name', 'last_name', 'email'] - ordering = ['email'] + search_fields = ["first_name", "last_name", "email"] + ordering = ["email"] filter_horizontal = [] diff --git a/sandbox/sandbox/apps/user/forms.py b/sandbox/sandbox/apps/user/forms.py index c5c94cb9..5a10a223 100644 --- a/sandbox/sandbox/apps/user/forms.py +++ b/sandbox/sandbox/apps/user/forms.py @@ -1,6 +1,6 @@ from django import forms from django.contrib.auth.forms import ReadOnlyPasswordHashField -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import gettext_lazy as _ from sandbox.apps.user import models @@ -10,21 +10,22 @@ class UserCreationForm(forms.ModelForm): fields, plus a repeated password. """ + password1 = forms.CharField( - label='Password', widget=forms.PasswordInput, - required=False) + label="Password", widget=forms.PasswordInput, required=False + ) password2 = forms.CharField( - label='Password confirmation', widget=forms.PasswordInput, - required=False) + label="Password confirmation", widget=forms.PasswordInput, required=False + ) class Meta: model = models.User - fields = ['email'] + fields = ["email"] def clean_password2(self): # Check that the two password entries match - password1 = self.cleaned_data.get('password1') - password2 = self.cleaned_data.get('password2') + password1 = self.cleaned_data.get("password1") + password2 = self.cleaned_data.get("password2") if password1 and password2 and password1 != password2: raise forms.ValidationError("Passwords don't match") return password2 @@ -32,7 +33,7 @@ def clean_password2(self): def save(self, commit=True): # Save the provided password in hashed format user = super(UserCreationForm, self).save(commit=False) - user.set_password(self.cleaned_data['password1']) + user.set_password(self.cleaned_data["password1"]) if commit: user.save() return user @@ -44,20 +45,22 @@ class UserChangeForm(forms.ModelForm): password hash display field. """ + password = ReadOnlyPasswordHashField( label=_("Password"), - help_text=_("Raw passwords are not stored, so there is no way to see " - "this user's password, but you can change the password " - "using this form.")) + help_text=_( + "Raw passwords are not stored, so there is no way to see " + "this user's password, but you can change the password " + 'using this form.' + ), + ) class Meta: model = models.User - fields = [ - 'email', 'password', 'is_active', 'is_superuser' - ] + fields = ["email", "password", "is_active", "is_superuser"] def clean_password(self): # Regardless of what the user provides, return the initial value. # This is done here, rather than on the field, because the # field does not have access to the initial value - return self.initial['password'] + return self.initial["password"] diff --git a/sandbox/sandbox/apps/user/migrations/0001_initial.py b/sandbox/sandbox/apps/user/migrations/0001_initial.py index a39c715a..24189fba 100644 --- a/sandbox/sandbox/apps/user/migrations/0001_initial.py +++ b/sandbox/sandbox/apps/user/migrations/0001_initial.py @@ -3,8 +3,8 @@ from __future__ import unicode_literals import django.contrib.auth.models -from django.db import migrations, models import django.utils.timezone +from django.db import migrations, models class Migration(migrations.Migration): @@ -12,32 +12,109 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('auth', '0008_alter_user_username_max_length'), + ("auth", "0008_alter_user_username_max_length"), ] operations = [ migrations.CreateModel( - name='User', + name="User", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('password', models.CharField(max_length=128, verbose_name='password')), - ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), - ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), - ('first_name', models.CharField(blank=True, max_length=100, verbose_name='first name')), - ('last_name', models.CharField(blank=True, max_length=100, verbose_name='last name')), - ('email', models.EmailField(blank=True, max_length=254, unique=True, verbose_name='email address')), - ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), - ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), - ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), - ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')), - ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("password", models.CharField(max_length=128, verbose_name="password")), + ( + "last_login", + models.DateTimeField( + blank=True, null=True, verbose_name="last login" + ), + ), + ( + "is_superuser", + models.BooleanField( + default=False, + help_text="Designates that this user has all permissions without explicitly assigning them.", + verbose_name="superuser status", + ), + ), + ( + "first_name", + models.CharField( + blank=True, max_length=100, verbose_name="first name" + ), + ), + ( + "last_name", + models.CharField( + blank=True, max_length=100, verbose_name="last name" + ), + ), + ( + "email", + models.EmailField( + blank=True, + max_length=254, + unique=True, + verbose_name="email address", + ), + ), + ( + "is_staff", + models.BooleanField( + default=False, + help_text="Designates whether the user can log into this admin site.", + verbose_name="staff status", + ), + ), + ( + "is_active", + models.BooleanField( + default=True, + help_text="Designates whether this user should be treated as active. Unselect this instead of deleting accounts.", # noqa + verbose_name="active", + ), + ), + ( + "date_joined", + models.DateTimeField( + default=django.utils.timezone.now, verbose_name="date joined" + ), + ), + ( + "groups", + models.ManyToManyField( + blank=True, + help_text="The groups this user belongs to. A user will get all permissions granted to each of their groups.", # noqa + related_name="user_set", + related_query_name="user", + to="auth.Group", + verbose_name="groups", + ), + ), + ( + "user_permissions", + models.ManyToManyField( + blank=True, + help_text="Specific permissions for this user.", + related_name="user_set", + related_query_name="user", + to="auth.Permission", + verbose_name="user permissions", + ), + ), ], options={ - 'verbose_name': 'user', - 'verbose_name_plural': 'users', + "verbose_name": "user", + "verbose_name_plural": "users", }, managers=[ - ('objects', django.contrib.auth.models.UserManager()), + ("objects", django.contrib.auth.models.UserManager()), ], ), ] diff --git a/sandbox/sandbox/apps/user/migrations/0002_auto_20190315_1254.py b/sandbox/sandbox/apps/user/migrations/0002_auto_20190315_1254.py index e44269da..dcf316f7 100644 --- a/sandbox/sandbox/apps/user/migrations/0002_auto_20190315_1254.py +++ b/sandbox/sandbox/apps/user/migrations/0002_auto_20190315_1254.py @@ -1,20 +1,21 @@ # Generated by Django 2.1.7 on 2019-03-15 12:54 from django.db import migrations + import sandbox.apps.user.models class Migration(migrations.Migration): dependencies = [ - ('user', '0001_initial'), + ("user", "0001_initial"), ] operations = [ migrations.AlterModelManagers( - name='user', + name="user", managers=[ - ('objects', sandbox.apps.user.models.UserManager()), + ("objects", sandbox.apps.user.models.UserManager()), ], ), ] diff --git a/sandbox/sandbox/apps/user/models.py b/sandbox/sandbox/apps/user/models.py index 458afaee..aa1e6dfc 100644 --- a/sandbox/sandbox/apps/user/models.py +++ b/sandbox/sandbox/apps/user/models.py @@ -1,9 +1,12 @@ from django.contrib.auth.models import ( - AbstractBaseUser, PermissionsMixin, BaseUserManager) + AbstractBaseUser, + BaseUserManager, + PermissionsMixin, +) from django.core.mail import send_mail from django.db import models from django.utils import timezone -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import gettext_lazy as _ class UserManager(BaseUserManager): @@ -14,7 +17,7 @@ def _create_user(self, email, password, **extra_fields): Create and save a user with the given username, email, and password. """ if not email: - raise ValueError('The given email address must be set') + raise ValueError("The given email address must be set") email = self.normalize_email(email) user = self.model(email=email, **extra_fields) user.set_password(password) @@ -22,52 +25,56 @@ def _create_user(self, email, password, **extra_fields): return user def create_user(self, email, password=None, **extra_fields): - extra_fields.setdefault('is_staff', False) - extra_fields.setdefault('is_superuser', False) + extra_fields.setdefault("is_staff", False) + extra_fields.setdefault("is_superuser", False) return self._create_user(email, password, **extra_fields) def create_superuser(self, email, password, **extra_fields): - extra_fields.setdefault('is_staff', True) - extra_fields.setdefault('is_superuser', True) + extra_fields.setdefault("is_staff", True) + extra_fields.setdefault("is_superuser", True) - if extra_fields.get('is_staff') is not True: - raise ValueError('Superuser must have is_staff=True.') - if extra_fields.get('is_superuser') is not True: - raise ValueError('Superuser must have is_superuser=True.') + if extra_fields.get("is_staff") is not True: + raise ValueError("Superuser must have is_staff=True.") + if extra_fields.get("is_superuser") is not True: + raise ValueError("Superuser must have is_superuser=True.") return self._create_user(email, password, **extra_fields) class User(AbstractBaseUser, PermissionsMixin): - """Customized version of the default `AbstractUser` from Django. + """Customized version of the default `AbstractUser` from Django.""" - """ - first_name = models.CharField(_('first name'), max_length=100, blank=True) - last_name = models.CharField(_('last name'), max_length=100, blank=True) - email = models.EmailField(_('email address'), blank=True, unique=True) + first_name = models.CharField(_("first name"), max_length=100, blank=True) + last_name = models.CharField(_("last name"), max_length=100, blank=True) + email = models.EmailField(_("email address"), blank=True, unique=True) is_staff = models.BooleanField( - _('staff status'), default=False, - help_text=_('Designates whether the user can log into this admin ' - 'site.')) + _("staff status"), + default=False, + help_text=_("Designates whether the user can log into this admin " "site."), + ) is_active = models.BooleanField( - _('active'), default=True, - help_text=_('Designates whether this user should be treated as ' - 'active. Unselect this instead of deleting accounts.')) - date_joined = models.DateTimeField(_('date joined'), default=timezone.now) + _("active"), + default=True, + help_text=_( + "Designates whether this user should be treated as " + "active. Unselect this instead of deleting accounts." + ), + ) + date_joined = models.DateTimeField(_("date joined"), default=timezone.now) objects = UserManager() - USERNAME_FIELD = 'email' + USERNAME_FIELD = "email" REQUIRED_FIELDS = [] class Meta: - verbose_name = _('user') - verbose_name_plural = _('users') + verbose_name = _("user") + verbose_name_plural = _("users") def get_full_name(self): """ Returns the first_name plus the last_name, with a space in between. """ - full_name = '%s %s' % (self.first_name, self.last_name) + full_name = "%s %s" % (self.first_name, self.last_name) return full_name.strip() def get_short_name(self): diff --git a/sandbox/sandbox/settings.py b/sandbox/sandbox/settings.py index 1a1b3fac..884cb254 100644 --- a/sandbox/sandbox/settings.py +++ b/sandbox/sandbox/settings.py @@ -22,10 +22,10 @@ DEBUG = True # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = '^anfvx$i7%wts8j=7u1h5ua$w6c76*333(@h)rrjlak1c&x0r+' +SECRET_KEY = "^anfvx$i7%wts8j=7u1h5ua$w6c76*333(@h)rrjlak1c&x0r+" -EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' +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/ @@ -35,90 +35,85 @@ # Application definition INSTALLED_APPS = [ - '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', - 'modelcluster', - 'taggit', - 'debug_toolbar', - - 'wagtail_personalisation', - - 'sandbox.apps.home', - 'sandbox.apps.search', - 'sandbox.apps.user', - + "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", + "wagtail.contrib.modeladmin", + "wagtailfontawesome", + "modelcluster", + "taggit", + "debug_toolbar", + "wagtail_personalisation", + "sandbox.apps.home", + "sandbox.apps.search", + "sandbox.apps.user", ] 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.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', - 'wagtail.contrib.redirects.middleware.RedirectMiddleware', + "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.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", + "wagtail.contrib.redirects.middleware.RedirectMiddleware", ] -if find_spec('wagtail.contrib.legacy'): - MIDDLEWARE += ('wagtail.contrib.legacy.sitemiddleware.SiteMiddleware',) +if find_spec("wagtail.contrib.legacy"): + MIDDLEWARE += ("wagtail.contrib.legacy.sitemiddleware.SiteMiddleware",) else: - MIDDLEWARE += ('wagtail.core.middleware.SiteMiddleware', ) + MIDDLEWARE += ("wagtail.middleware.SiteMiddleware",) -ROOT_URLCONF = 'sandbox.urls' +ROOT_URLCONF = "sandbox.urls" TEMPLATES = [ { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [ - os.path.join(PROJECT_DIR, 'templates'), + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [ + os.path.join(PROJECT_DIR, "templates"), ], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", ], }, }, ] -WSGI_APPLICATION = 'sandbox.wsgi.application' +WSGI_APPLICATION = "sandbox.wsgi.application" -AUTH_USER_MODEL = 'user.User' +AUTH_USER_MODEL = "user.User" # Database # https://docs.djangoproject.com/en/1.11/ref/settings/#databases DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': 'db.sqlite3', + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": "db.sqlite3", } } @@ -126,9 +121,9 @@ # Internationalization # https://docs.djangoproject.com/en/1.11/topics/i18n/ -LANGUAGE_CODE = 'en-us' +LANGUAGE_CODE = "en-us" -TIME_ZONE = 'UTC' +TIME_ZONE = "UTC" USE_I18N = True @@ -141,19 +136,19 @@ # https://docs.djangoproject.com/en/1.11/howto/static-files/ STATICFILES_FINDERS = [ - 'django.contrib.staticfiles.finders.FileSystemFinder', - 'django.contrib.staticfiles.finders.AppDirectoriesFinder', + "django.contrib.staticfiles.finders.FileSystemFinder", + "django.contrib.staticfiles.finders.AppDirectoriesFinder", ] STATICFILES_DIRS = [ - os.path.join(PROJECT_DIR, 'static'), + os.path.join(PROJECT_DIR, "static"), ] -STATIC_ROOT = os.path.join(BASE_DIR, 'static') -STATIC_URL = '/static/' +STATIC_ROOT = os.path.join(BASE_DIR, "static") +STATIC_URL = "/static/" -MEDIA_ROOT = os.path.join(BASE_DIR, 'media') -MEDIA_URL = '/media/' +MEDIA_ROOT = os.path.join(BASE_DIR, "media") +MEDIA_URL = "/media/" # Wagtail settings @@ -162,7 +157,11 @@ # Base URL to use when referring to full URLs within the Wagtail admin backend - # e.g. in notification emails. Don't include '/admin' or a trailing slash -BASE_URL = 'http://example.com' +BASE_URL = "http://example.com" + + +INTERNAL_IPS = ["127.0.0.1"] +DEFAULT_AUTO_FIELD = "django.db.models.AutoField" -INTERNAL_IPS = ['127.0.0.1'] +WAGTAILADMIN_BASE_URL = "http://localhost:8000/admin" diff --git a/sandbox/sandbox/templates/base.html b/sandbox/sandbox/templates/base.html index 79526f42..9dc642d6 100644 --- a/sandbox/sandbox/templates/base.html +++ b/sandbox/sandbox/templates/base.html @@ -20,6 +20,10 @@ + {% comment %} Required in Wagtail v4+ for page previews {% endcomment %} + {% if request.in_preview_panel %} + + {% endif %} {# Global stylesheets #} diff --git a/sandbox/sandbox/urls.py b/sandbox/sandbox/urls.py index 86561a25..1c39bf2a 100644 --- a/sandbox/sandbox/urls.py +++ b/sandbox/sandbox/urls.py @@ -2,27 +2,25 @@ import debug_toolbar from django.conf import settings -from django.conf.urls import include, url from django.contrib import admin +from django.urls import include, re_path + +from wagtail import urls as wagtail_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'^django-admin/', admin.site.urls), - - url(r'^admin/', include(wagtailadmin_urls)), - url(r'^documents/', include(wagtaildocs_urls)), - - url(r'^search/$', search_views.search, name='search'), - + re_path(r"^django-admin/", admin.site.urls), + re_path(r"^admin/", include(wagtailadmin_urls)), + re_path(r"^documents/", include(wagtaildocs_urls)), + re_path(r"^search/$", search_views.search, name="search"), # For anything not caught by a more specific rule above, hand over to # Wagtail's page serving mechanism. This should be the last pattern in # the list: - url(r'', include(wagtail_urls)), - + re_path(r"", include(wagtail_urls)), # Alternatively, if you want Wagtail pages to be served from a subpath # of your site, rather than the site root: # url(r'^pages/', include(wagtail_urls)), @@ -38,5 +36,5 @@ urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns = [ - url(r'^__debug__/', include(debug_toolbar.urls)), + re_path(r"^__debug__/", include(debug_toolbar.urls)), ] + urlpatterns diff --git a/setup.cfg b/setup.cfg index c4d818f6..8c946bae 100644 --- a/setup.cfg +++ b/setup.cfg @@ -13,11 +13,14 @@ testpaths = tests python_paths = . [flake8] -ignore = E731 +ignore = E731,W503 max-line-length = 120 exclude = src/**/migrations/*.py +[isort] +profile = black + [wheel] universal = 1 diff --git a/setup.py b/setup.py index c92e5628..0d656cae 100644 --- a/setup.py +++ b/setup.py @@ -1,66 +1,76 @@ import re + from setuptools import find_packages, setup install_requires = [ - 'wagtail>=2.0', - 'user-agents>=1.1.0', - 'wagtailfontawesome>=1.1.3', - 'pycountry', + "wagtail>=4.1", + "user-agents>=1.1.0", + "wagtailfontawesome>=1.2.1", + "pycountry", ] tests_require = [ - 'factory_boy==2.8.1', - 'flake8-blind-except', - 'flake8-debugger', - 'flake8-isort', - 'flake8', - 'freezegun==0.3.8', - 'pytest-cov==2.5.1', - 'pytest-django==4.1.0', - 'pytest-pythonpath==0.7.2', - 'pytest-sugar==0.9.1', - 'pytest==6.1.2', - 'wagtail_factories==1.1.0', - 'pytest-mock==1.6.3', + "factory_boy==3.2.1", + "flake8-blind-except", + "flake8-debugger", + "flake8-isort", + "flake8", + "freezegun==1.2.1", + "pytest-cov==3.0.0", + "pytest-django==4.5.2", + "pytest-pythonpath==0.7.4", + "pytest-sugar==0.9.4", + "pytest==6.2.5", + "wagtail_factories==4.0.0", + "pytest-mock==3.8.1", ] docs_require = [ - 'sphinx>=1.7.6', - 'sphinx_rtd_theme>=0.4.0', + "sphinx>=1.7.6", + "sphinx_rtd_theme>=0.4.0", ] -with open('README.rst') as fh: +with open("README.rst") as fh: long_description = re.sub( - '^.. start-no-pypi.*^.. end-no-pypi', '', fh.read(), flags=re.M | re.S) + "^.. start-no-pypi.*^.. end-no-pypi", "", fh.read(), flags=re.M | re.S + ) setup( - name='wagtail-personalisation', - version='0.15.3', - description='A Wagtail add-on for showing personalized content', - author='Lab Digital BV and others', - author_email='opensource@labdigital.nl', - url='https://labdigital.nl/', + name="wagtail-personalisation", + version="0.15.3", + 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={ - 'docs': docs_require, - 'test': tests_require, + "docs": docs_require, + "test": tests_require, }, - packages=find_packages('src'), - package_dir={'': 'src'}, + packages=find_packages("src"), + package_dir={"": "src"}, include_package_data=True, - license='MIT', + license="MIT", long_description=long_description, classifiers=[ - 'Development Status :: 5 - Production/Stable', - 'Environment :: Web Environment', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: BSD License', - 'Operating System :: OS Independent', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.6', - 'Framework :: Django', - 'Framework :: Django :: 2.0', - 'Topic :: Internet :: WWW/HTTP :: Site Management', + "Development Status :: 5 - Production/Stable", + "Environment :: Web Environment", + "Intended Audience :: Developers", + "License :: OSI Approved :: BSD License", + "Operating System :: OS Independent", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Framework :: Django", + "Framework :: Django :: 3.2", + "Framework :: Django :: 4.0", + "Framework :: Django :: 4.1", + "Framework :: Wagtail", + "Framework :: Wagtail :: 4", + "Topic :: Internet :: WWW/HTTP :: Site Management", ], ) diff --git a/src/wagtail_personalisation/__init__.py b/src/wagtail_personalisation/__init__.py index 3c0c97d7..9dd7e9c1 100644 --- a/src/wagtail_personalisation/__init__.py +++ b/src/wagtail_personalisation/__init__.py @@ -1 +1 @@ -default_app_config = 'wagtail_personalisation.config.WagtailPersonalisationConfig' +default_app_config = "wagtail_personalisation.config.WagtailPersonalisationConfig" diff --git a/src/wagtail_personalisation/adapters.py b/src/wagtail_personalisation/adapters.py index 509c5c78..fc582729 100644 --- a/src/wagtail_personalisation/adapters.py +++ b/src/wagtail_personalisation/adapters.py @@ -61,18 +61,13 @@ class SessionSegmentsAdapter(BaseSegmentsAdapter): def __init__(self, request): super(SessionSegmentsAdapter, self).__init__(request) - self.request.session.setdefault('segments', []) + 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) - ) + segments = Segment.objects.enabled().filter(persistent=True).filter(pk__in=ids) return segments def get_segments(self, key="segments"): @@ -90,7 +85,7 @@ def get_segments(self, key="segments"): if key not in self.request.session: return [] raw_segments = self.request.session[key] - segment_ids = [segment['id'] for segment in raw_segments] + segment_ids = [segment["id"] for segment in raw_segments] segments = self._segments(ids=segment_ids) @@ -113,7 +108,7 @@ def set_segments(self, segments, key="segments"): segment_ids = set() for segment in segments: serialized = create_segment_dictionary(segment) - if serialized['id'] in segment_ids: + if serialized["id"] in segment_ids: continue cache_segments.append(segment) @@ -139,41 +134,44 @@ def get_segment_by_id(self, segment_id): def add_page_visit(self, page): """Mark the page as visited by the user""" - visit_count = self.request.session.setdefault('visit_count', []) - page_visits = [visit for visit in visit_count if visit['id'] == page.pk] + visit_count = self.request.session.setdefault("visit_count", []) + page_visits = [visit for visit in visit_count if visit["id"] == page.pk] if page_visits: for page_visit in page_visits: - page_visit['count'] += 1 - page_visit['path'] = page.url_path if page else self.request.path + page_visit["count"] += 1 + page_visit["path"] = page.url_path if page else self.request.path self.request.session.modified = True else: - visit_count.append({ - 'slug': page.slug, - 'id': page.pk, - 'path': page.url_path if page else self.request.path, - 'count': 1, - }) + visit_count.append( + { + "slug": page.slug, + "id": page.pk, + "path": page.url_path if page else self.request.path, + "count": 1, + } + ) def get_visit_count(self, page=None): """Return the number of visits on the current request or given page""" path = page.url_path if page else self.request.path - visit_count = self.request.session.setdefault('visit_count', []) + visit_count = self.request.session.setdefault("visit_count", []) for visit in visit_count: - if visit['path'] == path: - return visit['count'] + if visit["path"] == path: + return visit["count"] return 0 def update_visit_count(self): """Update the visit count for all segments in the request session.""" - segments = self.request.session['segments'] - segment_pks = [s['id'] for s in segments] + segments = self.request.session["segments"] + segment_pks = [s["id"] for s in segments] # Update counts - (Segment.objects - .enabled() + ( + Segment.objects.enabled() .filter(pk__in=segment_pks) - .update(visit_count=F('visit_count') + 1)) + .update(visit_count=F("visit_count") + 1) + ) def refresh(self): """Retrieve the request session segments and verify whether or not they @@ -185,27 +183,31 @@ def refresh(self): current_segments = self.get_segments() excluded_segments = self.get_segments("excluded_segments") - current_segments = list( - set(current_segments) - set(excluded_segments) - ) + current_segments = list(set(current_segments) - set(excluded_segments)) # Run tests on all remaining enabled segments to verify applicability. additional_segments = [] for segment in enabled_segments: - if segment.is_static and segment.static_users.filter(id=self.request.user.id).exists(): + if ( + segment.is_static + and segment.static_users.filter(id=self.request.user.id).exists() + ): additional_segments.append(segment) - elif any(( - segment.excluded_users.filter(id=self.request.user.id).exists(), - segment in excluded_segments - )): + elif any( + ( + segment.excluded_users.filter(id=self.request.user.id).exists(), + segment in excluded_segments, + ) + ): continue elif not segment.is_static or not segment.is_full: segment_rules = [] for rule_model in rule_models: segment_rules.extend(rule_model.objects.filter(segment=segment)) - result = self._test_rules(segment_rules, self.request, - match_any=segment.match_any) + result = self._test_rules( + segment_rules, self.request, match_any=segment.match_any + ) if result and segment.randomise_into_segment(): if segment.is_static and not segment.is_full: @@ -223,14 +225,17 @@ def refresh(self): self.update_visit_count() -SEGMENT_ADAPTER_CLASS = import_string(getattr( - settings, - 'PERSONALISATION_SEGMENTS_ADAPTER', - 'wagtail_personalisation.adapters.SessionSegmentsAdapter')) +SEGMENT_ADAPTER_CLASS = import_string( + getattr( + settings, + "PERSONALISATION_SEGMENTS_ADAPTER", + "wagtail_personalisation.adapters.SessionSegmentsAdapter", + ) +) def get_segment_adapter(request): """Return the Segment Adapter for the given request""" - if not hasattr(request, 'segment_adapter'): + if not hasattr(request, "segment_adapter"): request.segment_adapter = SEGMENT_ADAPTER_CLASS(request) return request.segment_adapter diff --git a/src/wagtail_personalisation/admin.py b/src/wagtail_personalisation/admin.py index 3ff20ba3..fd6f9ab8 100644 --- a/src/wagtail_personalisation/admin.py +++ b/src/wagtail_personalisation/admin.py @@ -8,6 +8,7 @@ class UserIsLoggedInRuleAdminInline(admin.TabularInline): administration interface for segments. """ + model = rules.UserIsLoggedInRule @@ -16,6 +17,7 @@ class TimeRuleAdminInline(admin.TabularInline): administration interface for segments. """ + model = rules.TimeRule @@ -24,6 +26,7 @@ class ReferralRuleAdminInline(admin.TabularInline): administration interface for segments. """ + model = rules.ReferralRule @@ -32,13 +35,19 @@ class VisitCountRuleAdminInline(admin.TabularInline): administration interface for segments. """ + model = rules.VisitCountRule class SegmentAdmin(admin.ModelAdmin): """Add the inline models to the Segment admin interface.""" - inlines = (UserIsLoggedInRuleAdminInline, TimeRuleAdminInline, - ReferralRuleAdminInline, VisitCountRuleAdminInline) + + inlines = ( + UserIsLoggedInRuleAdminInline, + TimeRuleAdminInline, + ReferralRuleAdminInline, + VisitCountRuleAdminInline, + ) admin.site.register(models.Segment, SegmentAdmin) diff --git a/src/wagtail_personalisation/admin_urls.py b/src/wagtail_personalisation/admin_urls.py index f6c226a8..8a237e6a 100644 --- a/src/wagtail_personalisation/admin_urls.py +++ b/src/wagtail_personalisation/admin_urls.py @@ -1,16 +1,24 @@ -from django.conf.urls import url +from django.urls import re_path from wagtail_personalisation import views -app_name = 'segment' +app_name = "segment" urlpatterns = [ - url(r'^segment/(?P[0-9]+)/toggle/$', - views.toggle, name='toggle'), - url(r'^(?P[0-9]+)/copy/(?P[0-9]+)$', - views.copy_page_view, name='copy_page'), - url(r'^segment/toggle_segment_view/$', - views.toggle_segment_view, name='toggle_segment_view'), - url(r'^segment/users/(?P[0-9]+)$', - views.segment_user_data, name='segment_user_data'), + re_path(r"^segment/(?P[0-9]+)/toggle/$", views.toggle, name="toggle"), + re_path( + r"^(?P[0-9]+)/copy/(?P[0-9]+)$", + views.copy_page_view, + name="copy_page", + ), + re_path( + r"^segment/toggle_segment_view/$", + views.toggle_segment_view, + name="toggle_segment_view", + ), + re_path( + r"^segment/users/(?P[0-9]+)$", + views.segment_user_data, + name="segment_user_data", + ), ] diff --git a/src/wagtail_personalisation/blocks.py b/src/wagtail_personalisation/blocks.py index 85e148e6..7cb1ca32 100644 --- a/src/wagtail_personalisation/blocks.py +++ b/src/wagtail_personalisation/blocks.py @@ -1,5 +1,5 @@ -from django.utils.translation import ugettext_lazy as _ -from wagtail.core import blocks +from django.utils.translation import gettext_lazy as _ +from wagtail import blocks from wagtail_personalisation.adapters import get_segment_adapter from wagtail_personalisation.models import Segment @@ -7,7 +7,7 @@ def list_segment_choices(): yield -1, ("Show to everyone") - for pk, name in Segment.objects.values_list('pk', 'name'): + for pk, name in Segment.objects.values_list("pk", "name"): yield pk, name @@ -16,8 +16,10 @@ class PersonalisedStructBlock(blocks.StructBlock): segment = blocks.ChoiceBlock( choices=list_segment_choices, - required=False, label=_("Personalisation segment"), - help_text=_("Only show this content block for users in this segment")) + required=False, + label=_("Personalisation segment"), + help_text=_("Only show this content block for users in this segment"), + ) def render(self, value, context=None): """Only render this content block for users in this segment. @@ -30,23 +32,21 @@ def render(self, value, context=None): :rtype: blocks.StructBlock or empty str """ - request = context['request'] + request = context["request"] adapter = get_segment_adapter(request) user_segments = adapter.get_segments() try: - segment_id = int(value['segment']) + segment_id = int(value["segment"]) except (ValueError, TypeError): - return '' + return "" if segment_id > 0: for segment in user_segments: if segment.id == segment_id: - return super(PersonalisedStructBlock, self).render( - value, context) + return super(PersonalisedStructBlock, self).render(value, context) if segment_id == -1: - return super(PersonalisedStructBlock, self).render( - value, context) + return super(PersonalisedStructBlock, self).render(value, context) - return '' + return "" diff --git a/src/wagtail_personalisation/config.py b/src/wagtail_personalisation/config.py index baf1d073..c7b00336 100644 --- a/src/wagtail_personalisation/config.py +++ b/src/wagtail_personalisation/config.py @@ -1,11 +1,12 @@ from django.apps import AppConfig -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import gettext_lazy as _ class WagtailPersonalisationConfig(AppConfig): - label = 'wagtail_personalisation' - name = 'wagtail_personalisation' - verbose_name = _('Wagtail Personalisation') + label = "wagtail_personalisation" + name = "wagtail_personalisation" + verbose_name = _("Wagtail Personalisation") + default_auto_field = "django.db.models.AutoField" def ready(self): from wagtail_personalisation import receivers diff --git a/src/wagtail_personalisation/forms.py b/src/wagtail_personalisation/forms.py index 65e9ed85..c5cfbb73 100644 --- a/src/wagtail_personalisation/forms.py +++ b/src/wagtail_personalisation/forms.py @@ -1,5 +1,5 @@ -from datetime import datetime import functools +from datetime import datetime from importlib import import_module from django.conf import settings @@ -7,7 +7,7 @@ from django.contrib.auth.models import AnonymousUser from django.templatetags.static import static from django.test.client import RequestFactory -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import gettext_lazy as _ from wagtail.admin.forms import WagtailAdminModelForm SessionStore = import_module(settings.SESSION_ENGINE).SessionStore @@ -23,10 +23,8 @@ def user_from_data(user_id): class SegmentAdminForm(WagtailAdminModelForm): - def count_matching_users(self, rules, match_any): - """ Calculates how many users match the given static rules - """ + """Calculates how many users match the given static rules""" count = 0 static_rules = [rule for rule in rules if rule.static] @@ -51,18 +49,28 @@ def clean(self): Segment = self._meta.model rules = [ - form.instance for formset in self.formsets.values() + form.instance + for formset in self.formsets.values() for form in formset if form not in formset.deleted_forms ] consistent = rules and Segment.all_static(rules) - if cleaned_data.get('type') == Segment.TYPE_STATIC and not cleaned_data.get('count') and not consistent: - self.add_error('count', _('Static segments with non-static compatible rules must include a count.')) + if ( + cleaned_data.get("type") == Segment.TYPE_STATIC + and not cleaned_data.get("count") + and not consistent + ): + self.add_error( + "count", + _( + "Static segments with non-static compatible rules must include a count." + ), + ) if self.instance.id and self.instance.is_static: if self.has_changed(): - self.add_error_to_fields(self, excluded=['name', 'enabled']) + self.add_error_to_fields(self, excluded=["name", "enabled"]) for formset in self.formsets.values(): if formset.has_changed(): @@ -75,7 +83,7 @@ def clean(self): def add_error_to_fields(self, form, excluded=list()): for field in form.changed_data: if field not in excluded: - form.add_error(field, _('Cannot update a static segment')) + form.add_error(field, _("Cannot update a static segment")) def save(self, *args, **kwargs): is_new = not self.instance.id @@ -85,12 +93,14 @@ def save(self, *args, **kwargs): if is_new and self.instance.is_static and not self.instance.all_rules_static: rules = [ - form.instance for formset in self.formsets.values() + form.instance + for formset in self.formsets.values() for form in formset if form not in formset.deleted_forms ] self.instance.matched_users_count = self.count_matching_users( - rules, self.instance.match_any) + rules, self.instance.match_any + ) self.instance.matched_count_updated_at = datetime.now() instance = super(SegmentAdminForm, self).save(*args, **kwargs) @@ -98,7 +108,7 @@ def save(self, *args, **kwargs): if is_new and instance.is_static and instance.all_rules_static: from .adapters import get_segment_adapter - request = RequestFactory().get('/') + request = RequestFactory().get("/") request.session = SessionStore() adapter = get_segment_adapter(request) @@ -111,7 +121,9 @@ def save(self, *args, **kwargs): matched_count = 0 for user in users.iterator(): request.user = user - passes = adapter._test_rules(instance.get_rules(), request, instance.match_any) + passes = adapter._test_rules( + instance.get_rules(), request, instance.match_any + ) if passes: matched_count += 1 if instance.count == 0 or len(users_to_add) < instance.count: @@ -130,7 +142,5 @@ def save(self, *args, **kwargs): @property def media(self): media = super(SegmentAdminForm, self).media - media.add_js( - [static('js/segment_form_control.js')] - ) + media.add_js([static("js/segment_form_control.js")]) return media diff --git a/src/wagtail_personalisation/migrations/0001_initial.py b/src/wagtail_personalisation/migrations/0001_initial.py index 78a13d92..a25c8a49 100644 --- a/src/wagtail_personalisation/migrations/0001_initial.py +++ b/src/wagtail_personalisation/migrations/0001_initial.py @@ -8,85 +8,192 @@ class Migration(migrations.Migration): - initial = True dependencies = [ - ('wagtailcore', '0001_initial'), + ("wagtailcore", "0001_initial"), ] operations = [ migrations.CreateModel( - name='PersonalisablePage', + name="PersonalisablePage", 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')), - ('is_segmented', models.BooleanField(default=False)), - ('canonical_page', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='variants', to='wagtail_personalisation.PersonalisablePage')), + ( + "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", + ), + ), + ("is_segmented", models.BooleanField(default=False)), + ( + "canonical_page", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + related_name="variants", + to="wagtail_personalisation.PersonalisablePage", + ), + ), ], options={ - 'abstract': False, + "abstract": False, }, - bases=('wagtailcore.page',), + bases=("wagtailcore.page",), ), migrations.CreateModel( - name='ReferralRule', + name="ReferralRule", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('regex_string', models.TextField(verbose_name='Regex string to match the referer with')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "regex_string", + models.TextField( + verbose_name="Regex string to match the referer with" + ), + ), ], options={ - 'abstract': False, + "abstract": False, }, ), migrations.CreateModel( - name='Segment', + name="Segment", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(max_length=255)), - ('create_date', models.DateTimeField(auto_now_add=True)), - ('edit_date', models.DateTimeField(auto_now=True)), - ('enable_date', models.DateTimeField(editable=False, null=True)), - ('disable_date', models.DateTimeField(editable=False, null=True)), - ('visit_count', models.PositiveIntegerField(default=0, editable=False)), - ('status', models.CharField(choices=[('enabled', 'Enabled'), ('disabled', 'Disabled')], default='enabled', max_length=20)), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=255)), + ("create_date", models.DateTimeField(auto_now_add=True)), + ("edit_date", models.DateTimeField(auto_now=True)), + ("enable_date", models.DateTimeField(editable=False, null=True)), + ("disable_date", models.DateTimeField(editable=False, null=True)), + ("visit_count", models.PositiveIntegerField(default=0, editable=False)), + ( + "status", + models.CharField( + choices=[("enabled", "Enabled"), ("disabled", "Disabled")], + default="enabled", + max_length=20, + ), + ), ], options={ - 'abstract': False, + "abstract": False, }, ), migrations.CreateModel( - name='TimeRule', + name="TimeRule", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('start_time', models.TimeField(verbose_name='Starting time')), - ('end_time', models.TimeField(verbose_name='Ending time')), - ('segment', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_timerule_related', related_query_name='wagtail_personalisation_timerules', to='wagtail_personalisation.Segment')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("start_time", models.TimeField(verbose_name="Starting time")), + ("end_time", models.TimeField(verbose_name="Ending time")), + ( + "segment", + modelcluster.fields.ParentalKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="wagtail_personalisation_timerule_related", + related_query_name="wagtail_personalisation_timerules", + to="wagtail_personalisation.Segment", + ), + ), ], options={ - 'abstract': False, + "abstract": False, }, ), migrations.CreateModel( - name='VisitCountRule', + name="VisitCountRule", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('operator', models.CharField(choices=[('more_than', 'More than'), ('less_than', 'Less than'), ('equal_to', 'Equal to')], default='ht', max_length=20)), - ('count', models.PositiveSmallIntegerField(default=0, null=True)), - ('counted_page', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='wagtailcore.Page')), - ('segment', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_visitcountrule_related', related_query_name='wagtail_personalisation_visitcountrules', to='wagtail_personalisation.Segment')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "operator", + models.CharField( + choices=[ + ("more_than", "More than"), + ("less_than", "Less than"), + ("equal_to", "Equal to"), + ], + default="ht", + max_length=20, + ), + ), + ("count", models.PositiveSmallIntegerField(default=0, null=True)), + ( + "counted_page", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="+", + to="wagtailcore.Page", + ), + ), + ( + "segment", + modelcluster.fields.ParentalKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="wagtail_personalisation_visitcountrule_related", + related_query_name="wagtail_personalisation_visitcountrules", + to="wagtail_personalisation.Segment", + ), + ), ], options={ - 'abstract': False, + "abstract": False, }, ), migrations.AddField( - model_name='referralrule', - name='segment', - field=modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_referralrule_related', related_query_name='wagtail_personalisation_referralrules', to='wagtail_personalisation.Segment'), + model_name="referralrule", + name="segment", + field=modelcluster.fields.ParentalKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="wagtail_personalisation_referralrule_related", + related_query_name="wagtail_personalisation_referralrules", + to="wagtail_personalisation.Segment", + ), ), migrations.AddField( - model_name='personalisablepage', - name='segment', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='segments', to='wagtail_personalisation.Segment'), + model_name="personalisablepage", + name="segment", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + related_name="segments", + to="wagtail_personalisation.Segment", + ), ), ] diff --git a/src/wagtail_personalisation/migrations/0002_auto_20161205_1623.py b/src/wagtail_personalisation/migrations/0002_auto_20161205_1623.py index 3142269d..8e1c52db 100644 --- a/src/wagtail_personalisation/migrations/0002_auto_20161205_1623.py +++ b/src/wagtail_personalisation/migrations/0002_auto_20161205_1623.py @@ -8,27 +8,58 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0001_initial'), + ("wagtail_personalisation", "0001_initial"), ] operations = [ migrations.CreateModel( - name='QueryRule', + name="QueryRule", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('query_parameter', models.TextField(verbose_name='The query parameter to search for')), - ('query_value', models.TextField(verbose_name='The value of the parameter to match')), - ('segment', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_queryrule_related', related_query_name='wagtail_personalisation_queryrules', to='wagtail_personalisation.Segment')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "query_parameter", + models.TextField(verbose_name="The query parameter to search for"), + ), + ( + "query_value", + models.TextField( + verbose_name="The value of the parameter to match" + ), + ), + ( + "segment", + modelcluster.fields.ParentalKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="wagtail_personalisation_queryrule_related", + related_query_name="wagtail_personalisation_queryrules", + to="wagtail_personalisation.Segment", + ), + ), ], options={ - 'abstract': False, + "abstract": False, }, ), migrations.AlterField( - model_name='visitcountrule', - name='operator', - field=models.CharField(choices=[('more_than', 'More than'), ('less_than', 'Less than'), ('equal_to', 'Equal to')], default='more_than', max_length=20), + model_name="visitcountrule", + name="operator", + field=models.CharField( + choices=[ + ("more_than", "More than"), + ("less_than", "Less than"), + ("equal_to", "Equal to"), + ], + default="more_than", + max_length=20, + ), ), ] diff --git a/src/wagtail_personalisation/migrations/0003_auto_20161206_1005.py b/src/wagtail_personalisation/migrations/0003_auto_20161206_1005.py index e0933c03..c1648614 100644 --- a/src/wagtail_personalisation/migrations/0003_auto_20161206_1005.py +++ b/src/wagtail_personalisation/migrations/0003_auto_20161206_1005.py @@ -6,30 +6,37 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0002_auto_20161205_1623'), + ("wagtail_personalisation", "0002_auto_20161205_1623"), ] operations = [ migrations.RemoveField( - model_name='queryrule', - name='query_parameter', + model_name="queryrule", + name="query_parameter", ), migrations.RemoveField( - model_name='queryrule', - name='query_value', + model_name="queryrule", + name="query_value", ), migrations.AddField( - model_name='queryrule', - name='parameter', - field=models.SlugField(default='test', max_length=20, verbose_name='The query parameter to search for'), + model_name="queryrule", + name="parameter", + field=models.SlugField( + default="test", + max_length=20, + verbose_name="The query parameter to search for", + ), preserve_default=False, ), migrations.AddField( - model_name='queryrule', - name='value', - field=models.SlugField(default='test', max_length=20, verbose_name='The value of the parameter to match'), + model_name="queryrule", + name="value", + field=models.SlugField( + default="test", + max_length=20, + verbose_name="The value of the parameter to match", + ), preserve_default=False, ), ] diff --git a/src/wagtail_personalisation/migrations/0004_segment_persistent.py b/src/wagtail_personalisation/migrations/0004_segment_persistent.py index a4f89f8c..ba6c90cf 100644 --- a/src/wagtail_personalisation/migrations/0004_segment_persistent.py +++ b/src/wagtail_personalisation/migrations/0004_segment_persistent.py @@ -6,15 +6,16 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0003_auto_20161206_1005'), + ("wagtail_personalisation", "0003_auto_20161206_1005"), ] operations = [ migrations.AddField( - model_name='segment', - name='persistent', - field=models.BooleanField(default=False, help_text='Should the segment persist between visits?'), + model_name="segment", + name="persistent", + field=models.BooleanField( + default=False, help_text="Should the segment persist between visits?" + ), ), ] diff --git a/src/wagtail_personalisation/migrations/0005_userisloggedinrule.py b/src/wagtail_personalisation/migrations/0005_userisloggedinrule.py index b361d187..0a7d08d9 100644 --- a/src/wagtail_personalisation/migrations/0005_userisloggedinrule.py +++ b/src/wagtail_personalisation/migrations/0005_userisloggedinrule.py @@ -8,21 +8,36 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0004_segment_persistent'), + ("wagtail_personalisation", "0004_segment_persistent"), ] operations = [ migrations.CreateModel( - name='UserIsLoggedInRule', + name="UserIsLoggedInRule", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('is_logged_in', models.BooleanField(default=False)), - ('segment', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_userisloggedinrule_related', related_query_name='wagtail_personalisation_userisloggedinrules', to='wagtail_personalisation.Segment')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("is_logged_in", models.BooleanField(default=False)), + ( + "segment", + modelcluster.fields.ParentalKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="wagtail_personalisation_userisloggedinrule_related", + related_query_name="wagtail_personalisation_userisloggedinrules", + to="wagtail_personalisation.Segment", + ), + ), ], options={ - 'abstract': False, + "abstract": False, }, ), ] diff --git a/src/wagtail_personalisation/migrations/0006_segment_match_any.py b/src/wagtail_personalisation/migrations/0006_segment_match_any.py index 99bd98f1..1683f492 100644 --- a/src/wagtail_personalisation/migrations/0006_segment_match_any.py +++ b/src/wagtail_personalisation/migrations/0006_segment_match_any.py @@ -6,15 +6,17 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0005_userisloggedinrule'), + ("wagtail_personalisation", "0005_userisloggedinrule"), ] operations = [ migrations.AddField( - model_name='segment', - name='match_any', - field=models.BooleanField(default=False, help_text='Should the segment match all the rules or just one of them?'), + model_name="segment", + name="match_any", + field=models.BooleanField( + default=False, + help_text="Should the segment match all the rules or just one of them?", + ), ), ] diff --git a/src/wagtail_personalisation/migrations/0007_dayrule.py b/src/wagtail_personalisation/migrations/0007_dayrule.py index 7bfa79f3..575c8942 100644 --- a/src/wagtail_personalisation/migrations/0007_dayrule.py +++ b/src/wagtail_personalisation/migrations/0007_dayrule.py @@ -8,27 +8,42 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0006_segment_match_any'), + ("wagtail_personalisation", "0006_segment_match_any"), ] operations = [ migrations.CreateModel( - name='DayRule', + name="DayRule", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('mon', models.BooleanField(default=False, verbose_name='Monday')), - ('tue', models.BooleanField(default=False, verbose_name='Tuesday')), - ('wed', models.BooleanField(default=False, verbose_name='Wednesday')), - ('thu', models.BooleanField(default=False, verbose_name='Thursday')), - ('fri', models.BooleanField(default=False, verbose_name='Friday')), - ('sat', models.BooleanField(default=False, verbose_name='Saturday')), - ('sun', models.BooleanField(default=False, verbose_name='Sunday')), - ('segment', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_dayrule_related', related_query_name='wagtail_personalisation_dayrules', to='wagtail_personalisation.Segment')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("mon", models.BooleanField(default=False, verbose_name="Monday")), + ("tue", models.BooleanField(default=False, verbose_name="Tuesday")), + ("wed", models.BooleanField(default=False, verbose_name="Wednesday")), + ("thu", models.BooleanField(default=False, verbose_name="Thursday")), + ("fri", models.BooleanField(default=False, verbose_name="Friday")), + ("sat", models.BooleanField(default=False, verbose_name="Saturday")), + ("sun", models.BooleanField(default=False, verbose_name="Sunday")), + ( + "segment", + modelcluster.fields.ParentalKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="wagtail_personalisation_dayrule_related", + related_query_name="wagtail_personalisation_dayrules", + to="wagtail_personalisation.Segment", + ), + ), ], options={ - 'abstract': False, + "abstract": False, }, ), ] diff --git a/src/wagtail_personalisation/migrations/0008_devicerule.py b/src/wagtail_personalisation/migrations/0008_devicerule.py index 448985bb..8b20fc88 100644 --- a/src/wagtail_personalisation/migrations/0008_devicerule.py +++ b/src/wagtail_personalisation/migrations/0008_devicerule.py @@ -8,23 +8,41 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0007_dayrule'), + ("wagtail_personalisation", "0007_dayrule"), ] operations = [ migrations.CreateModel( - name='DeviceRule', + name="DeviceRule", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('mobile', models.BooleanField(default=False, verbose_name='Mobile phone')), - ('tablet', models.BooleanField(default=False, verbose_name='Tablet')), - ('desktop', models.BooleanField(default=False, verbose_name='Desktop')), - ('segment', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_devicerule_related', related_query_name='wagtail_personalisation_devicerules', to='wagtail_personalisation.Segment')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "mobile", + models.BooleanField(default=False, verbose_name="Mobile phone"), + ), + ("tablet", models.BooleanField(default=False, verbose_name="Tablet")), + ("desktop", models.BooleanField(default=False, verbose_name="Desktop")), + ( + "segment", + modelcluster.fields.ParentalKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="wagtail_personalisation_devicerule_related", + related_query_name="wagtail_personalisation_devicerules", + to="wagtail_personalisation.Segment", + ), + ), ], options={ - 'abstract': False, + "abstract": False, }, ), ] diff --git a/src/wagtail_personalisation/migrations/0009_auto_20170531_0428.py b/src/wagtail_personalisation/migrations/0009_auto_20170531_0428.py index 1f64e984..9b457d6b 100644 --- a/src/wagtail_personalisation/migrations/0009_auto_20170531_0428.py +++ b/src/wagtail_personalisation/migrations/0009_auto_20170531_0428.py @@ -6,25 +6,24 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0008_devicerule'), + ("wagtail_personalisation", "0008_devicerule"), ] operations = [ migrations.RemoveField( - model_name='personalisablepage', - name='canonical_page', + model_name="personalisablepage", + name="canonical_page", ), migrations.RemoveField( - model_name='personalisablepage', - name='page_ptr', + model_name="personalisablepage", + name="page_ptr", ), migrations.RemoveField( - model_name='personalisablepage', - name='segment', + model_name="personalisablepage", + name="segment", ), migrations.DeleteModel( - name='PersonalisablePage', + name="PersonalisablePage", ), ] diff --git a/src/wagtail_personalisation/migrations/0010_auto_20170531_1101.py b/src/wagtail_personalisation/migrations/0010_auto_20170531_1101.py index 289f8c32..3f799ad1 100644 --- a/src/wagtail_personalisation/migrations/0010_auto_20170531_1101.py +++ b/src/wagtail_personalisation/migrations/0010_auto_20170531_1101.py @@ -6,43 +6,44 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0009_auto_20170531_0428'), + ("wagtail_personalisation", "0009_auto_20170531_0428"), ] operations = [ migrations.AlterModelOptions( - name='dayrule', - options={'verbose_name': 'Day Rule'}, + name="dayrule", + options={"verbose_name": "Day Rule"}, ), migrations.AlterModelOptions( - name='devicerule', - options={'verbose_name': 'Device Rule'}, + name="devicerule", + options={"verbose_name": "Device Rule"}, ), migrations.AlterModelOptions( - name='queryrule', - options={'verbose_name': 'Query Rule'}, + name="queryrule", + options={"verbose_name": "Query Rule"}, ), migrations.AlterModelOptions( - name='referralrule', - options={'verbose_name': 'Referral Rule'}, + name="referralrule", + options={"verbose_name": "Referral Rule"}, ), migrations.AlterModelOptions( - name='timerule', - options={'verbose_name': 'Time Rule'}, + name="timerule", + options={"verbose_name": "Time Rule"}, ), migrations.AlterModelOptions( - name='userisloggedinrule', - options={'verbose_name': 'Logged in Rule'}, + name="userisloggedinrule", + options={"verbose_name": "Logged in Rule"}, ), migrations.AlterModelOptions( - name='visitcountrule', - options={'verbose_name': 'Visit count Rule'}, + name="visitcountrule", + options={"verbose_name": "Visit count Rule"}, ), migrations.AlterField( - model_name='referralrule', - name='regex_string', - field=models.TextField(verbose_name='Regular expression to match the referrer'), + model_name="referralrule", + name="regex_string", + field=models.TextField( + verbose_name="Regular expression to match the referrer" + ), ), ] diff --git a/src/wagtail_personalisation/migrations/0011_personalisablepagemetadata.py b/src/wagtail_personalisation/migrations/0011_personalisablepagemetadata.py index a4834f5c..7ce2f276 100644 --- a/src/wagtail_personalisation/migrations/0011_personalisablepagemetadata.py +++ b/src/wagtail_personalisation/migrations/0011_personalisablepagemetadata.py @@ -7,24 +7,56 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtailcore', '0001_initial'), - ('wagtail_personalisation', '0010_auto_20170531_1101'), + ("wagtailcore", "0001_initial"), + ("wagtail_personalisation", "0010_auto_20170531_1101"), ] operations = [ migrations.CreateModel( - name='PersonalisablePageMetadata', + name="PersonalisablePageMetadata", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('is_segmented', models.BooleanField(default=False)), - ('canonical_page', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='personalisable_canonical_metadata', to='wagtailcore.Page')), - ('segment', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='page_metadata', to='wagtail_personalisation.Segment')), - ('variant', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='_personalisable_page_metadata', to='wagtailcore.Page')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("is_segmented", models.BooleanField(default=False)), + ( + "canonical_page", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + related_name="personalisable_canonical_metadata", + to="wagtailcore.Page", + ), + ), + ( + "segment", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="page_metadata", + to="wagtail_personalisation.Segment", + ), + ), + ( + "variant", + models.OneToOneField( + on_delete=django.db.models.deletion.CASCADE, + related_name="_personalisable_page_metadata", + to="wagtailcore.Page", + ), + ), ], options={ - 'abstract': False, + "abstract": False, }, ), ] diff --git a/src/wagtail_personalisation/migrations/0012_remove_personalisablepagemetadata_is_segmented.py b/src/wagtail_personalisation/migrations/0012_remove_personalisablepagemetadata_is_segmented.py index b8e24387..2557ba08 100644 --- a/src/wagtail_personalisation/migrations/0012_remove_personalisablepagemetadata_is_segmented.py +++ b/src/wagtail_personalisation/migrations/0012_remove_personalisablepagemetadata_is_segmented.py @@ -6,14 +6,13 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0011_personalisablepagemetadata'), + ("wagtail_personalisation", "0011_personalisablepagemetadata"), ] operations = [ migrations.RemoveField( - model_name='personalisablepagemetadata', - name='is_segmented', + model_name="personalisablepagemetadata", + name="is_segmented", ), ] diff --git a/src/wagtail_personalisation/migrations/0013_add_dynamic_static_to_segment.py b/src/wagtail_personalisation/migrations/0013_add_dynamic_static_to_segment.py index 6196fa84..addc1a49 100644 --- a/src/wagtail_personalisation/migrations/0013_add_dynamic_static_to_segment.py +++ b/src/wagtail_personalisation/migrations/0013_add_dynamic_static_to_segment.py @@ -6,26 +6,36 @@ class Migration(migrations.Migration): - dependencies = [ - ('sessions', '0001_initial'), - ('wagtail_personalisation', '0012_remove_personalisablepagemetadata_is_segmented'), + ("sessions", "0001_initial"), + ( + "wagtail_personalisation", + "0012_remove_personalisablepagemetadata_is_segmented", + ), ] operations = [ migrations.AddField( - model_name='segment', - name='count', - field=models.PositiveSmallIntegerField(default=0, help_text='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.'), + model_name="segment", + name="count", + field=models.PositiveSmallIntegerField( + default=0, + help_text="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.", + ), ), migrations.AddField( - model_name='segment', - name='sessions', - field=models.ManyToManyField(to='sessions.Session'), + model_name="segment", + name="sessions", + field=models.ManyToManyField(to="sessions.Session"), ), migrations.AddField( - model_name='segment', - name='type', - field=models.CharField(choices=[('dynamic', 'Dynamic'), ('static', 'Static')], default='dynamic', help_text='\n

Dynamic: Users in this segment will change\n as more or less meet the rules specified in the segment.\n
Static: If the segment contains only static\n compatible rules the segment will contain the members that pass\n those rules when the segment is created. Mixed static segments or\n those containing entirely non static compatible rules will be\n populated using the count variable.\n ', max_length=20), + model_name="segment", + name="type", + field=models.CharField( + choices=[("dynamic", "Dynamic"), ("static", "Static")], + default="dynamic", + help_text="\n

Dynamic: Users in this segment will change\n as more or less meet the rules specified in the segment.\n
Static: If the segment contains only static\n compatible rules the segment will contain the members that pass\n those rules when the segment is created. Mixed static segments or\n those containing entirely non static compatible rules will be\n populated using the count variable.\n ", + max_length=20, + ), ), ] diff --git a/src/wagtail_personalisation/migrations/0015_static_users.py b/src/wagtail_personalisation/migrations/0015_static_users.py index ea76aa8b..db35051e 100644 --- a/src/wagtail_personalisation/migrations/0015_static_users.py +++ b/src/wagtail_personalisation/migrations/0015_static_users.py @@ -7,20 +7,19 @@ class Migration(migrations.Migration): - dependencies = [ migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ('wagtail_personalisation', '0013_add_dynamic_static_to_segment'), + ("wagtail_personalisation", "0013_add_dynamic_static_to_segment"), ] operations = [ migrations.RemoveField( - model_name='segment', - name='sessions', + model_name="segment", + name="sessions", ), migrations.AddField( - model_name='segment', - name='static_users', + model_name="segment", + name="static_users", field=models.ManyToManyField(to=settings.AUTH_USER_MODEL), ), ] diff --git a/src/wagtail_personalisation/migrations/0016_auto_20180125_0918.py b/src/wagtail_personalisation/migrations/0016_auto_20180125_0918.py index ae7bdd1d..82d30afa 100644 --- a/src/wagtail_personalisation/migrations/0016_auto_20180125_0918.py +++ b/src/wagtail_personalisation/migrations/0016_auto_20180125_0918.py @@ -6,20 +6,19 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0015_static_users'), + ("wagtail_personalisation", "0015_static_users"), ] operations = [ migrations.AddField( - model_name='segment', - name='matched_count_updated_at', + model_name="segment", + name="matched_count_updated_at", field=models.DateTimeField(editable=False, null=True), ), migrations.AddField( - model_name='segment', - name='matched_users_count', + model_name="segment", + name="matched_users_count", field=models.PositiveIntegerField(default=0, editable=False), ), ] diff --git a/src/wagtail_personalisation/migrations/0017_segment_randomisation_percent.py b/src/wagtail_personalisation/migrations/0017_segment_randomisation_percent.py index bd68335a..d75c51dc 100644 --- a/src/wagtail_personalisation/migrations/0017_segment_randomisation_percent.py +++ b/src/wagtail_personalisation/migrations/0017_segment_randomisation_percent.py @@ -7,15 +7,23 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0016_auto_20180125_0918'), + ("wagtail_personalisation", "0016_auto_20180125_0918"), ] operations = [ migrations.AddField( - model_name='segment', - name='randomisation_percent', - field=models.PositiveSmallIntegerField(blank=True, default=None, help_text='If this number is set each user matching the rules will have this percentage chance of being placed in the segment.', null=True, validators=[django.core.validators.MaxValueValidator(100), django.core.validators.MinValueValidator(0)]), + model_name="segment", + name="randomisation_percent", + field=models.PositiveSmallIntegerField( + blank=True, + default=None, + help_text="If this number is set each user matching the rules will have this percentage chance of being placed in the segment.", + null=True, + validators=[ + django.core.validators.MaxValueValidator(100), + django.core.validators.MinValueValidator(0), + ], + ), ), ] diff --git a/src/wagtail_personalisation/migrations/0018_segment_excluded_users.py b/src/wagtail_personalisation/migrations/0018_segment_excluded_users.py index bafa4776..ff18b9d8 100644 --- a/src/wagtail_personalisation/migrations/0018_segment_excluded_users.py +++ b/src/wagtail_personalisation/migrations/0018_segment_excluded_users.py @@ -7,16 +7,19 @@ class Migration(migrations.Migration): - dependencies = [ migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ('wagtail_personalisation', '0017_segment_randomisation_percent'), + ("wagtail_personalisation", "0017_segment_randomisation_percent"), ] operations = [ migrations.AddField( - model_name='segment', - name='excluded_users', - field=models.ManyToManyField(help_text='Users that matched the rules but were excluded from the segment for some reason e.g. randomisation', related_name='excluded_segments', to=settings.AUTH_USER_MODEL), + model_name="segment", + name="excluded_users", + field=models.ManyToManyField( + help_text="Users that matched the rules but were excluded from the segment for some reason e.g. randomisation", + related_name="excluded_segments", + to=settings.AUTH_USER_MODEL, + ), ), ] diff --git a/src/wagtail_personalisation/migrations/0019_auto_20180526_1425.py b/src/wagtail_personalisation/migrations/0019_auto_20180526_1425.py index 492fc11c..4431697f 100644 --- a/src/wagtail_personalisation/migrations/0019_auto_20180526_1425.py +++ b/src/wagtail_personalisation/migrations/0019_auto_20180526_1425.py @@ -5,15 +5,20 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0018_segment_excluded_users'), + ("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'), + 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", + ), ), ] diff --git a/src/wagtail_personalisation/migrations/0020_rules_delete_relatedqueryname.py b/src/wagtail_personalisation/migrations/0020_rules_delete_relatedqueryname.py index 88d811df..ab530c5f 100644 --- a/src/wagtail_personalisation/migrations/0020_rules_delete_relatedqueryname.py +++ b/src/wagtail_personalisation/migrations/0020_rules_delete_relatedqueryname.py @@ -6,45 +6,72 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0019_auto_20180526_1425'), + ("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'), + 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'), + 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'), + 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'), + 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'), + 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'), + 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'), + 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", + ), ), ] diff --git a/src/wagtail_personalisation/migrations/0021_personalisablepagemetadata_segment_set_on_delete_protect.py b/src/wagtail_personalisation/migrations/0021_personalisablepagemetadata_segment_set_on_delete_protect.py index 7608bf6a..58dec322 100644 --- a/src/wagtail_personalisation/migrations/0021_personalisablepagemetadata_segment_set_on_delete_protect.py +++ b/src/wagtail_personalisation/migrations/0021_personalisablepagemetadata_segment_set_on_delete_protect.py @@ -5,15 +5,19 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0020_rules_delete_relatedqueryname'), + ("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'), + 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", + ), ), ] diff --git a/src/wagtail_personalisation/migrations/0022_personalisablepagemetadata_canonical_protect.py b/src/wagtail_personalisation/migrations/0022_personalisablepagemetadata_canonical_protect.py index 6dbf7450..cb4cf0e1 100644 --- a/src/wagtail_personalisation/migrations/0022_personalisablepagemetadata_canonical_protect.py +++ b/src/wagtail_personalisation/migrations/0022_personalisablepagemetadata_canonical_protect.py @@ -5,15 +5,22 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0021_personalisablepagemetadata_segment_set_on_delete_protect'), + ( + "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'), + 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", + ), ), ] diff --git a/src/wagtail_personalisation/migrations/0023_personalisablepagemetadata_variant_cascade.py b/src/wagtail_personalisation/migrations/0023_personalisablepagemetadata_variant_cascade.py index 7dcaf871..0a3e95be 100644 --- a/src/wagtail_personalisation/migrations/0023_personalisablepagemetadata_variant_cascade.py +++ b/src/wagtail_personalisation/migrations/0023_personalisablepagemetadata_variant_cascade.py @@ -5,15 +5,22 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0022_personalisablepagemetadata_canonical_protect'), + ( + "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'), + 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", + ), ), ] diff --git a/src/wagtail_personalisation/migrations/0024_origincountryrule.py b/src/wagtail_personalisation/migrations/0024_origincountryrule.py index b6a9aa0b..6c522c28 100644 --- a/src/wagtail_personalisation/migrations/0024_origincountryrule.py +++ b/src/wagtail_personalisation/migrations/0024_origincountryrule.py @@ -6,21 +6,292 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0023_personalisablepagemetadata_variant_cascade'), + ("wagtail_personalisation", "0023_personalisablepagemetadata_variant_cascade"), ] operations = [ migrations.CreateModel( - name='OriginCountryRule', + name="OriginCountryRule", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('country', models.CharField(choices=[('aw', 'Aruba'), ('af', 'Afghanistan'), ('ao', 'Angola'), ('ai', 'Anguilla'), ('ax', 'Åland Islands'), ('al', 'Albania'), ('ad', 'Andorra'), ('ae', 'United Arab Emirates'), ('ar', 'Argentina'), ('am', 'Armenia'), ('as', 'American Samoa'), ('aq', 'Antarctica'), ('tf', 'French Southern Territories'), ('ag', 'Antigua and Barbuda'), ('au', 'Australia'), ('at', 'Austria'), ('az', 'Azerbaijan'), ('bi', 'Burundi'), ('be', 'Belgium'), ('bj', 'Benin'), ('bq', 'Bonaire, Sint Eustatius and Saba'), ('bf', 'Burkina Faso'), ('bd', 'Bangladesh'), ('bg', 'Bulgaria'), ('bh', 'Bahrain'), ('bs', 'Bahamas'), ('ba', 'Bosnia and Herzegovina'), ('bl', 'Saint Barthélemy'), ('by', 'Belarus'), ('bz', 'Belize'), ('bm', 'Bermuda'), ('bo', 'Bolivia, Plurinational State of'), ('br', 'Brazil'), ('bb', 'Barbados'), ('bn', 'Brunei Darussalam'), ('bt', 'Bhutan'), ('bv', 'Bouvet Island'), ('bw', 'Botswana'), ('cf', 'Central African Republic'), ('ca', 'Canada'), ('cc', 'Cocos (Keeling) Islands'), ('ch', 'Switzerland'), ('cl', 'Chile'), ('cn', 'China'), ('ci', "Côte d'Ivoire"), ('cm', 'Cameroon'), ('cd', 'Congo, The Democratic Republic of the'), ('cg', 'Congo'), ('ck', 'Cook Islands'), ('co', 'Colombia'), ('km', 'Comoros'), ('cv', 'Cabo Verde'), ('cr', 'Costa Rica'), ('cu', 'Cuba'), ('cw', 'Curaçao'), ('cx', 'Christmas Island'), ('ky', 'Cayman Islands'), ('cy', 'Cyprus'), ('cz', 'Czechia'), ('de', 'Germany'), ('dj', 'Djibouti'), ('dm', 'Dominica'), ('dk', 'Denmark'), ('do', 'Dominican Republic'), ('dz', 'Algeria'), ('ec', 'Ecuador'), ('eg', 'Egypt'), ('er', 'Eritrea'), ('eh', 'Western Sahara'), ('es', 'Spain'), ('ee', 'Estonia'), ('et', 'Ethiopia'), ('fi', 'Finland'), ('fj', 'Fiji'), ('fk', 'Falkland Islands (Malvinas)'), ('fr', 'France'), ('fo', 'Faroe Islands'), ('fm', 'Micronesia, Federated States of'), ('ga', 'Gabon'), ('gb', 'United Kingdom'), ('ge', 'Georgia'), ('gg', 'Guernsey'), ('gh', 'Ghana'), ('gi', 'Gibraltar'), ('gn', 'Guinea'), ('gp', 'Guadeloupe'), ('gm', 'Gambia'), ('gw', 'Guinea-Bissau'), ('gq', 'Equatorial Guinea'), ('gr', 'Greece'), ('gd', 'Grenada'), ('gl', 'Greenland'), ('gt', 'Guatemala'), ('gf', 'French Guiana'), ('gu', 'Guam'), ('gy', 'Guyana'), ('hk', 'Hong Kong'), ('hm', 'Heard Island and McDonald Islands'), ('hn', 'Honduras'), ('hr', 'Croatia'), ('ht', 'Haiti'), ('hu', 'Hungary'), ('id', 'Indonesia'), ('im', 'Isle of Man'), ('in', 'India'), ('io', 'British Indian Ocean Territory'), ('ie', 'Ireland'), ('ir', 'Iran, Islamic Republic of'), ('iq', 'Iraq'), ('is', 'Iceland'), ('il', 'Israel'), ('it', 'Italy'), ('jm', 'Jamaica'), ('je', 'Jersey'), ('jo', 'Jordan'), ('jp', 'Japan'), ('kz', 'Kazakhstan'), ('ke', 'Kenya'), ('kg', 'Kyrgyzstan'), ('kh', 'Cambodia'), ('ki', 'Kiribati'), ('kn', 'Saint Kitts and Nevis'), ('kr', 'Korea, Republic of'), ('kw', 'Kuwait'), ('la', "Lao People's Democratic Republic"), ('lb', 'Lebanon'), ('lr', 'Liberia'), ('ly', 'Libya'), ('lc', 'Saint Lucia'), ('li', 'Liechtenstein'), ('lk', 'Sri Lanka'), ('ls', 'Lesotho'), ('lt', 'Lithuania'), ('lu', 'Luxembourg'), ('lv', 'Latvia'), ('mo', 'Macao'), ('mf', 'Saint Martin (French part)'), ('ma', 'Morocco'), ('mc', 'Monaco'), ('md', 'Moldova, Republic of'), ('mg', 'Madagascar'), ('mv', 'Maldives'), ('mx', 'Mexico'), ('mh', 'Marshall Islands'), ('mk', 'Macedonia, Republic of'), ('ml', 'Mali'), ('mt', 'Malta'), ('mm', 'Myanmar'), ('me', 'Montenegro'), ('mn', 'Mongolia'), ('mp', 'Northern Mariana Islands'), ('mz', 'Mozambique'), ('mr', 'Mauritania'), ('ms', 'Montserrat'), ('mq', 'Martinique'), ('mu', 'Mauritius'), ('mw', 'Malawi'), ('my', 'Malaysia'), ('yt', 'Mayotte'), ('na', 'Namibia'), ('nc', 'New Caledonia'), ('ne', 'Niger'), ('nf', 'Norfolk Island'), ('ng', 'Nigeria'), ('ni', 'Nicaragua'), ('nu', 'Niue'), ('nl', 'Netherlands'), ('no', 'Norway'), ('np', 'Nepal'), ('nr', 'Nauru'), ('nz', 'New Zealand'), ('om', 'Oman'), ('pk', 'Pakistan'), ('pa', 'Panama'), ('pn', 'Pitcairn'), ('pe', 'Peru'), ('ph', 'Philippines'), ('pw', 'Palau'), ('pg', 'Papua New Guinea'), ('pl', 'Poland'), ('pr', 'Puerto Rico'), ('kp', "Korea, Democratic People's Republic of"), ('pt', 'Portugal'), ('py', 'Paraguay'), ('ps', 'Palestine, State of'), ('pf', 'French Polynesia'), ('qa', 'Qatar'), ('re', 'Réunion'), ('ro', 'Romania'), ('ru', 'Russian Federation'), ('rw', 'Rwanda'), ('sa', 'Saudi Arabia'), ('sd', 'Sudan'), ('sn', 'Senegal'), ('sg', 'Singapore'), ('gs', 'South Georgia and the South Sandwich Islands'), ('sh', 'Saint Helena, Ascension and Tristan da Cunha'), ('sj', 'Svalbard and Jan Mayen'), ('sb', 'Solomon Islands'), ('sl', 'Sierra Leone'), ('sv', 'El Salvador'), ('sm', 'San Marino'), ('so', 'Somalia'), ('pm', 'Saint Pierre and Miquelon'), ('rs', 'Serbia'), ('ss', 'South Sudan'), ('st', 'Sao Tome and Principe'), ('sr', 'Suriname'), ('sk', 'Slovakia'), ('si', 'Slovenia'), ('se', 'Sweden'), ('sz', 'Swaziland'), ('sx', 'Sint Maarten (Dutch part)'), ('sc', 'Seychelles'), ('sy', 'Syrian Arab Republic'), ('tc', 'Turks and Caicos Islands'), ('td', 'Chad'), ('tg', 'Togo'), ('th', 'Thailand'), ('tj', 'Tajikistan'), ('tk', 'Tokelau'), ('tm', 'Turkmenistan'), ('tl', 'Timor-Leste'), ('to', 'Tonga'), ('tt', 'Trinidad and Tobago'), ('tn', 'Tunisia'), ('tr', 'Turkey'), ('tv', 'Tuvalu'), ('tw', 'Taiwan, Province of China'), ('tz', 'Tanzania, United Republic of'), ('ug', 'Uganda'), ('ua', 'Ukraine'), ('um', 'United States Minor Outlying Islands'), ('uy', 'Uruguay'), ('us', 'United States'), ('uz', 'Uzbekistan'), ('va', 'Holy See (Vatican City State)'), ('vc', 'Saint Vincent and the Grenadines'), ('ve', 'Venezuela, Bolivarian Republic of'), ('vg', 'Virgin Islands, British'), ('vi', 'Virgin Islands, U.S.'), ('vn', 'Viet Nam'), ('vu', 'Vanuatu'), ('wf', 'Wallis and Futuna'), ('ws', 'Samoa'), ('ye', 'Yemen'), ('za', 'South Africa'), ('zm', 'Zambia'), ('zw', 'Zimbabwe')], help_text='Select origin country of the request that this rule will match against. This rule will only work if you use Cloudflare or CloudFront IP geolocation or if GeoIP2 module is configured.', max_length=2)), - ('segment', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_personalisation_origincountryrules', to='wagtail_personalisation.Segment')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "country", + models.CharField( + choices=[ + ("aw", "Aruba"), + ("af", "Afghanistan"), + ("ao", "Angola"), + ("ai", "Anguilla"), + ("ax", "Åland Islands"), + ("al", "Albania"), + ("ad", "Andorra"), + ("ae", "United Arab Emirates"), + ("ar", "Argentina"), + ("am", "Armenia"), + ("as", "American Samoa"), + ("aq", "Antarctica"), + ("tf", "French Southern Territories"), + ("ag", "Antigua and Barbuda"), + ("au", "Australia"), + ("at", "Austria"), + ("az", "Azerbaijan"), + ("bi", "Burundi"), + ("be", "Belgium"), + ("bj", "Benin"), + ("bq", "Bonaire, Sint Eustatius and Saba"), + ("bf", "Burkina Faso"), + ("bd", "Bangladesh"), + ("bg", "Bulgaria"), + ("bh", "Bahrain"), + ("bs", "Bahamas"), + ("ba", "Bosnia and Herzegovina"), + ("bl", "Saint Barthélemy"), + ("by", "Belarus"), + ("bz", "Belize"), + ("bm", "Bermuda"), + ("bo", "Bolivia, Plurinational State of"), + ("br", "Brazil"), + ("bb", "Barbados"), + ("bn", "Brunei Darussalam"), + ("bt", "Bhutan"), + ("bv", "Bouvet Island"), + ("bw", "Botswana"), + ("cf", "Central African Republic"), + ("ca", "Canada"), + ("cc", "Cocos (Keeling) Islands"), + ("ch", "Switzerland"), + ("cl", "Chile"), + ("cn", "China"), + ("ci", "Côte d'Ivoire"), + ("cm", "Cameroon"), + ("cd", "Congo, The Democratic Republic of the"), + ("cg", "Congo"), + ("ck", "Cook Islands"), + ("co", "Colombia"), + ("km", "Comoros"), + ("cv", "Cabo Verde"), + ("cr", "Costa Rica"), + ("cu", "Cuba"), + ("cw", "Curaçao"), + ("cx", "Christmas Island"), + ("ky", "Cayman Islands"), + ("cy", "Cyprus"), + ("cz", "Czechia"), + ("de", "Germany"), + ("dj", "Djibouti"), + ("dm", "Dominica"), + ("dk", "Denmark"), + ("do", "Dominican Republic"), + ("dz", "Algeria"), + ("ec", "Ecuador"), + ("eg", "Egypt"), + ("er", "Eritrea"), + ("eh", "Western Sahara"), + ("es", "Spain"), + ("ee", "Estonia"), + ("et", "Ethiopia"), + ("fi", "Finland"), + ("fj", "Fiji"), + ("fk", "Falkland Islands (Malvinas)"), + ("fr", "France"), + ("fo", "Faroe Islands"), + ("fm", "Micronesia, Federated States of"), + ("ga", "Gabon"), + ("gb", "United Kingdom"), + ("ge", "Georgia"), + ("gg", "Guernsey"), + ("gh", "Ghana"), + ("gi", "Gibraltar"), + ("gn", "Guinea"), + ("gp", "Guadeloupe"), + ("gm", "Gambia"), + ("gw", "Guinea-Bissau"), + ("gq", "Equatorial Guinea"), + ("gr", "Greece"), + ("gd", "Grenada"), + ("gl", "Greenland"), + ("gt", "Guatemala"), + ("gf", "French Guiana"), + ("gu", "Guam"), + ("gy", "Guyana"), + ("hk", "Hong Kong"), + ("hm", "Heard Island and McDonald Islands"), + ("hn", "Honduras"), + ("hr", "Croatia"), + ("ht", "Haiti"), + ("hu", "Hungary"), + ("id", "Indonesia"), + ("im", "Isle of Man"), + ("in", "India"), + ("io", "British Indian Ocean Territory"), + ("ie", "Ireland"), + ("ir", "Iran, Islamic Republic of"), + ("iq", "Iraq"), + ("is", "Iceland"), + ("il", "Israel"), + ("it", "Italy"), + ("jm", "Jamaica"), + ("je", "Jersey"), + ("jo", "Jordan"), + ("jp", "Japan"), + ("kz", "Kazakhstan"), + ("ke", "Kenya"), + ("kg", "Kyrgyzstan"), + ("kh", "Cambodia"), + ("ki", "Kiribati"), + ("kn", "Saint Kitts and Nevis"), + ("kr", "Korea, Republic of"), + ("kw", "Kuwait"), + ("la", "Lao People's Democratic Republic"), + ("lb", "Lebanon"), + ("lr", "Liberia"), + ("ly", "Libya"), + ("lc", "Saint Lucia"), + ("li", "Liechtenstein"), + ("lk", "Sri Lanka"), + ("ls", "Lesotho"), + ("lt", "Lithuania"), + ("lu", "Luxembourg"), + ("lv", "Latvia"), + ("mo", "Macao"), + ("mf", "Saint Martin (French part)"), + ("ma", "Morocco"), + ("mc", "Monaco"), + ("md", "Moldova, Republic of"), + ("mg", "Madagascar"), + ("mv", "Maldives"), + ("mx", "Mexico"), + ("mh", "Marshall Islands"), + ("mk", "Macedonia, Republic of"), + ("ml", "Mali"), + ("mt", "Malta"), + ("mm", "Myanmar"), + ("me", "Montenegro"), + ("mn", "Mongolia"), + ("mp", "Northern Mariana Islands"), + ("mz", "Mozambique"), + ("mr", "Mauritania"), + ("ms", "Montserrat"), + ("mq", "Martinique"), + ("mu", "Mauritius"), + ("mw", "Malawi"), + ("my", "Malaysia"), + ("yt", "Mayotte"), + ("na", "Namibia"), + ("nc", "New Caledonia"), + ("ne", "Niger"), + ("nf", "Norfolk Island"), + ("ng", "Nigeria"), + ("ni", "Nicaragua"), + ("nu", "Niue"), + ("nl", "Netherlands"), + ("no", "Norway"), + ("np", "Nepal"), + ("nr", "Nauru"), + ("nz", "New Zealand"), + ("om", "Oman"), + ("pk", "Pakistan"), + ("pa", "Panama"), + ("pn", "Pitcairn"), + ("pe", "Peru"), + ("ph", "Philippines"), + ("pw", "Palau"), + ("pg", "Papua New Guinea"), + ("pl", "Poland"), + ("pr", "Puerto Rico"), + ("kp", "Korea, Democratic People's Republic of"), + ("pt", "Portugal"), + ("py", "Paraguay"), + ("ps", "Palestine, State of"), + ("pf", "French Polynesia"), + ("qa", "Qatar"), + ("re", "Réunion"), + ("ro", "Romania"), + ("ru", "Russian Federation"), + ("rw", "Rwanda"), + ("sa", "Saudi Arabia"), + ("sd", "Sudan"), + ("sn", "Senegal"), + ("sg", "Singapore"), + ("gs", "South Georgia and the South Sandwich Islands"), + ("sh", "Saint Helena, Ascension and Tristan da Cunha"), + ("sj", "Svalbard and Jan Mayen"), + ("sb", "Solomon Islands"), + ("sl", "Sierra Leone"), + ("sv", "El Salvador"), + ("sm", "San Marino"), + ("so", "Somalia"), + ("pm", "Saint Pierre and Miquelon"), + ("rs", "Serbia"), + ("ss", "South Sudan"), + ("st", "Sao Tome and Principe"), + ("sr", "Suriname"), + ("sk", "Slovakia"), + ("si", "Slovenia"), + ("se", "Sweden"), + ("sz", "Swaziland"), + ("sx", "Sint Maarten (Dutch part)"), + ("sc", "Seychelles"), + ("sy", "Syrian Arab Republic"), + ("tc", "Turks and Caicos Islands"), + ("td", "Chad"), + ("tg", "Togo"), + ("th", "Thailand"), + ("tj", "Tajikistan"), + ("tk", "Tokelau"), + ("tm", "Turkmenistan"), + ("tl", "Timor-Leste"), + ("to", "Tonga"), + ("tt", "Trinidad and Tobago"), + ("tn", "Tunisia"), + ("tr", "Turkey"), + ("tv", "Tuvalu"), + ("tw", "Taiwan, Province of China"), + ("tz", "Tanzania, United Republic of"), + ("ug", "Uganda"), + ("ua", "Ukraine"), + ("um", "United States Minor Outlying Islands"), + ("uy", "Uruguay"), + ("us", "United States"), + ("uz", "Uzbekistan"), + ("va", "Holy See (Vatican City State)"), + ("vc", "Saint Vincent and the Grenadines"), + ("ve", "Venezuela, Bolivarian Republic of"), + ("vg", "Virgin Islands, British"), + ("vi", "Virgin Islands, U.S."), + ("vn", "Viet Nam"), + ("vu", "Vanuatu"), + ("wf", "Wallis and Futuna"), + ("ws", "Samoa"), + ("ye", "Yemen"), + ("za", "South Africa"), + ("zm", "Zambia"), + ("zw", "Zimbabwe"), + ], + help_text="Select origin country of the request that this rule will match against. This rule will only work if you use Cloudflare or CloudFront IP geolocation or if GeoIP2 module is configured.", + max_length=2, + ), + ), + ( + "segment", + modelcluster.fields.ParentalKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="wagtail_personalisation_origincountryrules", + to="wagtail_personalisation.Segment", + ), + ), ], options={ - 'verbose_name': 'origin country rule', + "verbose_name": "origin country rule", }, ), ] diff --git a/src/wagtail_personalisation/migrations/0025_auto_20190822_0627.py b/src/wagtail_personalisation/migrations/0025_auto_20190822_0627.py index b2e1b02a..ba6ca8af 100644 --- a/src/wagtail_personalisation/migrations/0025_auto_20190822_0627.py +++ b/src/wagtail_personalisation/migrations/0025_auto_20190822_0627.py @@ -4,15 +4,268 @@ class Migration(migrations.Migration): - dependencies = [ - ('wagtail_personalisation', '0024_origincountryrule'), + ("wagtail_personalisation", "0024_origincountryrule"), ] operations = [ migrations.AlterField( - model_name='origincountryrule', - name='country', - field=models.CharField(choices=[('aw', 'Aruba'), ('af', 'Afghanistan'), ('ao', 'Angola'), ('ai', 'Anguilla'), ('ax', 'Åland Islands'), ('al', 'Albania'), ('ad', 'Andorra'), ('ae', 'United Arab Emirates'), ('ar', 'Argentina'), ('am', 'Armenia'), ('as', 'American Samoa'), ('aq', 'Antarctica'), ('tf', 'French Southern Territories'), ('ag', 'Antigua and Barbuda'), ('au', 'Australia'), ('at', 'Austria'), ('az', 'Azerbaijan'), ('bi', 'Burundi'), ('be', 'Belgium'), ('bj', 'Benin'), ('bq', 'Bonaire, Sint Eustatius and Saba'), ('bf', 'Burkina Faso'), ('bd', 'Bangladesh'), ('bg', 'Bulgaria'), ('bh', 'Bahrain'), ('bs', 'Bahamas'), ('ba', 'Bosnia and Herzegovina'), ('bl', 'Saint Barthélemy'), ('by', 'Belarus'), ('bz', 'Belize'), ('bm', 'Bermuda'), ('bo', 'Bolivia, Plurinational State of'), ('br', 'Brazil'), ('bb', 'Barbados'), ('bn', 'Brunei Darussalam'), ('bt', 'Bhutan'), ('bv', 'Bouvet Island'), ('bw', 'Botswana'), ('cf', 'Central African Republic'), ('ca', 'Canada'), ('cc', 'Cocos (Keeling) Islands'), ('ch', 'Switzerland'), ('cl', 'Chile'), ('cn', 'China'), ('ci', "Côte d'Ivoire"), ('cm', 'Cameroon'), ('cd', 'Congo, The Democratic Republic of the'), ('cg', 'Congo'), ('ck', 'Cook Islands'), ('co', 'Colombia'), ('km', 'Comoros'), ('cv', 'Cabo Verde'), ('cr', 'Costa Rica'), ('cu', 'Cuba'), ('cw', 'Curaçao'), ('cx', 'Christmas Island'), ('ky', 'Cayman Islands'), ('cy', 'Cyprus'), ('cz', 'Czechia'), ('de', 'Germany'), ('dj', 'Djibouti'), ('dm', 'Dominica'), ('dk', 'Denmark'), ('do', 'Dominican Republic'), ('dz', 'Algeria'), ('ec', 'Ecuador'), ('eg', 'Egypt'), ('er', 'Eritrea'), ('eh', 'Western Sahara'), ('es', 'Spain'), ('ee', 'Estonia'), ('et', 'Ethiopia'), ('fi', 'Finland'), ('fj', 'Fiji'), ('fk', 'Falkland Islands (Malvinas)'), ('fr', 'France'), ('fo', 'Faroe Islands'), ('fm', 'Micronesia, Federated States of'), ('ga', 'Gabon'), ('gb', 'United Kingdom'), ('ge', 'Georgia'), ('gg', 'Guernsey'), ('gh', 'Ghana'), ('gi', 'Gibraltar'), ('gn', 'Guinea'), ('gp', 'Guadeloupe'), ('gm', 'Gambia'), ('gw', 'Guinea-Bissau'), ('gq', 'Equatorial Guinea'), ('gr', 'Greece'), ('gd', 'Grenada'), ('gl', 'Greenland'), ('gt', 'Guatemala'), ('gf', 'French Guiana'), ('gu', 'Guam'), ('gy', 'Guyana'), ('hk', 'Hong Kong'), ('hm', 'Heard Island and McDonald Islands'), ('hn', 'Honduras'), ('hr', 'Croatia'), ('ht', 'Haiti'), ('hu', 'Hungary'), ('id', 'Indonesia'), ('im', 'Isle of Man'), ('in', 'India'), ('io', 'British Indian Ocean Territory'), ('ie', 'Ireland'), ('ir', 'Iran, Islamic Republic of'), ('iq', 'Iraq'), ('is', 'Iceland'), ('il', 'Israel'), ('it', 'Italy'), ('jm', 'Jamaica'), ('je', 'Jersey'), ('jo', 'Jordan'), ('jp', 'Japan'), ('kz', 'Kazakhstan'), ('ke', 'Kenya'), ('kg', 'Kyrgyzstan'), ('kh', 'Cambodia'), ('ki', 'Kiribati'), ('kn', 'Saint Kitts and Nevis'), ('kr', 'Korea, Republic of'), ('kw', 'Kuwait'), ('la', "Lao People's Democratic Republic"), ('lb', 'Lebanon'), ('lr', 'Liberia'), ('ly', 'Libya'), ('lc', 'Saint Lucia'), ('li', 'Liechtenstein'), ('lk', 'Sri Lanka'), ('ls', 'Lesotho'), ('lt', 'Lithuania'), ('lu', 'Luxembourg'), ('lv', 'Latvia'), ('mo', 'Macao'), ('mf', 'Saint Martin (French part)'), ('ma', 'Morocco'), ('mc', 'Monaco'), ('md', 'Moldova, Republic of'), ('mg', 'Madagascar'), ('mv', 'Maldives'), ('mx', 'Mexico'), ('mh', 'Marshall Islands'), ('mk', 'North Macedonia'), ('ml', 'Mali'), ('mt', 'Malta'), ('mm', 'Myanmar'), ('me', 'Montenegro'), ('mn', 'Mongolia'), ('mp', 'Northern Mariana Islands'), ('mz', 'Mozambique'), ('mr', 'Mauritania'), ('ms', 'Montserrat'), ('mq', 'Martinique'), ('mu', 'Mauritius'), ('mw', 'Malawi'), ('my', 'Malaysia'), ('yt', 'Mayotte'), ('na', 'Namibia'), ('nc', 'New Caledonia'), ('ne', 'Niger'), ('nf', 'Norfolk Island'), ('ng', 'Nigeria'), ('ni', 'Nicaragua'), ('nu', 'Niue'), ('nl', 'Netherlands'), ('no', 'Norway'), ('np', 'Nepal'), ('nr', 'Nauru'), ('nz', 'New Zealand'), ('om', 'Oman'), ('pk', 'Pakistan'), ('pa', 'Panama'), ('pn', 'Pitcairn'), ('pe', 'Peru'), ('ph', 'Philippines'), ('pw', 'Palau'), ('pg', 'Papua New Guinea'), ('pl', 'Poland'), ('pr', 'Puerto Rico'), ('kp', "Korea, Democratic People's Republic of"), ('pt', 'Portugal'), ('py', 'Paraguay'), ('ps', 'Palestine, State of'), ('pf', 'French Polynesia'), ('qa', 'Qatar'), ('re', 'Réunion'), ('ro', 'Romania'), ('ru', 'Russian Federation'), ('rw', 'Rwanda'), ('sa', 'Saudi Arabia'), ('sd', 'Sudan'), ('sn', 'Senegal'), ('sg', 'Singapore'), ('gs', 'South Georgia and the South Sandwich Islands'), ('sh', 'Saint Helena, Ascension and Tristan da Cunha'), ('sj', 'Svalbard and Jan Mayen'), ('sb', 'Solomon Islands'), ('sl', 'Sierra Leone'), ('sv', 'El Salvador'), ('sm', 'San Marino'), ('so', 'Somalia'), ('pm', 'Saint Pierre and Miquelon'), ('rs', 'Serbia'), ('ss', 'South Sudan'), ('st', 'Sao Tome and Principe'), ('sr', 'Suriname'), ('sk', 'Slovakia'), ('si', 'Slovenia'), ('se', 'Sweden'), ('sz', 'Eswatini'), ('sx', 'Sint Maarten (Dutch part)'), ('sc', 'Seychelles'), ('sy', 'Syrian Arab Republic'), ('tc', 'Turks and Caicos Islands'), ('td', 'Chad'), ('tg', 'Togo'), ('th', 'Thailand'), ('tj', 'Tajikistan'), ('tk', 'Tokelau'), ('tm', 'Turkmenistan'), ('tl', 'Timor-Leste'), ('to', 'Tonga'), ('tt', 'Trinidad and Tobago'), ('tn', 'Tunisia'), ('tr', 'Turkey'), ('tv', 'Tuvalu'), ('tw', 'Taiwan, Province of China'), ('tz', 'Tanzania, United Republic of'), ('ug', 'Uganda'), ('ua', 'Ukraine'), ('um', 'United States Minor Outlying Islands'), ('uy', 'Uruguay'), ('us', 'United States'), ('uz', 'Uzbekistan'), ('va', 'Holy See (Vatican City State)'), ('vc', 'Saint Vincent and the Grenadines'), ('ve', 'Venezuela, Bolivarian Republic of'), ('vg', 'Virgin Islands, British'), ('vi', 'Virgin Islands, U.S.'), ('vn', 'Viet Nam'), ('vu', 'Vanuatu'), ('wf', 'Wallis and Futuna'), ('ws', 'Samoa'), ('ye', 'Yemen'), ('za', 'South Africa'), ('zm', 'Zambia'), ('zw', 'Zimbabwe')], help_text='Select origin country of the request that this rule will match against. This rule will only work if you use Cloudflare or CloudFront IP geolocation or if GeoIP2 module is configured.', max_length=2), + model_name="origincountryrule", + name="country", + field=models.CharField( + choices=[ + ("aw", "Aruba"), + ("af", "Afghanistan"), + ("ao", "Angola"), + ("ai", "Anguilla"), + ("ax", "Åland Islands"), + ("al", "Albania"), + ("ad", "Andorra"), + ("ae", "United Arab Emirates"), + ("ar", "Argentina"), + ("am", "Armenia"), + ("as", "American Samoa"), + ("aq", "Antarctica"), + ("tf", "French Southern Territories"), + ("ag", "Antigua and Barbuda"), + ("au", "Australia"), + ("at", "Austria"), + ("az", "Azerbaijan"), + ("bi", "Burundi"), + ("be", "Belgium"), + ("bj", "Benin"), + ("bq", "Bonaire, Sint Eustatius and Saba"), + ("bf", "Burkina Faso"), + ("bd", "Bangladesh"), + ("bg", "Bulgaria"), + ("bh", "Bahrain"), + ("bs", "Bahamas"), + ("ba", "Bosnia and Herzegovina"), + ("bl", "Saint Barthélemy"), + ("by", "Belarus"), + ("bz", "Belize"), + ("bm", "Bermuda"), + ("bo", "Bolivia, Plurinational State of"), + ("br", "Brazil"), + ("bb", "Barbados"), + ("bn", "Brunei Darussalam"), + ("bt", "Bhutan"), + ("bv", "Bouvet Island"), + ("bw", "Botswana"), + ("cf", "Central African Republic"), + ("ca", "Canada"), + ("cc", "Cocos (Keeling) Islands"), + ("ch", "Switzerland"), + ("cl", "Chile"), + ("cn", "China"), + ("ci", "Côte d'Ivoire"), + ("cm", "Cameroon"), + ("cd", "Congo, The Democratic Republic of the"), + ("cg", "Congo"), + ("ck", "Cook Islands"), + ("co", "Colombia"), + ("km", "Comoros"), + ("cv", "Cabo Verde"), + ("cr", "Costa Rica"), + ("cu", "Cuba"), + ("cw", "Curaçao"), + ("cx", "Christmas Island"), + ("ky", "Cayman Islands"), + ("cy", "Cyprus"), + ("cz", "Czechia"), + ("de", "Germany"), + ("dj", "Djibouti"), + ("dm", "Dominica"), + ("dk", "Denmark"), + ("do", "Dominican Republic"), + ("dz", "Algeria"), + ("ec", "Ecuador"), + ("eg", "Egypt"), + ("er", "Eritrea"), + ("eh", "Western Sahara"), + ("es", "Spain"), + ("ee", "Estonia"), + ("et", "Ethiopia"), + ("fi", "Finland"), + ("fj", "Fiji"), + ("fk", "Falkland Islands (Malvinas)"), + ("fr", "France"), + ("fo", "Faroe Islands"), + ("fm", "Micronesia, Federated States of"), + ("ga", "Gabon"), + ("gb", "United Kingdom"), + ("ge", "Georgia"), + ("gg", "Guernsey"), + ("gh", "Ghana"), + ("gi", "Gibraltar"), + ("gn", "Guinea"), + ("gp", "Guadeloupe"), + ("gm", "Gambia"), + ("gw", "Guinea-Bissau"), + ("gq", "Equatorial Guinea"), + ("gr", "Greece"), + ("gd", "Grenada"), + ("gl", "Greenland"), + ("gt", "Guatemala"), + ("gf", "French Guiana"), + ("gu", "Guam"), + ("gy", "Guyana"), + ("hk", "Hong Kong"), + ("hm", "Heard Island and McDonald Islands"), + ("hn", "Honduras"), + ("hr", "Croatia"), + ("ht", "Haiti"), + ("hu", "Hungary"), + ("id", "Indonesia"), + ("im", "Isle of Man"), + ("in", "India"), + ("io", "British Indian Ocean Territory"), + ("ie", "Ireland"), + ("ir", "Iran, Islamic Republic of"), + ("iq", "Iraq"), + ("is", "Iceland"), + ("il", "Israel"), + ("it", "Italy"), + ("jm", "Jamaica"), + ("je", "Jersey"), + ("jo", "Jordan"), + ("jp", "Japan"), + ("kz", "Kazakhstan"), + ("ke", "Kenya"), + ("kg", "Kyrgyzstan"), + ("kh", "Cambodia"), + ("ki", "Kiribati"), + ("kn", "Saint Kitts and Nevis"), + ("kr", "Korea, Republic of"), + ("kw", "Kuwait"), + ("la", "Lao People's Democratic Republic"), + ("lb", "Lebanon"), + ("lr", "Liberia"), + ("ly", "Libya"), + ("lc", "Saint Lucia"), + ("li", "Liechtenstein"), + ("lk", "Sri Lanka"), + ("ls", "Lesotho"), + ("lt", "Lithuania"), + ("lu", "Luxembourg"), + ("lv", "Latvia"), + ("mo", "Macao"), + ("mf", "Saint Martin (French part)"), + ("ma", "Morocco"), + ("mc", "Monaco"), + ("md", "Moldova, Republic of"), + ("mg", "Madagascar"), + ("mv", "Maldives"), + ("mx", "Mexico"), + ("mh", "Marshall Islands"), + ("mk", "North Macedonia"), + ("ml", "Mali"), + ("mt", "Malta"), + ("mm", "Myanmar"), + ("me", "Montenegro"), + ("mn", "Mongolia"), + ("mp", "Northern Mariana Islands"), + ("mz", "Mozambique"), + ("mr", "Mauritania"), + ("ms", "Montserrat"), + ("mq", "Martinique"), + ("mu", "Mauritius"), + ("mw", "Malawi"), + ("my", "Malaysia"), + ("yt", "Mayotte"), + ("na", "Namibia"), + ("nc", "New Caledonia"), + ("ne", "Niger"), + ("nf", "Norfolk Island"), + ("ng", "Nigeria"), + ("ni", "Nicaragua"), + ("nu", "Niue"), + ("nl", "Netherlands"), + ("no", "Norway"), + ("np", "Nepal"), + ("nr", "Nauru"), + ("nz", "New Zealand"), + ("om", "Oman"), + ("pk", "Pakistan"), + ("pa", "Panama"), + ("pn", "Pitcairn"), + ("pe", "Peru"), + ("ph", "Philippines"), + ("pw", "Palau"), + ("pg", "Papua New Guinea"), + ("pl", "Poland"), + ("pr", "Puerto Rico"), + ("kp", "Korea, Democratic People's Republic of"), + ("pt", "Portugal"), + ("py", "Paraguay"), + ("ps", "Palestine, State of"), + ("pf", "French Polynesia"), + ("qa", "Qatar"), + ("re", "Réunion"), + ("ro", "Romania"), + ("ru", "Russian Federation"), + ("rw", "Rwanda"), + ("sa", "Saudi Arabia"), + ("sd", "Sudan"), + ("sn", "Senegal"), + ("sg", "Singapore"), + ("gs", "South Georgia and the South Sandwich Islands"), + ("sh", "Saint Helena, Ascension and Tristan da Cunha"), + ("sj", "Svalbard and Jan Mayen"), + ("sb", "Solomon Islands"), + ("sl", "Sierra Leone"), + ("sv", "El Salvador"), + ("sm", "San Marino"), + ("so", "Somalia"), + ("pm", "Saint Pierre and Miquelon"), + ("rs", "Serbia"), + ("ss", "South Sudan"), + ("st", "Sao Tome and Principe"), + ("sr", "Suriname"), + ("sk", "Slovakia"), + ("si", "Slovenia"), + ("se", "Sweden"), + ("sz", "Eswatini"), + ("sx", "Sint Maarten (Dutch part)"), + ("sc", "Seychelles"), + ("sy", "Syrian Arab Republic"), + ("tc", "Turks and Caicos Islands"), + ("td", "Chad"), + ("tg", "Togo"), + ("th", "Thailand"), + ("tj", "Tajikistan"), + ("tk", "Tokelau"), + ("tm", "Turkmenistan"), + ("tl", "Timor-Leste"), + ("to", "Tonga"), + ("tt", "Trinidad and Tobago"), + ("tn", "Tunisia"), + ("tr", "Turkey"), + ("tv", "Tuvalu"), + ("tw", "Taiwan, Province of China"), + ("tz", "Tanzania, United Republic of"), + ("ug", "Uganda"), + ("ua", "Ukraine"), + ("um", "United States Minor Outlying Islands"), + ("uy", "Uruguay"), + ("us", "United States"), + ("uz", "Uzbekistan"), + ("va", "Holy See (Vatican City State)"), + ("vc", "Saint Vincent and the Grenadines"), + ("ve", "Venezuela, Bolivarian Republic of"), + ("vg", "Virgin Islands, British"), + ("vi", "Virgin Islands, U.S."), + ("vn", "Viet Nam"), + ("vu", "Vanuatu"), + ("wf", "Wallis and Futuna"), + ("ws", "Samoa"), + ("ye", "Yemen"), + ("za", "South Africa"), + ("zm", "Zambia"), + ("zw", "Zimbabwe"), + ], + help_text="Select origin country of the request that this rule will match against. This rule will only work if you use Cloudflare or CloudFront IP geolocation or if GeoIP2 module is configured.", + max_length=2, + ), ), ] diff --git a/src/wagtail_personalisation/models.py b/src/wagtail_personalisation/models.py index 1b28dd4d..1fbbaa77 100644 --- a/src/wagtail_personalisation/models.py +++ b/src/wagtail_personalisation/models.py @@ -8,11 +8,10 @@ from django.template.defaultfilters import slugify from django.utils.functional import cached_property from django.utils.safestring import mark_safe -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import gettext_lazy as _ from modelcluster.models import ClusterableModel -from wagtail.admin.edit_handlers import ( - FieldPanel, FieldRowPanel, InlinePanel, MultiFieldPanel) -from wagtail.core.models import Page +from wagtail.admin.panels import FieldPanel, FieldRowPanel, InlinePanel, MultiFieldPanel +from wagtail.models import Page from wagtail_personalisation.rules import AbstractBaseRule from wagtail_personalisation.utils import count_active_days @@ -22,7 +21,7 @@ class RulePanel(InlinePanel): def on_model_bound(self): - self.relation_name = self.relation_name.replace('_related', 's') + self.relation_name = self.relation_name.replace("_related", "s") self.db_field = self.model._meta.get_field(self.relation_name) manager = getattr(self.model, self.relation_name) self.related = manager.rel @@ -35,20 +34,21 @@ def enabled(self): class Segment(ClusterableModel): """The segment model.""" - STATUS_ENABLED = 'enabled' - STATUS_DISABLED = 'disabled' + + STATUS_ENABLED = "enabled" + STATUS_DISABLED = "disabled" STATUS_CHOICES = ( - (STATUS_ENABLED, _('Enabled')), - (STATUS_DISABLED, _('Disabled')), + (STATUS_ENABLED, _("Enabled")), + (STATUS_DISABLED, _("Disabled")), ) - TYPE_DYNAMIC = 'dynamic' - TYPE_STATIC = 'static' + TYPE_DYNAMIC = "dynamic" + TYPE_STATIC = "static" TYPE_CHOICES = ( - (TYPE_DYNAMIC, _('Dynamic')), - (TYPE_STATIC, _('Static')), + (TYPE_DYNAMIC, _("Dynamic")), + (TYPE_STATIC, _("Static")), ) name = models.CharField(max_length=255) @@ -58,18 +58,22 @@ class Segment(ClusterableModel): 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) + max_length=20, choices=STATUS_CHOICES, default=STATUS_ENABLED + ) persistent = models.BooleanField( - default=False, help_text=_("Should the segment persist between visits?")) + default=False, help_text=_("Should the segment persist between visits?") + ) match_any = models.BooleanField( default=False, - help_text=_("Should the segment match all the rules or just one of them?") + help_text=_("Should the segment match all the rules or just one of them?"), ) type = models.CharField( max_length=20, choices=TYPE_CHOICES, default=TYPE_DYNAMIC, - help_text=mark_safe(_(""" + help_text=mark_safe( + _( + """

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 @@ -77,37 +81,42 @@ class Segment(ClusterableModel): 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. - """)) + """ + ) + ), ) count = models.PositiveSmallIntegerField( default=0, help_text=_( "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." - ) + ), ) static_users = models.ManyToManyField( settings.AUTH_USER_MODEL, ) excluded_users = models.ManyToManyField( settings.AUTH_USER_MODEL, - help_text=_("Users that matched the rules but were excluded from the " - "segment for some reason e.g. randomisation"), - related_name="excluded_segments" + help_text=_( + "Users that matched the rules but were excluded from the " + "segment for some reason e.g. randomisation" + ), + related_name="excluded_segments", ) matched_users_count = models.PositiveIntegerField(default=0, editable=False) matched_count_updated_at = models.DateTimeField(null=True, editable=False) randomisation_percent = models.PositiveSmallIntegerField( - null=True, blank=True, default=None, + null=True, + blank=True, + default=None, help_text=_( "If this number is set each user matching the rules will " "have this percentage chance of being placed in the segment." - ), validators=[ - MaxValueValidator(100), - MinValueValidator(0) - ]) + ), + validators=[MaxValueValidator(100), MinValueValidator(0)], + ) objects = SegmentQuerySet.as_manager() @@ -115,26 +124,37 @@ class Segment(ClusterableModel): def __init__(self, *args, **kwargs): Segment.panels = [ - MultiFieldPanel([ - FieldPanel('name', classname="title"), - FieldRowPanel([ - FieldPanel('status'), - FieldPanel('persistent'), - ]), - FieldPanel('match_any'), - FieldPanel('type', widget=forms.RadioSelect), - FieldPanel('count', classname='count_field'), - FieldPanel('randomisation_percent', classname='percent_field'), - ], heading="Segment"), - MultiFieldPanel([ - RulePanel( - "{}_related".format(rule_model._meta.db_table), - label='{}{}'.format( - rule_model._meta.verbose_name, - ' ({})'.format(_('Static compatible')) if rule_model.static else '' + MultiFieldPanel( + [ + FieldPanel("name", classname="title"), + FieldRowPanel( + [ + FieldPanel("status"), + FieldPanel("persistent"), + ] ), - ) for rule_model in AbstractBaseRule.__subclasses__() - ], heading=_("Rules")), + FieldPanel("match_any"), + FieldPanel("type", widget=forms.RadioSelect), + FieldPanel("count", classname="count_field"), + FieldPanel("randomisation_percent", classname="percent_field"), + ], + heading="Segment", + ), + MultiFieldPanel( + [ + RulePanel( + "{}_related".format(rule_model._meta.db_table), + label="{}{}".format( + rule_model._meta.verbose_name, + " ({})".format(_("Static compatible")) + if rule_model.static + else "", + ), + ) + for rule_model in AbstractBaseRule.__subclasses__() + ], + heading=_("Rules"), + ), ] super(Segment, self).__init__(*args, **kwargs) @@ -179,20 +199,21 @@ def get_rules(self): """Retrieve all rules in the segment.""" segment_rules = [] for rule_model in AbstractBaseRule.get_descendant_models(): - segment_rules.extend( - rule_model._default_manager.filter(segment=self)) + segment_rules.extend(rule_model._default_manager.filter(segment=self)) return segment_rules def toggle(self, save=True): self.status = ( - self.STATUS_ENABLED if self.status == self.STATUS_DISABLED - else self.STATUS_DISABLED) + self.STATUS_ENABLED + if self.status == self.STATUS_DISABLED + else self.STATUS_DISABLED + ) if save: self.save() def randomise_into_segment(self): - """ Returns True if randomisation_percent is not set or it generates + """Returns True if randomisation_percent is not set or it generates a random number less than the randomisation_percent This is so there is some randomisation in which users are added to the segment @@ -210,21 +231,24 @@ 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, models.PROTECT, related_name='personalisable_canonical_metadata', - 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, models.CASCADE, related_name='_personalisable_page_metadata', - null=True + Page, models.CASCADE, related_name="_personalisable_page_metadata", null=True ) - segment = models.ForeignKey(Segment, models.PROTECT, null=True, - related_name='page_metadata') + segment = models.ForeignKey( + Segment, models.PROTECT, null=True, related_name="page_metadata" + ) @cached_property def has_variants(self): @@ -241,10 +265,12 @@ def has_variants(self): @cached_property def variants_metadata(self): return ( - PersonalisablePageMetadata.objects - .filter(canonical_page_id=self.canonical_page_id) + PersonalisablePageMetadata.objects.filter( + canonical_page_id=self.canonical_page_id + ) .exclude(variant_id=self.variant_id) - .exclude(variant_id=self.canonical_page_id)) + .exclude(variant_id=self.canonical_page_id) + ) @cached_property def is_canonical(self): @@ -265,33 +291,31 @@ def copy_for_segment(self, segment): slug = "{}-{}".format(page.slug, segment.encoded_name()) title = "{} ({})".format(page.title, segment.name) update_attrs = { - 'title': title, - 'slug': slug, - 'live': False, + "title": title, + "slug": slug, + "live": False, } with transaction.atomic(): new_page = self.canonical_page.copy( - update_attrs=update_attrs, copy_revisions=False) + update_attrs=update_attrs, copy_revisions=False + ) PersonalisablePageMetadata.objects.create( - canonical_page=page, - variant=new_page, - segment=segment) + canonical_page=page, variant=new_page, segment=segment + ) return new_page def metadata_for_segments(self, segments): - return ( - self.__class__.objects - .filter( - canonical_page_id=self.canonical_page_id, - segment__in=segments)) + return self.__class__.objects.filter( + canonical_page_id=self.canonical_page_id, segment__in=segments + ) def get_unused_segments(self): if self.is_canonical: - return ( - Segment.objects - .exclude(page_metadata__canonical_page_id=self.canonical_page_id)) + return Segment.objects.exclude( + page_metadata__canonical_page_id=self.canonical_page_id + ) return Segment.objects.none() @@ -307,7 +331,8 @@ def personalisation_metadata(self): metadata = self._personalisable_page_metadata except AttributeError: metadata = PersonalisablePageMetadata.objects.create( - canonical_page=self, variant=self) + canonical_page=self, variant=self + ) return metadata def get_sitemap_urls(self, request=None): diff --git a/src/wagtail_personalisation/rules.py b/src/wagtail_personalisation/rules.py index b4a17293..b54602c3 100644 --- a/src/wagtail_personalisation/rules.py +++ b/src/wagtail_personalisation/rules.py @@ -11,11 +11,10 @@ from django.template.defaultfilters import slugify from django.test.client import RequestFactory from django.utils import timezone -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import gettext_lazy as _ from modelcluster.fields import ParentalKey from user_agents import parse -from wagtail.admin.edit_handlers import ( - FieldPanel, FieldRowPanel, PageChooserPanel) +from wagtail.admin.panels import FieldPanel, FieldRowPanel from wagtail_personalisation.utils import get_client_ip @@ -27,32 +26,35 @@ def get_geoip_module(): try: from django.contrib.gis.geoip2 import GeoIP2 + return GeoIP2 except ImportError: logger.exception( - 'GeoIP module is disabled. To use GeoIP for the origin\n' - 'country personaliastion rule please set it up as per ' - 'documentation:\n' - 'https://docs.djangoproject.com/en/stable/ref/contrib/gis/' - 'geoip2/.\n' - 'Wagtail-personalisation also works with Cloudflare and\n' - 'CloudFront country detection, so you should not see this\n' - 'warning if you use one of those.') + "GeoIP module is disabled. To use GeoIP for the origin\n" + "country personaliastion rule please set it up as per " + "documentation:\n" + "https://docs.djangoproject.com/en/stable/ref/contrib/gis/" + "geoip2/.\n" + "Wagtail-personalisation also works with Cloudflare and\n" + "CloudFront country detection, so you should not see this\n" + "warning if you use one of those." + ) class AbstractBaseRule(models.Model): """Base for creating rules to segment users with.""" - icon = 'fa-circle-o' + + icon = "fa-circle-o" static = False segment = ParentalKey( - 'wagtail_personalisation.Segment', + "wagtail_personalisation.Segment", related_name="%(app_label)s_%(class)ss", ) class Meta: abstract = True - verbose_name = 'Abstract segmentation rule' + verbose_name = "Abstract segmentation rule" def __str__(self): return str(self._meta.verbose_name) @@ -74,16 +76,17 @@ def description(self): """ description = { - 'title': _('Abstract segmentation rule'), - 'value': '', + "title": _("Abstract segmentation rule"), + "value": "", } return description @classmethod def get_descendant_models(cls): - return [model for model in apps.get_models() - if issubclass(model, AbstractBaseRule)] + return [ + model for model in apps.get_models() if issubclass(model, AbstractBaseRule) + ] class TimeRule(AbstractBaseRule): @@ -93,30 +96,32 @@ class TimeRule(AbstractBaseRule): set start time and end time. """ - icon = 'fa-clock-o' + + icon = "fa-clock-o" start_time = models.TimeField(_("Starting time")) end_time = models.TimeField(_("Ending time")) panels = [ - FieldRowPanel([ - FieldPanel('start_time'), - FieldPanel('end_time'), - ]), + FieldRowPanel( + [ + FieldPanel("start_time"), + FieldPanel("end_time"), + ] + ), ] class Meta: - verbose_name = _('Time Rule') + verbose_name = _("Time Rule") def test_user(self, request=None): return self.start_time <= timezone.now().time() <= self.end_time def description(self): return { - 'title': _('These users visit between'), - 'value': _('{} and {}').format( - self.start_time.strftime("%H:%M"), - self.end_time.strftime("%H:%M") + "title": _("These users visit between"), + "value": _("{} and {}").format( + self.start_time.strftime("%H:%M"), self.end_time.strftime("%H:%M") ), } @@ -128,7 +133,8 @@ class DayRule(AbstractBaseRule): set in the rule. """ - icon = 'fa-calendar-check-o' + + icon = "fa-calendar-check-o" mon = models.BooleanField(_("Monday"), default=False) tue = models.BooleanField(_("Tuesday"), default=False) @@ -139,34 +145,39 @@ class DayRule(AbstractBaseRule): sun = models.BooleanField(_("Sunday"), default=False) panels = [ - FieldPanel('mon'), - FieldPanel('tue'), - FieldPanel('wed'), - FieldPanel('thu'), - FieldPanel('fri'), - FieldPanel('sat'), - FieldPanel('sun'), + FieldPanel("mon"), + FieldPanel("tue"), + FieldPanel("wed"), + FieldPanel("thu"), + FieldPanel("fri"), + FieldPanel("sat"), + FieldPanel("sun"), ] class Meta: - verbose_name = _('Day Rule') + verbose_name = _("Day Rule") def test_user(self, request=None): - return [self.mon, self.tue, self.wed, self.thu, - self.fri, self.sat, self.sun][timezone.now().date().weekday()] + return [self.mon, self.tue, self.wed, self.thu, self.fri, self.sat, self.sun][ + timezone.now().date().weekday() + ] def description(self): days = ( - ('mon', self.mon), ('tue', self.tue), ('wed', self.wed), - ('thu', self.thu), ('fri', self.fri), ('sat', self.sat), - ('sun', self.sun), + ("mon", self.mon), + ("tue", self.tue), + ("wed", self.wed), + ("thu", self.thu), + ("fri", self.fri), + ("sat", self.sat), + ("sun", self.sun), ) chosen_days = [day_name for day_name, chosen in days if chosen] return { - 'title': _('These users visit on'), - 'value': ", ".join([day for day in chosen_days]).title(), + "title": _("These users visit on"), + "value": ", ".join([day for day in chosen_days]).title(), } @@ -177,32 +188,32 @@ class ReferralRule(AbstractBaseRule): the set regex test. """ - icon = 'fa-globe' - regex_string = models.TextField( - _("Regular expression to match the referrer")) + icon = "fa-globe" + + regex_string = models.TextField(_("Regular expression to match the referrer")) panels = [ - FieldPanel('regex_string'), + FieldPanel("regex_string"), ] class Meta: - verbose_name = _('Referral Rule') + verbose_name = _("Referral Rule") def test_user(self, request): pattern = re.compile(self.regex_string) - if 'HTTP_REFERER' in request.META: - referer = request.META['HTTP_REFERER'] + if "HTTP_REFERER" in request.META: + referer = request.META["HTTP_REFERER"] if pattern.search(referer): return True return False def description(self): return { - 'title': _('These visits originate from'), - 'value': self.regex_string, - 'code': True + "title": _("These visits originate from"), + "value": self.regex_string, + "code": True, } @@ -214,48 +225,55 @@ class VisitCountRule(AbstractBaseRule): when visiting the set page. """ - icon = 'fa-calculator' + + icon = "fa-calculator" static = True OPERATOR_CHOICES = ( - ('more_than', _("More than")), - ('less_than', _("Less than")), - ('equal_to', _("Equal to")), + ("more_than", _("More than")), + ("less_than", _("Less than")), + ("equal_to", _("Equal to")), + ) + operator = models.CharField( + max_length=20, choices=OPERATOR_CHOICES, default="more_than" ) - operator = models.CharField(max_length=20, - choices=OPERATOR_CHOICES, default="more_than") count = models.PositiveSmallIntegerField(default=0, null=True) counted_page = models.ForeignKey( - 'wagtailcore.Page', + "wagtailcore.Page", null=False, blank=False, on_delete=models.CASCADE, - related_name='+', + related_name="+", ) panels = [ - PageChooserPanel('counted_page'), - FieldRowPanel([ - FieldPanel('operator'), - FieldPanel('count'), - ]), + FieldPanel("counted_page"), + FieldRowPanel( + [ + FieldPanel("operator"), + FieldPanel("count"), + ] + ), ] class Meta: - verbose_name = _('Visit count Rule') + verbose_name = _("Visit count Rule") def _get_user_session(self, user): sessions = Session.objects.iterator() for session in sessions: session_data = session.get_decoded() - if session_data.get('_auth_user_id') == str(user.id): + if session_data.get("_auth_user_id") == str(user.id): return SessionStore(session_key=session.session_key) return SessionStore() def test_user(self, request, user=None): # Local import for cyclic import from wagtail_personalisation.adapters import ( - get_segment_adapter, SessionSegmentsAdapter, SEGMENT_ADAPTER_CLASS) + SEGMENT_ADAPTER_CLASS, + SessionSegmentsAdapter, + get_segment_adapter, + ) # Django formsets don't honour 'required' fields so check rule is valid try: @@ -265,7 +283,7 @@ def test_user(self, request, user=None): if user: # Create a fake request so we can use the adapter - request = RequestFactory().get('/') + request = RequestFactory().get("/") request.user = user # If we're using the session adapter check for an active session @@ -297,13 +315,8 @@ def test_user(self, request, user=None): def description(self): return { - 'title': _('These users visited {}').format( - self.counted_page - ), - 'value': _('{} {} times').format( - self.get_operator_display(), - self.count - ), + "title": _("These users visited {}").format(self.counted_page), + "value": _("{} {} times").format(self.get_operator_display(), self.count), } def get_column_header(self): @@ -312,10 +325,13 @@ def get_column_header(self): def get_user_info_string(self, user): # Local import for cyclic import from wagtail_personalisation.adapters import ( - get_segment_adapter, SessionSegmentsAdapter, SEGMENT_ADAPTER_CLASS) + SEGMENT_ADAPTER_CLASS, + SessionSegmentsAdapter, + get_segment_adapter, + ) # Create a fake request so we can use the adapter - request = RequestFactory().get('/') + request = RequestFactory().get("/") request.user = user # If we're using the session adapter check for an active session @@ -336,32 +352,28 @@ class QueryRule(AbstractBaseRule): present in the request query. """ - icon = 'fa-link' - parameter = models.SlugField(_("The query parameter to search for"), - max_length=20) - value = models.SlugField(_("The value of the parameter to match"), - max_length=20) + icon = "fa-link" + + parameter = models.SlugField(_("The query parameter to search for"), max_length=20) + value = models.SlugField(_("The value of the parameter to match"), max_length=20) panels = [ - FieldPanel('parameter'), - FieldPanel('value'), + FieldPanel("parameter"), + FieldPanel("value"), ] class Meta: - verbose_name = _('Query Rule') + verbose_name = _("Query Rule") def test_user(self, request): - return request.GET.get(self.parameter, '') == self.value + return request.GET.get(self.parameter, "") == self.value def description(self): return { - 'title': _('These users used a URL with the query'), - 'value': _('?{}={}').format( - self.parameter, - self.value - ), - 'code': True + "title": _("These users used a URL with the query"), + "value": _("?{}={}").format(self.parameter, self.value), + "code": True, } @@ -372,23 +384,24 @@ class DeviceRule(AbstractBaseRule): in the request user agent headers. """ - icon = 'fa-tablet' + + icon = "fa-tablet" mobile = models.BooleanField(_("Mobile phone"), default=False) tablet = models.BooleanField(_("Tablet"), default=False) desktop = models.BooleanField(_("Desktop"), default=False) panels = [ - FieldPanel('mobile'), - FieldPanel('tablet'), - FieldPanel('desktop'), + FieldPanel("mobile"), + FieldPanel("tablet"), + FieldPanel("desktop"), ] class Meta: - verbose_name = _('Device Rule') + verbose_name = _("Device Rule") def test_user(self, request=None): - ua_header = request.META['HTTP_USER_AGENT'] + ua_header = request.META["HTTP_USER_AGENT"] user_agent = parse(ua_header) if user_agent.is_mobile: @@ -407,29 +420,31 @@ class UserIsLoggedInRule(AbstractBaseRule): Matches when the user is authenticated. """ - icon = 'fa-user' + + icon = "fa-user" is_logged_in = models.BooleanField(default=False) panels = [ - FieldPanel('is_logged_in'), + FieldPanel("is_logged_in"), ] class Meta: - verbose_name = _('Logged in Rule') + verbose_name = _("Logged in Rule") def test_user(self, request=None): return request.user.is_authenticated == self.is_logged_in def description(self): return { - 'title': _('These visitors are'), - 'value': _('Logged in') if self.is_logged_in else _('Not logged in'), + "title": _("These visitors are"), + "value": _("Logged in") if self.is_logged_in else _("Not logged in"), } -COUNTRY_CHOICES = [(country.alpha_2.lower(), country.name) - for country in pycountry.countries] +COUNTRY_CHOICES = [ + (country.alpha_2.lower(), country.name) for country in pycountry.countries +] class OriginCountryRule(AbstractBaseRule): @@ -439,12 +454,16 @@ class OriginCountryRule(AbstractBaseRule): Using this rule requires setting up GeoIP2 on Django or using CloudFlare or CloudFront geolocation detection. """ + country = models.CharField( - max_length=2, choices=COUNTRY_CHOICES, - help_text=_("Select origin country of the request that this rule will " - "match against. This rule will only work if you use " - "Cloudflare or CloudFront IP geolocation or if GeoIP2 " - "module is configured.") + max_length=2, + choices=COUNTRY_CHOICES, + help_text=_( + "Select origin country of the request that this rule will " + "match against. This rule will only work if you use " + "Cloudflare or CloudFront IP geolocation or if GeoIP2 " + "module is configured." + ), ) class Meta: @@ -458,13 +477,13 @@ def get_cloudflare_country(self, request): https://support.cloudflare.com/hc/en-us/articles/200168236-What-does-Cloudflare-IP-Geolocation-do- """ try: - return request.META['HTTP_CF_IPCOUNTRY'].lower() + return request.META["HTTP_CF_IPCOUNTRY"].lower() except KeyError: pass def get_cloudfront_country(self, request): try: - return request.META['HTTP_CLOUDFRONT_VIEWER_COUNTRY'].lower() + return request.META["HTTP_CLOUDFRONT_VIEWER_COUNTRY"].lower() except KeyError: pass @@ -487,4 +506,4 @@ def get_country(self, request): return result def test_user(self, request=None): - return (self.get_country(request) or '') == self.country.lower() + return (self.get_country(request) or "") == self.country.lower() diff --git a/src/wagtail_personalisation/templatetags/wagtail_personalisation_filters.py b/src/wagtail_personalisation/templatetags/wagtail_personalisation_filters.py index d408aea9..b85d5b1a 100644 --- a/src/wagtail_personalisation/templatetags/wagtail_personalisation_filters.py +++ b/src/wagtail_personalisation/templatetags/wagtail_personalisation_filters.py @@ -5,6 +5,6 @@ register = Library() -@register.filter(name='days_since') +@register.filter(name="days_since") def active_days(enable_date, disable_date): return count_active_days(enable_date, disable_date) diff --git a/src/wagtail_personalisation/templatetags/wagtail_personalisation_tags.py b/src/wagtail_personalisation/templatetags/wagtail_personalisation_tags.py index 6238789e..eb081169 100644 --- a/src/wagtail_personalisation/templatetags/wagtail_personalisation_tags.py +++ b/src/wagtail_personalisation/templatetags/wagtail_personalisation_tags.py @@ -15,17 +15,17 @@ def do_segment(parser, token): tag_name, _, kwargs = parse_tag(token, parser) # If no segment is provided this block will raise an error - if set(kwargs.keys()) != {'name'}: + if set(kwargs.keys()) != {"name"}: usage = '{% segment name="segmentname" %} ... {% endsegment %}' raise TemplateSyntaxError("Usage: %s" % usage) - nodelist = parser.parse(('endsegment',)) + nodelist = parser.parse(("endsegment",)) parser.delete_first_token() - return SegmentNode(nodelist, name=kwargs['name']) + return SegmentNode(nodelist, name=kwargs["name"]) -register.tag('segment', do_segment) +register.tag("segment", do_segment) class SegmentNode(template.Node): @@ -36,6 +36,7 @@ class SegmentNode(template.Node): If not it will return nothing """ + def __init__(self, nodelist, name): self.nodelist = nodelist self.name = name @@ -48,10 +49,10 @@ def render(self, context): return "" # Check if user has segment - adapter = get_segment_adapter(context['request']) + adapter = get_segment_adapter(context["request"]) user_segment = adapter.get_segment_by_id(segment_id=segment.pk) if not user_segment: - return '' + return "" content = self.nodelist.render(context) content = mark_safe(content) diff --git a/src/wagtail_personalisation/utils.py b/src/wagtail_personalisation/utils.py index d8181822..35dcbc00 100644 --- a/src/wagtail_personalisation/utils.py +++ b/src/wagtail_personalisation/utils.py @@ -36,7 +36,7 @@ def create_segment_dictionary(segment): "encoded_name": segment.encoded_name(), "id": segment.pk, "timestamp": int(time.time()), - "persistent": segment.persistent + "persistent": segment.persistent, } @@ -107,9 +107,10 @@ def exclude_variants(pages): :rtype: QuerySet """ from wagtail_personalisation.models import PersonalisablePageMetadata + excluded_variant_pages = PersonalisablePageMetadata.objects.exclude( - canonical_page_id=F('variant_id') - ).values_list('variant_id') + canonical_page_id=F("variant_id") + ).values_list("variant_id") return pages.exclude(pk__in=excluded_variant_pages) @@ -128,7 +129,7 @@ def get_client_ip(request): else: return func(request) try: - x_forwarded_for = request.META['HTTP_X_FORWARDED_FOR'] - return x_forwarded_for.split(',')[-1].strip() + x_forwarded_for = request.META["HTTP_X_FORWARDED_FOR"] + return x_forwarded_for.split(",")[-1].strip() except KeyError: - return request.META['REMOTE_ADDR'] + return request.META["REMOTE_ADDR"] diff --git a/src/wagtail_personalisation/views.py b/src/wagtail_personalisation/views.py index 0765230f..ae2e66a8 100644 --- a/src/wagtail_personalisation/views.py +++ b/src/wagtail_personalisation/views.py @@ -3,14 +3,13 @@ from django import forms from django.core.exceptions import PermissionDenied from django.db import transaction -from django.http import ( - HttpResponse, HttpResponseForbidden, HttpResponseRedirect) +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 django.utils.translation import gettext_lazy as _ from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register from wagtail.contrib.modeladmin.views import DeleteView, IndexView -from wagtail.core.models import Page +from wagtail.models import Page from wagtail_personalisation.models import Segment from wagtail_personalisation.utils import can_delete_pages @@ -18,41 +17,42 @@ class SegmentModelIndexView(IndexView): """Placeholder for additional list functionality.""" + pass class SegmentModelDashboardView(IndexView): """Additional dashboard functionality.""" + def media(self): return forms.Media( - css={'all': ['css/dashboard.css']}, - js=['js/commons.js', 'js/dashboard.js'] + css={"all": ["css/dashboard.css"]}, js=["js/commons.js", "js/dashboard.js"] ) def get_template_names(self): return [ - 'modeladmin/wagtail_personalisation/segment/dashboard.html', - 'modeladmin/index.html' + "modeladmin/wagtail_personalisation/segment/dashboard.html", + "modeladmin/index.html", ] 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) - )) + 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', + "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.' + "User has no permission to delete variant page objects." ) # Deleting page objects triggers deletion of the personalisation # metadata too because of models.CASCADE. @@ -63,8 +63,7 @@ def delete_instance(self): super().delete_instance() def post(self, request, *args, **kwargs): - if not can_delete_pages(self.get_affected_page_objects(), - self.request.user): + if not can_delete_pages(self.get_affected_page_objects(), self.request.user): context = self.get_context_data( cannot_delete_page_variants_error=True, ) @@ -75,28 +74,39 @@ def post(self, 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' + menu_icon = "fa-snowflake-o" add_to_settings_menu = False - list_display = ('name', 'persistent', 'match_any', 'status', - 'page_count', 'variant_count', 'statistics') - index_view_extra_js = ['js/commons.js', 'js/index.js'] - index_view_extra_css = ['css/index.css'] - form_view_extra_js = ['js/commons.js', 'js/form.js', - 'js/segment_form_control.js', - 'wagtailadmin/js/page-chooser-modal.js', - 'wagtailadmin/js/page-chooser.js'] - form_view_extra_css = ['css/form.css'] + list_display = ( + "name", + "persistent", + "match_any", + "status", + "page_count", + "variant_count", + "statistics", + ) + index_view_extra_js = ["js/commons.js", "js/index.js"] + index_view_extra_css = ["css/index.css"] + form_view_extra_js = [ + "js/commons.js", + "js/form.js", + "js/segment_form_control.js", + "wagtailadmin/js/page-chooser-modal.js", + "wagtailadmin/js/page-chooser.js", + ] + form_view_extra_css = ["css/form.css"] def index_view(self, request): - kwargs = {'model_admin': self} + kwargs = {"model_admin": self} view_class = self.dashboard_view_class - request.session.setdefault('segment_view', 'dashboard') - if request.session['segment_view'] != 'dashboard': + request.session.setdefault("segment_view", "dashboard") + if request.session["segment_view"] != "dashboard": view_class = self.index_view_class return view_class.as_view(**kwargs)(request) @@ -109,7 +119,8 @@ def variant_count(self, obj): def statistics(self, obj): return _("{visits} visits in {days} days").format( - visits=obj.visit_count, days=obj.get_active_days()) + visits=obj.visit_count, days=obj.get_active_days() + ) def toggle_segment_view(request): @@ -121,14 +132,14 @@ def toggle_segment_view(request): :rtype: django.http.HttpResponseRedirect """ - if request.user.has_perm('wagtailadmin.access_admin'): - if request.session['segment_view'] == 'dashboard': - request.session['segment_view'] = 'list' + if request.user.has_perm("wagtailadmin.access_admin"): + if request.session["segment_view"] == "dashboard": + request.session["segment_view"] = "list" - elif request.session['segment_view'] != 'dashboard': - request.session['segment_view'] = 'dashboard' + elif request.session["segment_view"] != "dashboard": + request.session["segment_view"] = "dashboard" - return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) + return HttpResponseRedirect(request.META.get("HTTP_REFERER", "/")) return HttpResponseForbidden() @@ -144,12 +155,12 @@ def toggle(request, segment_id): :rtype: django.http.HttpResponseRedirect """ - if request.user.has_perm('wagtailadmin.access_admin'): + if request.user.has_perm("wagtailadmin.access_admin"): segment = get_object_or_404(Segment, pk=segment_id) segment.toggle() - return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) + return HttpResponseRedirect(request.META.get("HTTP_REFERER", "/")) return HttpResponseForbidden() @@ -168,7 +179,7 @@ def copy_page_view(request, page_id, segment_id): :rtype: django.http.HttpResponseRedirect """ - if request.user.has_perm('wagtailadmin.access_admin'): + if request.user.has_perm("wagtailadmin.access_admin"): segment = get_object_or_404(Segment, pk=segment_id) page = get_object_or_404(Page, pk=page_id).specific @@ -178,7 +189,7 @@ def copy_page_view(request, page_id, segment_id): variant = variant_metadata.first() else: variant = metadata.copy_for_segment(segment) - edit_url = reverse('wagtailadmin_pages:edit', args=[variant.id]) + edit_url = reverse("wagtailadmin_pages:edit", args=[variant.id]) return HttpResponseRedirect(edit_url) @@ -187,14 +198,15 @@ def copy_page_view(request, page_id, segment_id): # CSV download views def segment_user_data(request, segment_id): - if request.user.has_perm('wagtailadmin.access_admin'): + if request.user.has_perm("wagtailadmin.access_admin"): segment = get_object_or_404(Segment, pk=segment_id) - response = HttpResponse(content_type='text/csv; charset=utf-8') - response['Content-Disposition'] = \ - 'attachment;filename=segment-%s-users.csv' % str(segment_id) + response = HttpResponse(content_type="text/csv; charset=utf-8") + response[ + "Content-Disposition" + ] = "attachment;filename=segment-%s-users.csv" % str(segment_id) - headers = ['Username'] + headers = ["Username"] for rule in segment.get_rules(): if rule.static: headers.append(rule.get_column_header()) diff --git a/src/wagtail_personalisation/wagtail_hooks.py b/src/wagtail_personalisation/wagtail_hooks.py index 3c440b2e..8a0553d0 100644 --- a/src/wagtail_personalisation/wagtail_hooks.py +++ b/src/wagtail_personalisation/wagtail_hooks.py @@ -1,26 +1,19 @@ import logging -from django.conf.urls import include, url from django.db import transaction from django.db.models import F from django.http import Http404 from django.shortcuts import redirect, render from django.template.defaultfilters import pluralize -from django.urls import reverse +from django.urls import include, re_path, reverse from django.utils.safestring import mark_safe -from django.utils.translation import ugettext_lazy as _ -from wagtail import VERSION as WAGTAIL_VERSION +from django.utils.translation import gettext_lazy as _ +from wagtail import hooks from wagtail.admin import messages from wagtail.admin.site_summary import PagesSummaryItem, SummaryItem - -try: - from wagtail.admin.views.pages.utils import get_valid_next_url_from_request -except ModuleNotFoundError: - from wagtail.admin.views.pages import get_valid_next_url_from_request # noqa - +from wagtail.admin.views.pages.utils 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.models import Page from wagtail_personalisation import admin_urls, models, utils from wagtail_personalisation.adapters import get_segment_adapter @@ -33,7 +26,7 @@ def register_admin_urls(): """Adds the administration urls for the personalisation apps.""" return [ - url( + re_path( r"^personalisation/", include(admin_urls, namespace="wagtail_personalisation"), ) @@ -46,7 +39,7 @@ def set_visit_count(page, request, serve_args, serve_kwargs): to a segment. :param page: The page being served - :type page: wagtail.core.models.Page + :type page: wagtail.models.Page :param request: The http request :type request: django.http.HttpRequest @@ -60,7 +53,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.core.models.Page + :type page: wagtail.models.Page :param request: The http request :type request: django.http.HttpRequest @@ -98,12 +91,12 @@ 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.core.models.Page + :type page: wagtail.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.core.models.Page + :rtype: wagtail.models.Page """ user_segments = [] @@ -133,7 +126,7 @@ def dont_show_variant(parent_page, pages, request): @hooks.register("register_page_listing_buttons") -def page_listing_variant_buttons(page, page_perms, is_parent=False, *args): +def page_listing_variant_buttons(page, page_perms, *args, **is_parent): """Adds page listing buttons to personalisable pages. Shows variants for the page (if any) and a 'Create a new variant' button. @@ -142,13 +135,13 @@ def page_listing_variant_buttons(page, page_perms, is_parent=False, *args): return metadata = page.personalisation_metadata + if metadata.is_canonical: yield ButtonWithDropdownFromHook( _("Variants"), hook_name="register_page_listing_variant_buttons", page=page, page_perms=page_perms, - is_parent=is_parent, attrs={"target": "_blank", "title": _("Create or edit a variant")}, priority=100, ) @@ -210,16 +203,10 @@ def get_total_pages(self, context): return page_count - if WAGTAIL_VERSION >= (2, 15): - def get_context_data(self, parent_context): - context = super().get_context_data(parent_context) - context["total_pages"] = self.get_total_pages(context) - return context - else: - def get_context(self): - context = super().get_context() - context["total_pages"] = self.get_total_pages(context) - return context + def get_context_data(self, parent_context): + context = super().get_context_data(parent_context) + context["total_pages"] = self.get_total_pages(context) + return context @hooks.register("construct_homepage_summary_items") @@ -238,13 +225,14 @@ class SegmentSummaryPanel(SummaryItem): order = 2000 - def render(self): + def render_html(self, parent_context): segment_count = models.Segment.objects.count() target_url = reverse("wagtail_personalisation_segment_modeladmin_index") title = _("Segments") return mark_safe( """ -
  • +
  • + {}{}
  • """.format( target_url, segment_count, title @@ -255,15 +243,16 @@ def render(self): class PersonalisedPagesSummaryPanel(PagesSummaryItem): order = 2100 - def render(self): + def render_html(self, parent_context): page_count = models.PersonalisablePageMetadata.objects.filter( segment__isnull=True ).count() title = _("Personalised Page") return mark_safe( """ -
  • - {}{}{} +
  • + + {}{}{}
  • """.format( page_count, title, pluralize(page_count) ) @@ -273,15 +262,17 @@ def render(self): class VariantPagesSummaryPanel(PagesSummaryItem): order = 2200 - def render(self): + def render_html(self, parent_context): page_count = models.PersonalisablePageMetadata.objects.filter( segment__isnull=False ).count() title = _("Variant") return mark_safe( """ -
  • - {}{}{} +
  • + + {}{}{}
  • """.format( page_count, title, pluralize(page_count) ) diff --git a/tests/conftest.py b/tests/conftest.py index e677aeb4..8d1e59af 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,6 @@ import pytest -pytest_plugins = [ - 'tests.fixtures' -] +pytest_plugins = ["tests.fixtures"] @pytest.fixture(autouse=True) @@ -10,9 +8,9 @@ def enable_db_access(db): pass -@pytest.fixture(scope='session') +@pytest.fixture(scope="session") def django_db_setup(django_db_setup, django_db_blocker): - from wagtail.core.models import Page, Site + from wagtail.models import Page, Site with django_db_blocker.unblock(): # Remove some initial data that is brought by the tests.site module diff --git a/tests/factories/page.py b/tests/factories/page.py index 177ad362..ad039467 100644 --- a/tests/factories/page.py +++ b/tests/factories/page.py @@ -2,25 +2,23 @@ import factory from django.utils.text import slugify +from wagtail.models import Locale from wagtail_factories.factories import PageFactory from tests.site.pages import models from wagtail_personalisation.models import PersonalisablePageMetadata -try: - from wagtail.core.models import Locale - class LocaleFactory(factory.DjangoModelFactory): - language_code = "en" +class LocaleFactory(factory.django.DjangoModelFactory): + language_code = "en" + + class Meta: + model = Locale - class Meta: - model = Locale -except ImportError: - pass class ContentPageFactory(PageFactory): parent = None - title = 'Test page' + title = "Test page" slug = factory.LazyAttribute(lambda obj: slugify(obj.title)) class Meta: @@ -28,14 +26,13 @@ class Meta: class RegularPageFactory(PageFactory): - title = 'Regular page' + title = "Regular page" slug = factory.LazyAttribute(lambda obj: slugify(obj.title)) class Meta: model = models.RegularPage -class PersonalisablePageMetadataFactory(factory.DjangoModelFactory): - +class PersonalisablePageMetadataFactory(factory.django.DjangoModelFactory): class Meta: model = PersonalisablePageMetadata diff --git a/tests/factories/rule.py b/tests/factories/rule.py index ef8665ce..0abb9cbb 100644 --- a/tests/factories/rule.py +++ b/tests/factories/rule.py @@ -7,32 +7,29 @@ from wagtail_personalisation import rules -class DayRuleFactory(factory.DjangoModelFactory): - +class DayRuleFactory(factory.django.DjangoModelFactory): class Meta: model = rules.DayRule -class DeviceRuleFactory(factory.DjangoModelFactory): - +class DeviceRuleFactory(factory.django.DjangoModelFactory): class Meta: model = rules.DeviceRule -class QueryRuleFactory(factory.DjangoModelFactory): - +class QueryRuleFactory(factory.django.DjangoModelFactory): class Meta: model = rules.QueryRule -class ReferralRuleFactory(factory.DjangoModelFactory): +class ReferralRuleFactory(factory.django.DjangoModelFactory): regex_string = "test.test" class Meta: model = rules.ReferralRule -class TimeRuleFactory(factory.DjangoModelFactory): +class TimeRuleFactory(factory.django.DjangoModelFactory): start_time = datetime.time(8, 0, 0) end_time = datetime.time(23, 0, 0) @@ -40,7 +37,7 @@ class Meta: model = rules.TimeRule -class VisitCountRuleFactory(factory.DjangoModelFactory): +class VisitCountRuleFactory(factory.django.DjangoModelFactory): operator = "more_than" count = 0 @@ -48,6 +45,6 @@ class Meta: model = rules.VisitCountRule -class OriginCountryRuleFactory(factory.DjangoModelFactory): +class OriginCountryRuleFactory(factory.django.DjangoModelFactory): class Meta: model = rules.OriginCountryRule diff --git a/tests/factories/segment.py b/tests/factories/segment.py index f92f579c..ae12575f 100644 --- a/tests/factories/segment.py +++ b/tests/factories/segment.py @@ -5,8 +5,8 @@ from wagtail_personalisation import models -class SegmentFactory(factory.DjangoModelFactory): - name = 'TestSegment' +class SegmentFactory(factory.django.DjangoModelFactory): + name = "TestSegment" status = models.Segment.STATUS_ENABLED class Meta: diff --git a/tests/factories/site.py b/tests/factories/site.py index 631963b6..3409ef64 100644 --- a/tests/factories/site.py +++ b/tests/factories/site.py @@ -1,13 +1,13 @@ import factory -from wagtail.core.models import Site +from wagtail.models import Site from tests.factories.page import ContentPageFactory -class SiteFactory(factory.DjangoModelFactory): - hostname = 'localhost' +class SiteFactory(factory.django.DjangoModelFactory): + hostname = "localhost" port = factory.Sequence(lambda n: 81 + n) - site_name = 'Test site' + site_name = "Test site" root_page = factory.SubFactory(ContentPageFactory, parent=None) is_default_site = False diff --git a/tests/fixtures.py b/tests/fixtures.py index 8775f0d9..b82403d5 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -9,23 +9,23 @@ from tests.factories.site import SiteFactory -@pytest.fixture(scope='function') +@pytest.fixture(scope="function") def site(): - root_page = ContentPageFactory(parent=None, slug='') + root_page = ContentPageFactory(parent=None, slug="") site = SiteFactory(is_default_site=True, root_page=root_page) - page1 = ContentPageFactory(parent=root_page, slug='page-1') - page2 = ContentPageFactory(parent=root_page, slug='page-2') - ContentPageFactory(parent=page1, slug='page-1-1') - ContentPageFactory(parent=page2, slug='page-2-1') + page1 = ContentPageFactory(parent=root_page, slug="page-1") + page2 = ContentPageFactory(parent=root_page, slug="page-2") + ContentPageFactory(parent=page1, slug="page-1-1") + ContentPageFactory(parent=page2, slug="page-2-1") - RegularPageFactory(parent=root_page, slug='regular') + RegularPageFactory(parent=root_page, slug="regular") return site @pytest.fixture() def segmented_page(site): - page = ContentPageFactory(parent=site.root_page, slug='personalised') + page = ContentPageFactory(parent=site.root_page, slug="personalised") segment = SegmentFactory() return page.personalisation_metadata.copy_for_segment(segment) @@ -37,7 +37,6 @@ def rf(): class RequestFactory(BaseRequestFactory): - def request(self, user=None, **request): request = super(RequestFactory, self).request(**request) request.user = AnonymousUser() @@ -48,4 +47,4 @@ def request(self, user=None, **request): @pytest.fixture() def user(django_user_model): - return django_user_model.objects.create(username='user') + return django_user_model.objects.create(username="user") diff --git a/tests/settings.py b/tests/settings.py index 51c3f1cb..f6d342b3 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -1,98 +1,86 @@ import os -from importlib.util import find_spec DATABASES = { - 'default': { - 'ENGINE': os.environ.get('DATABASE_ENGINE', 'django.db.backends.sqlite3'), - 'NAME': os.environ.get('DATABASE_NAME', 'db.sqlite3'), + "default": { + "ENGINE": os.environ.get("DATABASE_ENGINE", "django.db.backends.sqlite3"), + "NAME": os.environ.get("DATABASE_NAME", "db.sqlite3"), } } -ALLOWED_HOSTS = ['localhost'] +ALLOWED_HOSTS = ["localhost"] CACHES = { - 'default': { - 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', - 'LOCATION': 'unique-snowflake', + "default": { + "BACKEND": "django.core.cache.backends.locmem.LocMemCache", + "LOCATION": "unique-snowflake", } } -SECRET_KEY = 'not needed' -SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db' +SECRET_KEY = "not needed" +SESSION_ENGINE = "django.contrib.sessions.backends.cached_db" -ROOT_URLCONF = 'tests.site.urls' +ROOT_URLCONF = "tests.site.urls" -STATIC_URL = '/static/' +STATIC_URL = "/static/" -STATICFILES_FINDERS = ( - 'django.contrib.staticfiles.finders.AppDirectoriesFinder', -) +STATICFILES_FINDERS = ("django.contrib.staticfiles.finders.AppDirectoriesFinder",) USE_TZ = False TESTS_ROOT = os.path.dirname(os.path.abspath(__file__)) TEMPLATES = [ { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [ - os.path.join(TESTS_ROOT, 'site', 'templates'), + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [ + os.path.join(TESTS_ROOT, "site", "templates"), ], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', - 'django.template.context_processors.request', + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + "django.template.context_processors.request", ], - 'debug': True, + "debug": True, }, }, ] 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', + "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", ) -if find_spec('wagtail.contrib.legacy'): - MIDDLEWARE += ('wagtail.contrib.legacy.sitemiddleware.SiteMiddleware',) -else: - MIDDLEWARE += ('wagtail.core.middleware.SiteMiddleware', ) - INSTALLED_APPS = ( - 'wagtail_personalisation', - - 'wagtail.contrib.modeladmin', - 'wagtail.search', - 'wagtail.sites', - 'wagtail.users', - 'wagtail.images', - 'wagtail.documents', - 'wagtail.admin', - 'wagtail.core', - - 'taggit', - - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', - - 'tests.site.pages', + "wagtail_personalisation", + "wagtail.contrib.modeladmin", + "wagtail.search", + "wagtail.sites", + "wagtail.users", + "wagtail.images", + "wagtail.documents", + "wagtail.admin", + "wagtail", + "taggit", + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "tests.site.pages", ) PASSWORD_HASHERS = ( - 'django.contrib.auth.hashers.MD5PasswordHasher', # don't use the intentionally slow default password hasher + "django.contrib.auth.hashers.MD5PasswordHasher", # don't use the intentionally slow default password hasher ) -WAGTAIL_SITE_NAME = 'wagtail-personalisation test' +WAGTAIL_SITE_NAME = "wagtail-personalisation test" diff --git a/tests/site/pages/migrations/0001_initial.py b/tests/site/pages/migrations/0001_initial.py index bbd75426..a9d99261 100644 --- a/tests/site/pages/migrations/0001_initial.py +++ b/tests/site/pages/migrations/0001_initial.py @@ -3,31 +3,43 @@ from __future__ import unicode_literals import django.db.models.deletion -import wagtail.core.fields from django.db import migrations, models +from wagtail import fields import wagtail_personalisation.models class Migration(migrations.Migration): - initial = True dependencies = [ - ('wagtailcore', '0001_initial'), + ("wagtailcore", "0001_initial"), ] operations = [ migrations.CreateModel( - name='ContentPage', + name="ContentPage", 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.core.fields.RichTextField(blank=True, default='')), + ( + "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", fields.RichTextField(blank=True, default="")), ], options={ - 'abstract': False, + "abstract": False, }, - bases=(wagtail_personalisation.models.PersonalisablePageMixin, 'wagtailcore.page'), + bases=( + wagtail_personalisation.models.PersonalisablePageMixin, + "wagtailcore.page", + ), ), ] diff --git a/tests/site/pages/migrations/0002_regularpage.py b/tests/site/pages/migrations/0002_regularpage.py index df2697b9..1609cc60 100644 --- a/tests/site/pages/migrations/0002_regularpage.py +++ b/tests/site/pages/migrations/0002_regularpage.py @@ -2,28 +2,37 @@ from __future__ import unicode_literals import django.db.models.deletion -import wagtail.core.fields from django.db import migrations, models +from wagtail import fields class Migration(migrations.Migration): - dependencies = [ - ('wagtailcore', '0001_initial'), - ('pages', '0001_initial'), + ("wagtailcore", "0001_initial"), + ("pages", "0001_initial"), ] operations = [ migrations.CreateModel( - name='RegularPage', + name="RegularPage", 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.core.fields.RichTextField(blank=True, default='')), + ( + "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", fields.RichTextField(blank=True, default="")), ], options={ - 'abstract': False, + "abstract": False, }, - bases=('wagtailcore.page',), + bases=("wagtailcore.page",), ), ] diff --git a/tests/site/pages/models.py b/tests/site/pages/models.py index 6c40d92e..1c661abe 100644 --- a/tests/site/pages/models.py +++ b/tests/site/pages/models.py @@ -1,26 +1,26 @@ from django.db import models -from wagtail.admin.edit_handlers import FieldPanel -from wagtail.core.fields import RichTextField -from wagtail.core.models import Page +from wagtail.admin.panels import FieldPanel +from wagtail.fields import RichTextField +from wagtail.models import Page from wagtail_personalisation.models import PersonalisablePageMixin class RegularPage(Page): - subtitle = models.CharField(max_length=255, blank=True, default='') - body = RichTextField(blank=True, default='') + subtitle = models.CharField(max_length=255, blank=True, default="") + body = RichTextField(blank=True, default="") content_panels = Page.content_panels + [ - FieldPanel('subtitle'), - FieldPanel('body'), + FieldPanel("subtitle"), + FieldPanel("body"), ] class ContentPage(PersonalisablePageMixin, Page): - subtitle = models.CharField(max_length=255, blank=True, default='') - body = RichTextField(blank=True, default='') + subtitle = models.CharField(max_length=255, blank=True, default="") + body = RichTextField(blank=True, default="") content_panels = Page.content_panels + [ - FieldPanel('subtitle'), - FieldPanel('body'), + FieldPanel("subtitle"), + FieldPanel("body"), ] diff --git a/tests/site/urls.py b/tests/site/urls.py index b13e9cb9..afa697ba 100644 --- a/tests/site/urls.py +++ b/tests/site/urls.py @@ -1,16 +1,14 @@ from __future__ import absolute_import, unicode_literals -from django.conf.urls import include, url from django.contrib import admin +from django.urls import include, re_path +from wagtail import urls as wagtail_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/', admin.site.urls), - - url(r'^admin/', include(wagtailadmin_urls)), - url(r'^documents/', include(wagtaildocs_urls)), - - url(r'', include(wagtail_urls)), + re_path(r"^django-admin/", admin.site.urls), + re_path(r"^admin/", include(wagtailadmin_urls)), + re_path(r"^documents/", include(wagtaildocs_urls)), + re_path(r"", include(wagtail_urls)), ] diff --git a/tests/unit/test_adapter_session.py b/tests/unit/test_adapter_session.py index 191b20ae..6bf03ef8 100644 --- a/tests/unit/test_adapter_session.py +++ b/tests/unit/test_adapter_session.py @@ -6,15 +6,15 @@ @pytest.mark.django_db def test_get_segments(rf): - request = rf.get('/') + request = rf.get("/") adapter = adapters.SessionSegmentsAdapter(request) - segment_1 = SegmentFactory(name='segment-1', persistent=True) - segment_2 = SegmentFactory(name='segment-2', persistent=True) + 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 + assert len(request.session["segments"]) == 2 segments = adapter.get_segments() assert segments == [segment_1, segment_2] @@ -22,15 +22,15 @@ def test_get_segments(rf): @pytest.mark.django_db def test_get_segments_session(rf): - request = rf.get('/') + request = rf.get("/") adapter = adapters.SessionSegmentsAdapter(request) - segment_1 = SegmentFactory(name='segment-1', persistent=True) - segment_2 = SegmentFactory(name='segment-2', persistent=True) + 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 + assert len(request.session["segments"]) == 2 adapter._segment_cache = None segments = adapter.get_segments() @@ -39,12 +39,12 @@ def test_get_segments_session(rf): @pytest.mark.django_db def test_get_segment_by_id(rf): - request = rf.get('/') + request = rf.get("/") adapter = adapters.SessionSegmentsAdapter(request) - segment_1 = SegmentFactory(name='segment-1', persistent=True) - segment_2 = SegmentFactory(name='segment-2', persistent=True) + segment_1 = SegmentFactory(name="segment-1", persistent=True) + segment_2 = SegmentFactory(name="segment-2", persistent=True) adapter.set_segments([segment_1, segment_2]) @@ -54,12 +54,12 @@ def test_get_segment_by_id(rf): @pytest.mark.django_db def test_refresh_removes_disabled(rf): - request = rf.get('/') + request = rf.get("/") adapter = adapters.SessionSegmentsAdapter(request) - segment_1 = SegmentFactory(name='segment-1', persistent=True) - segment_2 = SegmentFactory(name='segment-2', persistent=True) + segment_1 = SegmentFactory(name="segment-1", persistent=True) + segment_2 = SegmentFactory(name="segment-2", persistent=True) adapter.set_segments([segment_1, segment_2]) @@ -73,27 +73,27 @@ def test_refresh_removes_disabled(rf): @pytest.mark.django_db def test_add_page_visit(rf, site): - request = rf.get('/') + request = rf.get("/") adapter = adapters.SessionSegmentsAdapter(request) adapter.add_page_visit(site.root_page) - assert request.session['visit_count'][0]['count'] == 1 + assert request.session["visit_count"][0]["count"] == 1 adapter.add_page_visit(site.root_page) - assert request.session['visit_count'][0]['count'] == 2 + assert request.session["visit_count"][0]["count"] == 2 assert adapter.get_visit_count() == 2 @pytest.mark.django_db def test_update_visit_count(rf, site): - request = rf.get('/') + request = rf.get("/") adapter = adapters.SessionSegmentsAdapter(request) - segment_1 = SegmentFactory(name='segment-1', persistent=True, visit_count=0) - segment_2 = SegmentFactory(name='segment-2', persistent=True, visit_count=0) + segment_1 = SegmentFactory(name="segment-1", persistent=True, visit_count=0) + segment_2 = SegmentFactory(name="segment-2", persistent=True, visit_count=0) adapter.set_segments([segment_1, segment_2]) adapter.update_visit_count() @@ -107,12 +107,12 @@ def test_update_visit_count(rf, site): @pytest.mark.django_db def test_update_visit_count_deleted_segment(rf, site): - request = rf.get('/') + request = rf.get("/") adapter = adapters.SessionSegmentsAdapter(request) - segment_1 = SegmentFactory(name='segment-1', persistent=True, visit_count=0) - segment_2 = SegmentFactory(name='segment-2', persistent=True, visit_count=0) + segment_1 = SegmentFactory(name="segment-1", persistent=True, visit_count=0) + segment_2 = SegmentFactory(name="segment-2", persistent=True, visit_count=0) adapter.set_segments([segment_1, segment_2]) segment_2.delete() diff --git a/tests/unit/test_factories.py b/tests/unit/test_factories.py index d82efba7..31e5a2ea 100644 --- a/tests/unit/test_factories.py +++ b/tests/unit/test_factories.py @@ -15,11 +15,12 @@ @pytest.mark.django_db def test_segment_create(): factoried_segment = SegmentFactory() - segment = Segment(name='TestSegment', status='enabled') + segment = Segment(name="TestSegment", status="enabled") TimeRule( start_time=datetime.time(8, 0, 0), end_time=datetime.time(23, 0, 0), - segment=segment) + segment=segment, + ) assert factoried_segment.name == segment.name assert factoried_segment.status == segment.status @@ -27,21 +28,16 @@ def test_segment_create(): @pytest.mark.django_db def test_referral_rule_create(): - segment = SegmentFactory(name='Referral') - referral_rule = ReferralRuleFactory( - regex_string='test.test', - segment=segment) + segment = SegmentFactory(name="Referral") + referral_rule = ReferralRuleFactory(regex_string="test.test", segment=segment) - assert referral_rule.regex_string == 'test.test' + assert referral_rule.regex_string == "test.test" @pytest.mark.django_db def test_query_rule_create(): - segment = SegmentFactory(name='Query') - query_rule = QueryRuleFactory( - parameter="query", - value="value", - segment=segment) - - assert query_rule.parameter == 'query' - assert query_rule.value == 'value' + segment = SegmentFactory(name="Query") + query_rule = QueryRuleFactory(parameter="query", value="value", segment=segment) + + assert query_rule.parameter == "query" + assert query_rule.value == "value" diff --git a/tests/unit/test_models.py b/tests/unit/test_models.py index e15571ae..ba7c28cc 100644 --- a/tests/unit/test_models.py +++ b/tests/unit/test_models.py @@ -16,7 +16,8 @@ def test_segment_create(): TimeRule( start_time=datetime.time(8, 0, 0), end_time=datetime.time(23, 0, 0), - segment=segment) + segment=segment, + ) @pytest.mark.django_db @@ -76,8 +77,5 @@ def test_sitemap_generation_for_variants_is_disabled(segmented_page): @pytest.mark.django_db def test_segment_edit_view(site, client, django_user_model): test_segment = SegmentFactory() - try: - new_panel = test_segment.panels[1].children[0].bind_to(model=Segment) - except AttributeError: - new_panel = test_segment.panels[1].children[0].bind_to_model(Segment) + new_panel = test_segment.panels[1].children[0].bind_to_model(Segment) assert new_panel.related.name == "wagtail_personalisation_timerules" diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index 8adffa35..f208bbb1 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -3,5 +3,5 @@ @pytest.mark.django_db def test_request_device_segment_no_match(client, site): - response = client.get('/regular/') + response = client.get("/regular/") assert response.status_code == 200 diff --git a/tests/unit/test_rules_country_origin.py b/tests/unit/test_rules_country_origin.py index 8ee82f2b..64b2486f 100644 --- a/tests/unit/test_rules_country_origin.py +++ b/tests/unit/test_rules_country_origin.py @@ -8,71 +8,72 @@ from wagtail_personalisation.rules import get_geoip_module skip_if_geoip2_installed = pytest.mark.skipif( - find_spec('geoip2'), reason='requires GeoIP2 to be not installed' + find_spec("geoip2"), reason="requires GeoIP2 to be not installed" ) skip_if_geoip2_not_installed = pytest.mark.skipif( - not find_spec('geoip2'), reason='requires GeoIP2 to be installed.' + not find_spec("geoip2"), reason="requires GeoIP2 to be installed." ) @pytest.mark.django_db def test_get_cloudflare_country_with_header(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='GB') - request = rf.get('/', HTTP_CF_IPCOUNTRY='PL') - assert rule.get_cloudflare_country(request) == 'pl' + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="GB") + request = rf.get("/", HTTP_CF_IPCOUNTRY="PL") + assert rule.get_cloudflare_country(request) == "pl" @pytest.mark.django_db def test_get_cloudflare_country_with_no_header(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='GB') - request = rf.get('/') - assert 'HTTP_CF_IPCOUNTRY' not in request.META + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="GB") + request = rf.get("/") + assert "HTTP_CF_IPCOUNTRY" not in request.META assert rule.get_cloudflare_country(request) is None @pytest.mark.django_db def test_get_cloudfront_country_with_header(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='GB') - request = rf.get('/', HTTP_CLOUDFRONT_VIEWER_COUNTRY='BY') - assert rule.get_cloudfront_country(request) == 'by' + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="GB") + request = rf.get("/", HTTP_CLOUDFRONT_VIEWER_COUNTRY="BY") + assert rule.get_cloudfront_country(request) == "by" @pytest.mark.django_db def test_get_cloudfront_country_with_no_header(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='GB') - request = rf.get('/') - assert 'HTTP_CLOUDFRONT_VIEWER_COUNTRY' not in request.META + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="GB") + request = rf.get("/") + assert "HTTP_CLOUDFRONT_VIEWER_COUNTRY" not in request.META assert rule.get_cloudfront_country(request) is None @pytest.mark.django_db def test_get_geoip_country_with_remote_addr(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='GB') - request = rf.get('/', REMOTE_ADDR='173.254.89.34') + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="GB") + request = rf.get("/", REMOTE_ADDR="173.254.89.34") geoip_mock = MagicMock() - with patch('wagtail_personalisation.rules.get_geoip_module', - return_value=geoip_mock) as geoip_import_mock: + with patch( + "wagtail_personalisation.rules.get_geoip_module", return_value=geoip_mock + ) as geoip_import_mock: rule.get_geoip_country(request) geoip_import_mock.assert_called_once() geoip_mock.assert_called_once() - assert geoip_mock.mock_calls[1] == call().country_code('173.254.89.34') + assert geoip_mock.mock_calls[1] == call().country_code("173.254.89.34") @pytest.mark.django_db def test_get_country_calls_all_methods(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='GB') - request = rf.get('/') + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="GB") + request = rf.get("/") - @patch.object(rule, 'get_geoip_country', return_value='') - @patch.object(rule, 'get_cloudflare_country', return_value='') - @patch.object(rule, 'get_cloudfront_country', return_value='') + @patch.object(rule, "get_geoip_country", return_value="") + @patch.object(rule, "get_cloudflare_country", return_value="") + @patch.object(rule, "get_cloudfront_country", return_value="") def test_mock(cloudfront_mock, cloudflare_mock, geoip_mock): country = rule.get_country(request) cloudflare_mock.assert_called_once_with(request) @@ -85,107 +86,106 @@ def test_mock(cloudfront_mock, cloudflare_mock, geoip_mock): @pytest.mark.django_db def test_get_country_does_not_use_all_detection_methods_unnecessarily(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='GB') - request = rf.get('/') + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="GB") + request = rf.get("/") - @patch.object(rule, 'get_geoip_country', return_value='') - @patch.object(rule, 'get_cloudflare_country', return_value='t1') - @patch.object(rule, 'get_cloudfront_country', return_value='') + @patch.object(rule, "get_geoip_country", return_value="") + @patch.object(rule, "get_cloudflare_country", return_value="t1") + @patch.object(rule, "get_cloudfront_country", return_value="") def test_mock(cloudfront_mock, cloudflare_mock, geoip_mock): country = rule.get_country(request) cloudflare_mock.assert_called_once_with(request) cloudfront_mock.assert_not_called() geoip_mock.assert_not_called() - assert country == 't1' + assert country == "t1" test_mock() @pytest.mark.django_db def test_test_user_calls_get_country(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='GB') - request = rf.get('/') - with patch.object(rule, 'get_country') as get_country_mock: + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="GB") + request = rf.get("/") + with patch.object(rule, "get_country") as get_country_mock: rule.test_user(request) get_country_mock.assert_called_once_with(request) @pytest.mark.django_db def test_test_user_returns_true_if_cloudflare_country_match(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='GB') - request = rf.get('/', HTTP_CF_IPCOUNTRY='GB') + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="GB") + request = rf.get("/", HTTP_CF_IPCOUNTRY="GB") assert rule.test_user(request) is True @pytest.mark.django_db def test_test_user_returns_false_if_cloudflare_country_doesnt_match(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='GB') - request = rf.get('/', HTTP_CF_IPCOUNTRY='NL') + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="GB") + request = rf.get("/", HTTP_CF_IPCOUNTRY="NL") assert not rule.test_user(request) @pytest.mark.django_db def test_test_user_returns_false_if_cloudfront_country_doesnt_match(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='GB') - request = rf.get('/', HTTP_CLOUDFRONT_VIEWER_COUNTRY='NL') + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="GB") + request = rf.get("/", HTTP_CLOUDFRONT_VIEWER_COUNTRY="NL") assert rule.test_user(request) is False @pytest.mark.django_db def test_test_user_returns_true_if_cloudfront_country_matches(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='se') - request = rf.get('/', HTTP_CLOUDFRONT_VIEWER_COUNTRY='SE') + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="se") + request = rf.get("/", HTTP_CLOUDFRONT_VIEWER_COUNTRY="SE") assert rule.test_user(request) is True @skip_if_geoip2_not_installed @pytest.mark.django_db def test_test_user_geoip_module_matches(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='se') - request = rf.get('/', REMOTE_ADDR='123.120.0.2') + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="se") + request = rf.get("/", REMOTE_ADDR="123.120.0.2") GeoIP2Mock = MagicMock() - GeoIP2Mock().configure_mock(**{'country_code.return_value': 'SE'}) + GeoIP2Mock().configure_mock(**{"country_code.return_value": "SE"}) GeoIP2Mock.reset_mock() - with patch('wagtail_personalisation.rules.get_geoip_module', - return_value=GeoIP2Mock): + with patch( + "wagtail_personalisation.rules.get_geoip_module", return_value=GeoIP2Mock + ): assert rule.test_user(request) is True assert GeoIP2Mock.mock_calls == [ call(), - call().country_code('123.120.0.2'), + call().country_code("123.120.0.2"), ] @skip_if_geoip2_not_installed @pytest.mark.django_db def test_test_user_geoip_module_does_not_match(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='nl') - request = rf.get('/', REMOTE_ADDR='123.120.0.2') + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="nl") + request = rf.get("/", REMOTE_ADDR="123.120.0.2") GeoIP2Mock = MagicMock() - GeoIP2Mock().configure_mock(**{'country_code.return_value': 'SE'}) + GeoIP2Mock().configure_mock(**{"country_code.return_value": "SE"}) GeoIP2Mock.reset_mock() - with patch('wagtail_personalisation.rules.get_geoip_module', - return_value=GeoIP2Mock): + with patch( + "wagtail_personalisation.rules.get_geoip_module", return_value=GeoIP2Mock + ): assert rule.test_user(request) is False - assert GeoIP2Mock.mock_calls == [ - call(), - call().country_code('123.120.0.2') - ] + assert GeoIP2Mock.mock_calls == [call(), call().country_code("123.120.0.2")] @skip_if_geoip2_installed @pytest.mark.django_db def test_test_user_does_not_use_geoip_module_if_disabled(rf): - segment = SegmentFactory(name='Test segment') - rule = OriginCountryRuleFactory(segment=segment, country='se') - request = rf.get('/', REMOTE_ADDR='123.120.0.2') + segment = SegmentFactory(name="Test segment") + rule = OriginCountryRuleFactory(segment=segment, country="se") + request = rf.get("/", REMOTE_ADDR="123.120.0.2") assert rule.test_user(request) is False @@ -199,4 +199,5 @@ def test_get_geoip_module_disabled(): @skip_if_geoip2_not_installed def test_get_geoip_module_enabled(): from django.contrib.gis.geoip2 import GeoIP2 + assert get_geoip_module() is GeoIP2 diff --git a/tests/unit/test_rules_day.py b/tests/unit/test_rules_day.py index f42c7158..10090f67 100644 --- a/tests/unit/test_rules_day.py +++ b/tests/unit/test_rules_day.py @@ -9,7 +9,7 @@ @pytest.mark.django_db def test_day_rule_create(): - segment = SegmentFactory(name='DaySegment') + segment = SegmentFactory(name="DaySegment") day_rule = DayRuleFactory(mon=True, thu=True, segment=segment) assert day_rule.mon is True @@ -20,11 +20,9 @@ def test_day_rule_create(): @pytest.mark.django_db @freeze_time("2017-01-01") def test_request_day_segment(client, site): - day_only_segment = SegmentFactory(name='Day only') - DayRuleFactory( - sun=True, - segment=day_only_segment) + day_only_segment = SegmentFactory(name="Day only") + DayRuleFactory(sun=True, segment=day_only_segment) - client.get('/') + client.get("/") - assert client.session['segments'][0]['encoded_name'] == 'day-only' + assert client.session["segments"][0]["encoded_name"] == "day-only" diff --git a/tests/unit/test_rules_device.py b/tests/unit/test_rules_device.py index f00524d3..cb9c1c7e 100644 --- a/tests/unit/test_rules_device.py +++ b/tests/unit/test_rules_device.py @@ -6,7 +6,7 @@ @pytest.mark.django_db def test_device_rule_create(): - segment = SegmentFactory(name='DeviceSegment') + segment = SegmentFactory(name="DeviceSegment") device_rule = DeviceRuleFactory(mobile=True, segment=segment) assert device_rule.mobile is True @@ -16,23 +16,25 @@ def test_device_rule_create(): @pytest.mark.django_db def test_request_device_segment(client, site): - device_only_segment = SegmentFactory(name='Device only') - DeviceRuleFactory( - tablet=True, - segment=device_only_segment) + device_only_segment = SegmentFactory(name="Device only") + DeviceRuleFactory(tablet=True, segment=device_only_segment) - client.get('/', **{'HTTP_USER_AGENT': 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X)'}) + client.get( + "/", + **{"HTTP_USER_AGENT": "Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X)"} + ) - assert client.session['segments'][0]['encoded_name'] == 'device-only' + assert client.session["segments"][0]["encoded_name"] == "device-only" @pytest.mark.django_db def test_request_device_segment_no_match(client, site): - no_device_segment = SegmentFactory(name='No device') - DeviceRuleFactory( - mobile=True, - segment=no_device_segment) + no_device_segment = SegmentFactory(name="No device") + DeviceRuleFactory(mobile=True, segment=no_device_segment) - client.get('/', **{'HTTP_USER_AGENT': 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X)'}) + client.get( + "/", + **{"HTTP_USER_AGENT": "Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X)"} + ) - assert not client.session['segments'] + assert not client.session["segments"] diff --git a/tests/unit/test_rules_mixed.py b/tests/unit/test_rules_mixed.py index 1a38b492..f538e34f 100644 --- a/tests/unit/test_rules_mixed.py +++ b/tests/unit/test_rules_mixed.py @@ -11,80 +11,68 @@ @pytest.mark.django_db def test_no_segments(client, site): - response = client.get('/') + response = client.get("/") assert response.status_code == 200 - assert client.session['segments'] == [] + assert client.session["segments"] == [] @pytest.mark.django_db def test_referral_segment(client, site): - referral_segment = SegmentFactory(name='Referral') - ReferralRuleFactory( - regex_string="test.test", - segment=referral_segment - ) + referral_segment = SegmentFactory(name="Referral") + ReferralRuleFactory(regex_string="test.test", segment=referral_segment) - response = client.get('/', **{'HTTP_REFERER': 'test.test'}) + response = client.get("/", **{"HTTP_REFERER": "test.test"}) assert response.status_code == 200 - assert client.session['segments'][0]['encoded_name'] == 'referral' + assert client.session["segments"][0]["encoded_name"] == "referral" @pytest.mark.django_db @freeze_time("10:00:00") def test_time_and_referral_segment(client, site): - segment = SegmentFactory(name='Both') + segment = SegmentFactory(name="Both") TimeRuleFactory( start_time=datetime.time(8, 0, 0), end_time=datetime.time(23, 0, 0), - segment=segment - ) - ReferralRuleFactory( - regex_string="test.test", - segment=segment + segment=segment, ) + ReferralRuleFactory(regex_string="test.test", segment=segment) - response = client.get('/', **{'HTTP_REFERER': 'test.test'}) + response = client.get("/", **{"HTTP_REFERER": "test.test"}) assert response.status_code == 200 - assert client.session['segments'][0]['encoded_name'] == 'both' + assert client.session["segments"][0]["encoded_name"] == "both" @pytest.mark.django_db @freeze_time("7:00:00") def test_no_time_but_referral_segment(client, site): - segment = SegmentFactory(name='Not both') + segment = SegmentFactory(name="Not both") TimeRuleFactory( start_time=datetime.time(8, 0, 0), end_time=datetime.time(23, 0, 0), - segment=segment - ) - ReferralRuleFactory( - regex_string="test.test", - segment=segment + segment=segment, ) + ReferralRuleFactory(regex_string="test.test", segment=segment) - response = client.get('/', **{'HTTP_REFERER': 'test.test'}) + response = client.get("/", **{"HTTP_REFERER": "test.test"}) assert response.status_code == 200 - assert len(client.session['segments']) == 0 + assert len(client.session["segments"]) == 0 @pytest.mark.django_db @freeze_time("9:00:00") def test_time_but_no_referral_segment(client, site): - segment = SegmentFactory(name='Not both') + segment = SegmentFactory(name="Not both") TimeRuleFactory( start_time=datetime.time(8, 0, 0), end_time=datetime.time(23, 0, 0), - segment=segment - ) - ReferralRuleFactory( - regex_string="test.test", - segment=segment + segment=segment, ) + ReferralRuleFactory(regex_string="test.test", segment=segment) - response = client.get('/') + response = client.get("/") assert response.status_code == 200 - assert len(client.session['segments']) == 0 + assert len(client.session["segments"]) == 0 diff --git a/tests/unit/test_rules_query.py b/tests/unit/test_rules_query.py index 22a9be50..99ede520 100644 --- a/tests/unit/test_rules_query.py +++ b/tests/unit/test_rules_query.py @@ -6,43 +6,33 @@ @pytest.mark.django_db def test_request_query_rule(client, site): - segment = SegmentFactory(name='Query') + segment = SegmentFactory(name="Query") QueryRuleFactory( parameter="query", value="value", segment=segment, ) - response = client.get('/?query=value') + response = client.get("/?query=value") assert response.status_code == 200 - assert any( - item['encoded_name'] == 'query' for item in client.session['segments']) + assert any(item["encoded_name"] == "query" for item in client.session["segments"]) @pytest.mark.django_db def test_request_only_one_query_rule(client, site): - segment = SegmentFactory(name='Query') - QueryRuleFactory( - parameter="query", - value="value", - segment=segment - ) + segment = SegmentFactory(name="Query") + QueryRuleFactory(parameter="query", value="value", segment=segment) - response = client.get('/?test=test&query=value') + response = client.get("/?test=test&query=value") assert response.status_code == 200 - assert any( - item['encoded_name'] == 'query' for item in client.session['segments']) + assert any(item["encoded_name"] == "query" for item in client.session["segments"]) @pytest.mark.django_db def test_request_multiple_queries(client, site): - segment = SegmentFactory(name='Multiple queries') - QueryRuleFactory( - parameter="test", - value="test", - segment=segment - ) + segment = SegmentFactory(name="Multiple queries") + QueryRuleFactory(parameter="test", value="test", segment=segment) QueryRuleFactory( parameter="query", @@ -50,77 +40,65 @@ def test_request_multiple_queries(client, site): segment=segment, ) - response = client.get('/?test=test&query=value') + response = client.get("/?test=test&query=value") assert response.status_code == 200 assert any( - item['encoded_name'] == 'multiple-queries' - for item in client.session['segments'] + item["encoded_name"] == "multiple-queries" + for item in client.session["segments"] ) @pytest.mark.django_db def test_request_persistent_segmenting(client, site): - segment = SegmentFactory(name='Persistent', persistent=True) - QueryRuleFactory( - parameter="test", - value="test", - segment=segment - ) + segment = SegmentFactory(name="Persistent", persistent=True) + QueryRuleFactory(parameter="test", value="test", segment=segment) - response = client.get('/?test=test') + response = client.get("/?test=test") assert response.status_code == 200 assert any( - item['encoded_name'] == 'persistent' - for item in client.session['segments']) + item["encoded_name"] == "persistent" for item in client.session["segments"] + ) - response = client.get('/') + response = client.get("/") assert response.status_code == 200 assert any( - item['encoded_name'] == 'persistent' - for item in client.session['segments']) + item["encoded_name"] == "persistent" for item in client.session["segments"] + ) @pytest.mark.django_db def test_request_non_persistent_segmenting(client, site): - segment = SegmentFactory(name='Non Persistent') - QueryRuleFactory( - parameter="test", - value="test", - segment=segment - ) + segment = SegmentFactory(name="Non Persistent") + QueryRuleFactory(parameter="test", value="test", segment=segment) - response = client.get('/?test=test') + response = client.get("/?test=test") assert response.status_code == 200 assert any( - item['encoded_name'] == 'non-persistent' - for item in client.session['segments']) + item["encoded_name"] == "non-persistent" for item in client.session["segments"] + ) - response = client.get('/') + response = client.get("/") assert response.status_code == 200 assert not any( - item['encoded_name'] == 'non-persistent' - for item in client.session['segments']) + item["encoded_name"] == "non-persistent" for item in client.session["segments"] + ) @pytest.mark.django_db def test_request_match_any_segmenting(client, site): - segment = SegmentFactory(name='Match any', match_any=True) + segment = SegmentFactory(name="Match any", match_any=True) QueryRuleFactory( - parameter='test', - value='test', + parameter="test", + value="test", segment=segment, ) - QueryRuleFactory( - parameter='test2', - value='test2', - segment=segment - ) + QueryRuleFactory(parameter="test2", value="test2", segment=segment) - response = client.get('/?test=test') + response = client.get("/?test=test") assert response.status_code == 200 assert any( - item['encoded_name'] == 'match-any' - for item in client.session['segments']) + item["encoded_name"] == "match-any" for item in client.session["segments"] + ) diff --git a/tests/unit/test_rules_referral.py b/tests/unit/test_rules_referral.py index b1378e21..fb6faf42 100644 --- a/tests/unit/test_rules_referral.py +++ b/tests/unit/test_rules_referral.py @@ -1,4 +1,3 @@ - import pytest from tests.factories.rule import ReferralRuleFactory @@ -7,9 +6,7 @@ @pytest.mark.django_db def test_referral_rule_create(): - segment = SegmentFactory(name='Referral') - referral_rule = ReferralRuleFactory( - regex_string='test.test', - segment=segment) + segment = SegmentFactory(name="Referral") + referral_rule = ReferralRuleFactory(regex_string="test.test", segment=segment) - assert referral_rule.regex_string == 'test.test' + assert referral_rule.regex_string == "test.test" diff --git a/tests/unit/test_rules_time.py b/tests/unit/test_rules_time.py index f1fa0053..09ac35f4 100644 --- a/tests/unit/test_rules_time.py +++ b/tests/unit/test_rules_time.py @@ -9,11 +9,12 @@ @pytest.mark.django_db def test_time_rule_create(): - segment = SegmentFactory(name='TimeSegment') + segment = SegmentFactory(name="TimeSegment") time_rule = TimeRuleFactory( start_time=datetime.time(8, 0, 0), end_time=datetime.time(23, 0, 0), - segment=segment) + segment=segment, + ) assert time_rule.start_time == datetime.time(8, 0, 0) @@ -21,13 +22,14 @@ def test_time_rule_create(): @pytest.mark.django_db @freeze_time("10:00:00") def test_requesttime_segment(client, site): - time_only_segment = SegmentFactory(name='Time only') + time_only_segment = SegmentFactory(name="Time only") TimeRuleFactory( start_time=datetime.time(8, 0, 0), end_time=datetime.time(23, 0, 0), - segment=time_only_segment) + segment=time_only_segment, + ) - response = client.get('/') + response = client.get("/") assert response.status_code == 200 - assert client.session['segments'][0]['encoded_name'] == 'time-only' + assert client.session["segments"][0]["encoded_name"] == "time-only" diff --git a/tests/unit/test_rules_visitcount.py b/tests/unit/test_rules_visitcount.py index 321f0439..ece9709c 100644 --- a/tests/unit/test_rules_visitcount.py +++ b/tests/unit/test_rules_visitcount.py @@ -7,23 +7,23 @@ @pytest.mark.django_db def test_visit_count(site, client): - response = client.get('/') + response = client.get("/") assert response.status_code == 200 - visit_count = client.session['visit_count'] - assert visit_count[0]['path'] == '/' - assert visit_count[0]['count'] == 1 + visit_count = client.session["visit_count"] + assert visit_count[0]["path"] == "/" + assert visit_count[0]["count"] == 1 - response = client.get('/') + response = client.get("/") assert response.status_code == 200 - visit_count = client.session['visit_count'] - assert visit_count[0]['path'] == '/' - assert visit_count[0]['count'] == 2 + visit_count = client.session["visit_count"] + assert visit_count[0]["path"] == "/" + assert visit_count[0]["count"] == 2 - response = client.get('/page-1/') + response = client.get("/page-1/") assert response.status_code == 200 - visit_count = client.session['visit_count'] - assert visit_count[0]['count'] == 2 - assert visit_count[1]['count'] == 1 + visit_count = client.session["visit_count"] + assert visit_count[0]["count"] == 2 + assert visit_count[1]["count"] == 1 @pytest.mark.django_db @@ -34,11 +34,11 @@ def test_call_test_user_on_invalid_rule_fails(site, user, mocker): @pytest.mark.django_db def test_visit_count_call_test_user_with_user(site, client, user): - segment = SegmentFactory(name='VisitCount') + segment = SegmentFactory(name="VisitCount") rule = VisitCountRuleFactory(counted_page=site.root_page, segment=segment) session = client.session - session['visit_count'] = [{'path': '/', 'count': 2}] + session["visit_count"] = [{"path": "/", "count": 2}] session.save() client.force_login(user) @@ -47,11 +47,11 @@ def test_visit_count_call_test_user_with_user(site, client, user): @pytest.mark.django_db def test_visit_count_call_test_user_with_user_or_request_fails(site, client, user): - segment = SegmentFactory(name='VisitCount') + segment = SegmentFactory(name="VisitCount") rule = VisitCountRuleFactory(counted_page=site.root_page, segment=segment) session = client.session - session['visit_count'] = [{'path': '/', 'count': 2}] + session["visit_count"] = [{"path": "/", "count": 2}] session.save() client.force_login(user) @@ -60,20 +60,20 @@ def test_visit_count_call_test_user_with_user_or_request_fails(site, client, use @pytest.mark.django_db def test_get_column_header(site): - segment = SegmentFactory(name='VisitCount') + segment = SegmentFactory(name="VisitCount") rule = VisitCountRuleFactory(counted_page=site.root_page, segment=segment) - assert rule.get_column_header() == 'Visit count - Test page' + assert rule.get_column_header() == "Visit count - Test page" @pytest.mark.django_db def test_get_user_info_string_returns_count(site, client, user): - segment = SegmentFactory(name='VisitCount') + segment = SegmentFactory(name="VisitCount") rule = VisitCountRuleFactory(counted_page=site.root_page, segment=segment) session = client.session - session['visit_count'] = [{'path': '/', 'count': 2}] + session["visit_count"] = [{"path": "/", "count": 2}] session.save() client.force_login(user) - assert rule.get_user_info_string(user) == '2' + assert rule.get_user_info_string(user) == "2" diff --git a/tests/unit/test_static_dynamic_segments.py b/tests/unit/test_static_dynamic_segments.py index b69cd4a9..94f5a99e 100644 --- a/tests/unit/test_static_dynamic_segments.py +++ b/tests/unit/test_static_dynamic_segments.py @@ -12,12 +12,30 @@ def form_with_data(segment, *rules): - model_fields = ['type', 'status', 'count', 'name', 'match_any', 'randomisation_percent'] + model_fields = [ + "type", + "status", + "count", + "name", + "match_any", + "randomisation_percent", + ] + model_formsets = [ + "wagtail_personalisation_timerules", + "wagtail_personalisation_dayrules", + "wagtail_personalisation_referralrules", + "wagtail_personalisation_visitcountrules", + "wagtail_personalisation_queryrules", + "wagtail_personalisation_devicerules", + "wagtail_personalisation_userisloggedinrules", + "wagtail_personalisation_origincountryrules", + ] class TestSegmentAdminForm(SegmentAdminForm): class Meta: model = Segment fields = model_fields + formsets = model_formsets data = model_to_dict(segment, model_fields) for formset in TestSegmentAdminForm().formsets.values(): @@ -27,10 +45,10 @@ class Meta: if isinstance(rule, formset.model): rule_data = model_to_dict(rule) for key, value in rule_data.items(): - data['{}-{}-{}'.format(formset.prefix, count, key)] = value + data["{}-{}-{}".format(formset.prefix, count, key)] = value count += 1 - data['{}-INITIAL_FORMS'.format(formset.prefix)] = 0 - data['{}-TOTAL_FORMS'.format(formset.prefix)] = count + data["{}-INITIAL_FORMS".format(formset.prefix)] = 0 + data["{}-TOTAL_FORMS".format(formset.prefix)] = count return TestSegmentAdminForm(data) @@ -39,21 +57,27 @@ def test_user_added_to_static_segment_at_creation(site, user, mocker): segment = SegmentFactory.build(type=Segment.TYPE_STATIC) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) - mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user', return_value=True) + mocker.patch( + "wagtail_personalisation.rules.VisitCountRule.test_user", return_value=True + ) instance = form.save() assert user in instance.static_users.all() @pytest.mark.django_db -def test_user_not_added_to_full_static_segment_at_creation(site, django_user_model, mocker): - django_user_model.objects.create(username='first') - django_user_model.objects.create(username='second') +def test_user_not_added_to_full_static_segment_at_creation( + site, django_user_model, mocker +): + django_user_model.objects.create(username="first") + django_user_model.objects.create(username="second") segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=1) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) - mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user', - side_effect=[True, True]) + mocker.patch( + "wagtail_personalisation.rules.VisitCountRule.test_user", + side_effect=[True, True], + ) instance = form.save() assert len(instance.static_users.all()) == 1 @@ -68,7 +92,9 @@ def test_anonymous_user_not_added_to_static_segment_at_creation(site, client, mo segment = SegmentFactory.build(type=Segment.TYPE_STATIC) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) - mock_test_rule = mocker.patch('wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules') + mock_test_rule = mocker.patch( + "wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules" + ) instance = form.save() assert not instance.static_users.all() @@ -77,15 +103,18 @@ def test_anonymous_user_not_added_to_static_segment_at_creation(site, client, mo @pytest.mark.django_db def test_match_any_correct_populates(site, django_user_model, mocker): - user = django_user_model.objects.create(username='first') - other_user = django_user_model.objects.create(username='second') + user = django_user_model.objects.create(username="first") + other_user = django_user_model.objects.create(username="second") other_page = site.root_page.get_last_child() segment = SegmentFactory.build(type=Segment.TYPE_STATIC, match_any=True) rule_1 = VisitCountRule(counted_page=site.root_page) rule_2 = VisitCountRule(counted_page=other_page) form = form_with_data(segment, rule_1, rule_2) - mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user', side_effect=[True, False, True, False]) + mocker.patch( + "wagtail_personalisation.rules.VisitCountRule.test_user", + side_effect=[True, False, True, False], + ) instance = form.save() assert user in instance.static_users.all() @@ -102,7 +131,9 @@ def test_mixed_static_dynamic_session_doesnt_generate_at_creation(site, mocker): ) form = form_with_data(segment, static_rule, non_static_rule) - mock_test_rule = mocker.patch('wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules') + mock_test_rule = mocker.patch( + "wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules" + ) instance = form.save() assert not instance.static_users.all() @@ -154,9 +185,11 @@ def test_anonymou_user_not_added_to_static_segment_after_creation(site, client): @pytest.mark.django_db -def test_session_not_added_to_static_segment_after_full(site, client, django_user_model): - user = django_user_model.objects.create(username='first') - other_user = django_user_model.objects.create(username='second') +def test_session_not_added_to_static_segment_after_full( + site, client, django_user_model +): + user = django_user_model.objects.create(username="first") + other_user = django_user_model.objects.create(username="second") segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=1) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) @@ -190,7 +223,9 @@ def test_sessions_not_added_to_static_segment_if_rule_not_static(mocker): segment=segment, ) form = form_with_data(segment, rule) - mock_test_rule = mocker.patch('wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules') + mock_test_rule = mocker.patch( + "wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules" + ) instance = form.save() assert not instance.static_users.all() @@ -202,12 +237,16 @@ def test_does_not_calculate_the_segment_again(site, client, mocker, user): segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=2) rule = VisitCountRule(counted_page=site.root_page, segment=segment) form = form_with_data(segment, rule) - mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user', return_value=True) + mocker.patch( + "wagtail_personalisation.rules.VisitCountRule.test_user", return_value=True + ) instance = form.save() assert user in instance.static_users.all() - mock_test_rule = mocker.patch('wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules') + mock_test_rule = mocker.patch( + "wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules" + ) session = client.session session.save() client.force_login(user) @@ -247,7 +286,9 @@ def test_dynamic_segment_with_non_static_rules_have_a_count(): @pytest.mark.django_db -def test_randomisation_percentage_added_to_segment_at_creation(site, client, mocker, django_user_model): +def test_randomisation_percentage_added_to_segment_at_creation( + site, client, mocker, django_user_model +): segment = SegmentFactory.build(type=Segment.TYPE_STATIC) segment.randomisation_percent = 80 rule = VisitCountRule() @@ -280,80 +321,85 @@ def test_randomisation_percentage_max_100(site, client, mocker, django_user_mode @pytest.mark.django_db def test_in_static_segment_if_random_is_below_percentage(site, client, mocker, user): - segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=1, - randomisation_percent=40) + segment = SegmentFactory.build( + type=Segment.TYPE_STATIC, count=1, randomisation_percent=40 + ) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) instance = form.save() - mocker.patch('random.randint', return_value=39) + mocker.patch("random.randint", return_value=39) session = client.session session.save() client.force_login(user) client.get(site.root_page.url) - assert instance.id == client.session['segments'][0]['id'] + assert instance.id == client.session["segments"][0]["id"] assert user in instance.static_users.all() assert user not in instance.excluded_users.all() @pytest.mark.django_db -def test_not_in_static_segment_if_random_is_above_percentage(site, client, mocker, user): - segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=1, - randomisation_percent=40) +def test_not_in_static_segment_if_random_is_above_percentage( + site, client, mocker, user +): + segment = SegmentFactory.build( + type=Segment.TYPE_STATIC, count=1, randomisation_percent=40 + ) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) instance = form.save() - mocker.patch('random.randint', return_value=41) + mocker.patch("random.randint", return_value=41) session = client.session session.save() client.force_login(user) client.get(site.root_page.url) - assert len(client.session['segments']) == 0 + assert len(client.session["segments"]) == 0 assert user not in instance.static_users.all() assert user in instance.excluded_users.all() @pytest.mark.django_db def test_offered_dynamic_segment_if_random_is_below_percentage(site, client, mocker): - segment = SegmentFactory.build(type=Segment.TYPE_DYNAMIC, - randomisation_percent=40) + segment = SegmentFactory.build(type=Segment.TYPE_DYNAMIC, randomisation_percent=40) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) instance = form.save() - mocker.patch('random.randint', return_value=39) + mocker.patch("random.randint", return_value=39) session = client.session session.save() client.get(site.root_page.url) - assert len(client.session['excluded_segments']) == 0 - assert instance.id == client.session['segments'][0]['id'] + assert len(client.session["excluded_segments"]) == 0 + assert instance.id == client.session["segments"][0]["id"] @pytest.mark.django_db -def test_not_offered_dynamic_segment_if_random_is_above_percentage(site, client, mocker): - segment = SegmentFactory.build(type=Segment.TYPE_DYNAMIC, - randomisation_percent=40) +def test_not_offered_dynamic_segment_if_random_is_above_percentage( + site, client, mocker +): + segment = SegmentFactory.build(type=Segment.TYPE_DYNAMIC, randomisation_percent=40) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) instance = form.save() - mocker.patch('random.randint', return_value=41) + mocker.patch("random.randint", return_value=41) session = client.session session.save() client.get(site.root_page.url) - assert len(client.session['segments']) == 0 - assert instance.id == client.session['excluded_segments'][0]['id'] + assert len(client.session["segments"]) == 0 + assert instance.id == client.session["excluded_segments"][0]["id"] @pytest.mark.django_db def test_not_in_segment_if_percentage_is_0(site, client, mocker, user): - segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=1, - randomisation_percent=0) + segment = SegmentFactory.build( + type=Segment.TYPE_STATIC, count=1, randomisation_percent=0 + ) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) instance = form.save() @@ -363,15 +409,16 @@ def test_not_in_segment_if_percentage_is_0(site, client, mocker, user): client.force_login(user) client.get(site.root_page.url) - assert len(client.session['segments']) == 0 + assert len(client.session["segments"]) == 0 assert user not in instance.static_users.all() assert user in instance.excluded_users.all() @pytest.mark.django_db def test_always_in_segment_if_percentage_is_100(site, client, mocker, user): - segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=1, - randomisation_percent=100) + segment = SegmentFactory.build( + type=Segment.TYPE_STATIC, count=1, randomisation_percent=100 + ) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) instance = form.save() @@ -381,18 +428,22 @@ def test_always_in_segment_if_percentage_is_100(site, client, mocker, user): client.force_login(user) client.get(site.root_page.url) - assert instance.id == client.session['segments'][0]['id'] + assert instance.id == client.session["segments"][0]["id"] assert user in instance.static_users.all() assert user not in instance.excluded_users.all() @pytest.mark.django_db -def test_not_added_to_static_segment_at_creation_if_random_above_percent(site, mocker, user): - mocker.patch('random.randint', return_value=41) +def test_not_added_to_static_segment_at_creation_if_random_above_percent( + site, mocker, user +): + mocker.patch("random.randint", return_value=41) segment = SegmentFactory.build(type=Segment.TYPE_STATIC, randomisation_percent=40) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) - mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user', return_value=True) + mocker.patch( + "wagtail_personalisation.rules.VisitCountRule.test_user", return_value=True + ) instance = form.save() assert user not in instance.static_users.all() @@ -400,12 +451,16 @@ def test_not_added_to_static_segment_at_creation_if_random_above_percent(site, m @pytest.mark.django_db -def test_added_to_static_segment_at_creation_if_random_below_percent(site, mocker, user): - mocker.patch('random.randint', return_value=39) +def test_added_to_static_segment_at_creation_if_random_below_percent( + site, mocker, user +): + mocker.patch("random.randint", return_value=39) segment = SegmentFactory.build(type=Segment.TYPE_STATIC, randomisation_percent=40) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) - mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user', return_value=True) + mocker.patch( + "wagtail_personalisation.rules.VisitCountRule.test_user", return_value=True + ) instance = form.save() assert user in instance.static_users.all() @@ -414,8 +469,9 @@ def test_added_to_static_segment_at_creation_if_random_below_percent(site, mocke @pytest.mark.django_db def test_rules_check_skipped_if_user_in_excluded(site, client, mocker, user): - segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=1, - randomisation_percent=100) + segment = SegmentFactory.build( + type=Segment.TYPE_STATIC, count=1, randomisation_percent=100 + ) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) instance = form.save() @@ -423,7 +479,8 @@ def test_rules_check_skipped_if_user_in_excluded(site, client, mocker, user): instance.save mock_test_rule = mocker.patch( - 'wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules') + "wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules" + ) session = client.session session.save() @@ -431,15 +488,14 @@ def test_rules_check_skipped_if_user_in_excluded(site, client, mocker, user): client.get(site.root_page.url) assert mock_test_rule.call_count == 0 - assert len(client.session['segments']) == 0 + assert len(client.session["segments"]) == 0 assert user not in instance.static_users.all() assert user in instance.excluded_users.all() @pytest.mark.django_db def test_rules_check_skipped_if_dynamic_segment_in_excluded(site, client, mocker, user): - segment = SegmentFactory.build(type=Segment.TYPE_DYNAMIC, - randomisation_percent=100) + segment = SegmentFactory.build(type=Segment.TYPE_DYNAMIC, randomisation_percent=100) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) instance = form.save() @@ -447,30 +503,35 @@ def test_rules_check_skipped_if_dynamic_segment_in_excluded(site, client, mocker instance.save() session = client.session - session['excluded_segments'] = [{'id': instance.pk}] + session["excluded_segments"] = [{"id": instance.pk}] session.save() mock_test_rule = mocker.patch( - 'wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules') + "wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules" + ) client.force_login(user) client.get(site.root_page.url) assert mock_test_rule.call_count == 0 - assert len(client.session['segments']) == 0 + assert len(client.session["segments"]) == 0 @pytest.mark.django_db -def test_matched_user_count_added_to_segment_at_creation(site, mocker, django_user_model): - django_user_model.objects.create(username='first') - django_user_model.objects.create(username='second') +def test_matched_user_count_added_to_segment_at_creation( + site, mocker, django_user_model +): + django_user_model.objects.create(username="first") + django_user_model.objects.create(username="second") segment = SegmentFactory.build(type=Segment.TYPE_STATIC) rule = VisitCountRule() form = form_with_data(segment, rule) form.instance.type = Segment.TYPE_STATIC - mock_test_user = mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user', return_value=True) + mock_test_user = mocker.patch( + "wagtail_personalisation.rules.VisitCountRule.test_user", return_value=True + ) instance = form.save() assert mock_test_user.call_count == 2 @@ -479,49 +540,59 @@ def test_matched_user_count_added_to_segment_at_creation(site, mocker, django_us @pytest.mark.django_db def test_count_users_matching_static_rules(site, client, mocker, django_user_model): - django_user_model.objects.create(username='first') - django_user_model.objects.create(username='second') + django_user_model.objects.create(username="first") + django_user_model.objects.create(username="second") segment = SegmentFactory.build(type=Segment.TYPE_STATIC) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) - mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user', return_value=True) + mocker.patch( + "wagtail_personalisation.rules.VisitCountRule.test_user", return_value=True + ) assert form.count_matching_users([rule], True) == 2 @pytest.mark.django_db def test_count_matching_users_excludes_staff(site, client, mocker, django_user_model): - django_user_model.objects.create(username='first') - django_user_model.objects.create(username='second', is_staff=True) + django_user_model.objects.create(username="first") + django_user_model.objects.create(username="second", is_staff=True) segment = SegmentFactory.build(type=Segment.TYPE_STATIC) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) - mock_test_user = mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user', return_value=True) + mock_test_user = mocker.patch( + "wagtail_personalisation.rules.VisitCountRule.test_user", return_value=True + ) assert form.count_matching_users([rule], True) == 1 assert mock_test_user.call_count == 1 @pytest.mark.django_db -def test_count_matching_users_excludes_inactive(site, client, mocker, django_user_model): - django_user_model.objects.create(username='first') - django_user_model.objects.create(username='second', is_active=False) +def test_count_matching_users_excludes_inactive( + site, client, mocker, django_user_model +): + django_user_model.objects.create(username="first") + django_user_model.objects.create(username="second", is_active=False) segment = SegmentFactory.build(type=Segment.TYPE_STATIC) rule = VisitCountRule(counted_page=site.root_page) form = form_with_data(segment, rule) - mock_test_user = mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user', return_value=True) + mock_test_user = mocker.patch( + "wagtail_personalisation.rules.VisitCountRule.test_user", return_value=True + ) assert form.count_matching_users([rule], True) == 1 assert mock_test_user.call_count == 1 @pytest.mark.django_db -def test_count_matching_users_only_counts_static_rules(site, client, mocker, django_user_model): - django_user_model.objects.create(username='first') - django_user_model.objects.create(username='second') +def test_count_matching_users_only_counts_static_rules( + site, client, mocker, django_user_model +): + django_user_model.objects.create(username="first") + django_user_model.objects.create(username="second") segment = SegmentFactory.build(type=Segment.TYPE_STATIC) rule = TimeRule( @@ -530,16 +601,18 @@ def test_count_matching_users_only_counts_static_rules(site, client, mocker, dja segment=segment, ) form = form_with_data(segment, rule) - mock_test_user = mocker.patch('wagtail_personalisation.rules.TimeRule.test_user') + mock_test_user = mocker.patch("wagtail_personalisation.rules.TimeRule.test_user") assert form.count_matching_users([rule], True) == 0 assert mock_test_user.call_count == 0 @pytest.mark.django_db -def test_count_matching_users_handles_match_any(site, client, mocker, django_user_model): - django_user_model.objects.create(username='first') - django_user_model.objects.create(username='second') +def test_count_matching_users_handles_match_any( + site, client, mocker, django_user_model +): + django_user_model.objects.create(username="first") + django_user_model.objects.create(username="second") segment = SegmentFactory.build(type=Segment.TYPE_STATIC) first_rule = VisitCountRule(counted_page=site.root_page) @@ -548,17 +621,20 @@ def test_count_matching_users_handles_match_any(site, client, mocker, django_use form = form_with_data(segment, first_rule, second_rule) mock_test_user = mocker.patch( - 'wagtail_personalisation.rules.VisitCountRule.test_user', - side_effect=[True, False, True, False]) + "wagtail_personalisation.rules.VisitCountRule.test_user", + side_effect=[True, False, True, False], + ) assert form.count_matching_users([first_rule, second_rule], True) == 2 mock_test_user.call_count == 4 @pytest.mark.django_db -def test_count_matching_users_handles_match_all(site, client, mocker, django_user_model): - django_user_model.objects.create(username='first') - django_user_model.objects.create(username='second') +def test_count_matching_users_handles_match_all( + site, client, mocker, django_user_model +): + django_user_model.objects.create(username="first") + django_user_model.objects.create(username="second") segment = SegmentFactory.build(type=Segment.TYPE_STATIC) first_rule = VisitCountRule(counted_page=site.root_page) @@ -567,8 +643,9 @@ def test_count_matching_users_handles_match_all(site, client, mocker, django_use form = form_with_data(segment, first_rule, second_rule) mock_test_user = mocker.patch( - 'wagtail_personalisation.rules.VisitCountRule.test_user', - side_effect=[True, True, False, True]) + "wagtail_personalisation.rules.VisitCountRule.test_user", + side_effect=[True, True, False, True], + ) assert form.count_matching_users([first_rule, second_rule], False) == 1 mock_test_user.call_count == 4 diff --git a/tests/unit/test_templatetags.py b/tests/unit/test_templatetags.py index 92116bb4..d6d47035 100644 --- a/tests/unit/test_templatetags.py +++ b/tests/unit/test_templatetags.py @@ -11,33 +11,44 @@ @pytest.mark.django_db def test_segment_template_block(rf, site): - SegmentFactory(name='test', persistent=True) + SegmentFactory(name="test", persistent=True) - request = rf.get('/') + request = rf.get("/") - request.session['segments'] = [{ - "encoded_name": 'test', - "id": 1, - "timestamp": int(time.time()), - "persistent": True - }] + request.session["segments"] = [ + { + "encoded_name": "test", + "id": 1, + "timestamp": int(time.time()), + "persistent": True, + } + ] - content = render_template(""" + content = render_template( + """ {% load wagtail_personalisation_tags %} {% segment name='test' %}Test{% endsegment %} - """, request=request).strip() + """, + request=request, + ).strip() assert content == "Test" - content = render_template(""" + content = render_template( + """ {% load wagtail_personalisation_tags %} {% segment name='test2' %}Test{% endsegment %} - """, request=request).strip() + """, + request=request, + ).strip() assert content == "" with pytest.raises(TemplateSyntaxError): - content = render_template(""" + content = render_template( + """ {% load wagtail_personalisation_tags %} {% segment wrongname='test2' %}Test{% endsegment %} - """, request=request).strip() + """, + request=request, + ).strip() diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 42c0d0fa..12caf84f 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1,16 +1,20 @@ import pytest from django.test import override_settings -from wagtail.core.models import Page as WagtailPage +from wagtail.models import Page as WagtailPage -from tests.factories.page import (ContentPageFactory, PersonalisablePageMetadataFactory) +from tests.factories.page import ContentPageFactory, PersonalisablePageMetadataFactory from wagtail_personalisation.utils import ( - can_delete_pages, exclude_variants, get_client_ip, impersonate_other_page) - + can_delete_pages, + exclude_variants, + get_client_ip, + impersonate_other_page, +) locale_factory = False try: - from tests.factories.page import LocaleFactory + from tests.factories.page import LocaleFactory # noqa + locale_factory = True except ImportError: pass @@ -18,23 +22,23 @@ @pytest.fixture def rootpage(): - return ContentPageFactory(parent=None, path='/', depth=0, title='root') + return ContentPageFactory(parent=None, path="/", depth=0, title="root") @pytest.fixture def page(rootpage): - return ContentPageFactory(parent=rootpage, path='/hi', title='Hi') + return ContentPageFactory(parent=rootpage, path="/hi", title="Hi") @pytest.fixture def otherpage(rootpage): - return ContentPageFactory(parent=rootpage, path='/bye', title='Bye') + return ContentPageFactory(parent=rootpage, path="/bye", title="Bye") @pytest.mark.django_db def test_impersonate_other_page(page, otherpage): impersonate_other_page(page, otherpage) - assert page.title == otherpage.title == 'Bye' + assert page.title == otherpage.title == "Bye" assert page.path == otherpage.path @@ -50,58 +54,63 @@ def test_cannot_delete_pages_with_standard_user(user, segmented_page): def test_get_client_ip_with_remote_addr(rf): - request = rf.get('/', REMOTE_ADDR='173.231.235.87') - assert get_client_ip(request) == '173.231.235.87' + request = rf.get("/", REMOTE_ADDR="173.231.235.87") + assert get_client_ip(request) == "173.231.235.87" def test_get_client_ip_with_x_forwarded_for(rf): - request = rf.get('/', HTTP_X_FORWARDED_FOR='173.231.235.87', - REMOTE_ADDR='10.0.23.24') - assert get_client_ip(request) == '173.231.235.87' + request = rf.get( + "/", HTTP_X_FORWARDED_FOR="173.231.235.87", REMOTE_ADDR="10.0.23.24" + ) + assert get_client_ip(request) == "173.231.235.87" -@override_settings( - WAGTAIL_PERSONALISATION_IP_FUNCTION='some.non.existent.path' -) +@override_settings(WAGTAIL_PERSONALISATION_IP_FUNCTION="some.non.existent.path") def test_get_client_ip_custom_get_client_ip_function_does_not_exist(rf): with pytest.raises(ImportError): - get_client_ip(rf.get('/')) + get_client_ip(rf.get("/")) -@override_settings( - WAGTAIL_PERSONALISATION_IP_FUNCTION='tests.utils.get_custom_ip' -) +@override_settings(WAGTAIL_PERSONALISATION_IP_FUNCTION="tests.utils.get_custom_ip") def test_get_client_ip_custom_get_client_ip_used(rf): - assert get_client_ip(rf.get('/')) == '123.123.123.123' + assert get_client_ip(rf.get("/")) == "123.123.123.123" def test_exclude_variants_with_pages_querysets(): - ''' + """ Test that excludes variant works for querysets - ''' + """ for i in range(5): - page = ContentPageFactory(path="/" + str(i), depth=0, url_path="/", title="Hoi " + str(i)) + page = ContentPageFactory( + path="/" + str(i), depth=0, url_path="/", title="Hoi " + str(i) + ) page.save() - pages = WagtailPage.objects.all().specific().order_by('id') + pages = WagtailPage.objects.all().specific().order_by("id") result = exclude_variants(pages) assert type(result) == type(pages) - assert set(result.values_list('pk', flat=True)) == set(pages.values_list('pk', flat=True)) + assert set(result.values_list("pk", flat=True)) == set( + pages.values_list("pk", flat=True) + ) def test_exclude_variants_with_pages_querysets_not_canonical(): - ''' + """ Test that excludes variant works for querysets with personalisation_metadata canonical False - ''' + """ for i in range(5): - page = ContentPageFactory(path="/" + str(i), depth=0, url_path="/", title="Hoi " + str(i)) + page = ContentPageFactory( + path="/" + str(i), depth=0, url_path="/", title="Hoi " + str(i) + ) page.save() - pages = WagtailPage.objects.all().specific().order_by('id') + pages = WagtailPage.objects.all().specific().order_by("id") # add variants for page in pages: - variant = ContentPageFactory(title='variant %d' % page.pk) - page.personalisation_metadata = PersonalisablePageMetadataFactory(canonical_page=page, variant=variant) + variant = ContentPageFactory(title="variant %d" % page.pk) + page.personalisation_metadata = PersonalisablePageMetadataFactory( + canonical_page=page, variant=variant + ) page.save() pages = WagtailPage.objects.all().specific() @@ -111,19 +120,25 @@ def test_exclude_variants_with_pages_querysets_not_canonical(): def test_exclude_variants_with_pages_querysets_meta_none(): - ''' + """ Test that excludes variant works for querysets with meta as none - ''' + """ for i in range(5): - page = ContentPageFactory(path="/" + str(i), depth=0, url_path="/", title="Hoi " + str(i)) + page = ContentPageFactory( + path="/" + str(i), depth=0, url_path="/", title="Hoi " + str(i) + ) page.save() - pages = WagtailPage.objects.all().specific().order_by('id') + pages = WagtailPage.objects.all().specific().order_by("id") # add variants for page in pages: - page.personalisation_metadata = PersonalisablePageMetadataFactory(canonical_page=page, variant=page) + page.personalisation_metadata = PersonalisablePageMetadataFactory( + canonical_page=page, variant=page + ) page.save() pages = WagtailPage.objects.all().specific() result = exclude_variants(pages) assert type(result) == type(pages) - assert set(result.values_list('pk', flat=True)) == set(pages.values_list('pk', flat=True)) + assert set(result.values_list("pk", flat=True)) == set( + pages.values_list("pk", flat=True) + ) diff --git a/tests/unit/test_views.py b/tests/unit/test_views.py index f70116b8..444b8e88 100644 --- a/tests/unit/test_views.py +++ b/tests/unit/test_views.py @@ -1,35 +1,33 @@ import pytest from django.core.exceptions import PermissionDenied from django.urls import reverse -from wagtail.core.models import Page +from wagtail.models import Page from wagtail_personalisation.models import Segment from wagtail_personalisation.rules import VisitCountRule -from wagtail_personalisation.views import ( - SegmentModelAdmin, SegmentModelDeleteView) +from wagtail_personalisation.views import SegmentModelAdmin, SegmentModelDeleteView @pytest.mark.django_db def test_segment_user_data_view_requires_admin_access(site, client, django_user_model): - user = django_user_model.objects.create(username='first') + user = django_user_model.objects.create(username="first") segment = Segment(type=Segment.TYPE_STATIC, count=1) segment.save() client.force_login(user) - url = reverse('segment:segment_user_data', args=(segment.id,)) + url = reverse("segment:segment_user_data", args=(segment.id,)) response = client.get(url) assert response.status_code == 302 - assert response.url == '/admin/login/?next=%s' % url + assert response.url == "/admin/login/?next=%s" % url @pytest.mark.django_db 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', is_superuser=True) + 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", is_superuser=True) segment = Segment(type=Segment.TYPE_STATIC, count=1) segment.save() @@ -37,24 +35,28 @@ def test_segment_user_data_view(site, client, mocker, django_user_model): segment.static_users.add(user2) rule1 = VisitCountRule(counted_page=site.root_page, segment=segment) - rule2 = VisitCountRule(counted_page=site.root_page.get_last_child(), - segment=segment) + rule2 = VisitCountRule( + counted_page=site.root_page.get_last_child(), segment=segment + ) rule1.save() rule2.save() - mocker.patch('wagtail_personalisation.rules.VisitCountRule.get_user_info_string', - side_effect=[3, 9, 0, 1]) + mocker.patch( + "wagtail_personalisation.rules.VisitCountRule.get_user_info_string", + side_effect=[3, 9, 0, 1], + ) client.force_login(admin_user) - response = client.get( - reverse('segment:segment_user_data', args=(segment.id,))) + response = client.get(reverse("segment:segment_user_data", args=(segment.id,))) assert response.status_code == 200 data_lines = response.content.decode().split("\n") - 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' + 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 @@ -64,9 +66,9 @@ def test_segment_delete_view_delete_instance(rf, segmented_page, user): 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) - )) + 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 @@ -74,11 +76,10 @@ def test_segment_delete_view_delete_instance(rf, segmented_page, user): assert variants_metadata # Delete the segment via the method on the view. - request = rf.get('/'.format(segment.pk)) + request = rf.get("/".format(segment.pk)) # noqa: F523 request.user = user view = SegmentModelDeleteView( - instance_pk=str(segment.pk), - model_admin=SegmentModelAdmin() + instance_pk=str(segment.pk), model_admin=SegmentModelAdmin() ) view.request = request view.delete_instance() @@ -98,13 +99,12 @@ def test_segment_delete_view_delete_instance(rf, segmented_page, user): @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 = rf.get("/".format(segment.pk)) # noqa: F523 request.user = user view = SegmentModelDeleteView( - instance_pk=str(segment.pk), - model_admin=SegmentModelAdmin() + instance_pk=str(segment.pk), model_admin=SegmentModelAdmin() ) view.request = request - message = 'User have no permission to delete variant page objects.' + message = "User have no permission to delete variant page objects." # noqa: F841 with pytest.raises(PermissionDenied): view.delete_instance() diff --git a/tests/unit/test_wagtail_hooks.py b/tests/unit/test_wagtail_hooks.py index 917ac2fc..07de88c6 100644 --- a/tests/unit/test_wagtail_hooks.py +++ b/tests/unit/test_wagtail_hooks.py @@ -1,6 +1,6 @@ import pytest from django.http import Http404 -from wagtail.core.models import Page +from wagtail.models import Page from tests.factories.page import ContentPageFactory from tests.factories.segment import SegmentFactory @@ -10,7 +10,7 @@ @pytest.mark.django_db def test_serve_variant_no_variant(site, rf): page = site.root_page - request = rf.get('/') + request = rf.get("/") args = tuple() kwargs = {} @@ -20,7 +20,7 @@ def test_serve_variant_no_variant(site, rf): @pytest.mark.django_db def test_variant_accessed_directly_returns_404(segmented_page, rf): - request = rf.get('/') + request = rf.get("/") args = tuple() kwargs = {} with pytest.raises(Http404): @@ -29,7 +29,7 @@ def test_variant_accessed_directly_returns_404(segmented_page, rf): @pytest.mark.django_db def test_serve_variant_with_variant_no_segment(site, rf, segmented_page): - request = rf.get('/') + request = rf.get("/") args = tuple() kwargs = {} @@ -40,7 +40,7 @@ def test_serve_variant_with_variant_no_segment(site, rf, segmented_page): @pytest.mark.django_db def test_serve_variant_with_variant_segmented(site, rf, segmented_page): - request = rf.get('/') + request = rf.get("/") args = tuple() kwargs = {} @@ -58,7 +58,7 @@ def test_serve_variant_with_variant_segmented(site, rf, segmented_page): def test_page_listing_variant_buttons(site, rf, segmented_page): page = segmented_page.personalisation_metadata.canonical_page - SegmentFactory(name='something') + SegmentFactory(name="something") result = wagtail_hooks.page_listing_variant_buttons(page, []) items = list(result) assert len(items) == 1 @@ -68,47 +68,42 @@ def test_page_listing_variant_buttons(site, rf, segmented_page): def test_page_listing_more_buttons(site, rf, segmented_page): page = segmented_page.personalisation_metadata.canonical_page - SegmentFactory(name='something') + SegmentFactory(name="something") 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 +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 -): +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 + 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('/') + 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 = 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()) @@ -126,18 +121,20 @@ def test_custom_delete_page_view_deletes_variants(rf, segmented_page, user): @pytest.mark.django_db -def test_custom_delete_page_view_deletes_variants_of_child_pages(rf, segmented_page, user): +def test_custom_delete_page_view_deletes_variants_of_child_pages( + rf, segmented_page, user +): """ Regression test for deleting pages that have children with variants """ - post_request = rf.post('/') + post_request = rf.post("/") user.is_superuser = True rf.user = user canonical_page = segmented_page.personalisation_metadata.canonical_page # Create a child with a variant - child_page = ContentPageFactory(parent=canonical_page, slug='personalised-child') - child_page.personalisation_metadata.copy_for_segment(segmented_page.personalisation_metadata.segment) - # A ProtectedError would be raised if the bug persists - wagtail_hooks.delete_related_variants( - post_request, canonical_page + child_page = ContentPageFactory(parent=canonical_page, slug="personalised-child") + child_page.personalisation_metadata.copy_for_segment( + segmented_page.personalisation_metadata.segment ) + # A ProtectedError would be raised if the bug persists + wagtail_hooks.delete_related_variants(post_request, canonical_page) diff --git a/tests/utils.py b/tests/utils.py index e1a5e49a..77b54589 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -2,10 +2,10 @@ def render_template(value, **context): - template = engines['django'].from_string(value) - request = context.pop('request', None) + template = engines["django"].from_string(value) + request = context.pop("request", None) return template.render(context, request) def get_custom_ip(request): - return '123.123.123.123' + return "123.123.123.123" diff --git a/tox.ini b/tox.ini index be4f1c79..6e6fa213 100644 --- a/tox.ini +++ b/tox.ini @@ -1,33 +1,37 @@ [tox] envlist = flake8 - py{36,37,38}-dj{22}-wt{211,212,213} - py{37,38}-dj{30,31}-wt{211,212,213} + py{37}-dj{32}-wt{41,42} + py{38,39,310}-dj{32,40,41}-wt{41,42} + py{311}-dj{41}-wt{41,42} [gh-actions] python = - 3.6: py36 3.7: py37 3.8: py38 + 3.9: py39 + 3.10: py310 + 3.11: py311 [testenv] basepython = - py36: python3.6 py37: python3.7 py38: python3.8 + py39: python3.9 + py310: python3.10 + py311: python3.11 commands = coverage run --parallel -m pytest -rs {posargs} extras = test deps = - dj22: Django>=2.2.8,<2.3 - dj30: Django>=3.0,<3.1 - dj31: Django>=3.1,<3.2 - wt211: wagtail>=2.11,<2.12 - wt212: wagtail>=2.12,<2.13 - wt213: wagtail>=2.13,<2.14 + dj32: Django>=3.2,<3.3 + dj40: Django>=4.0,<4.1 + dj41: Django>=4.1,<4.2 + wt41: wagtail>=4.1,<4.2 + wt42: wagtail>=4.2,<5.0 geoip2: geoip2 [testenv:coverage-report] -basepython = python3.6 +basepython = python3.8 deps = coverage pip_pre = true skip_install = true @@ -35,7 +39,7 @@ commands = coverage report --include="src/**/" --omit="src/**/migrations/*.py" [testenv:lint] -basepython = python3.6 +basepython = python3.8 deps = flake8==3.5.0 commands = flake8 src tests setup.py @@ -48,4 +52,4 @@ deps = black skip_install = true commands = - black --check setup.py src/wagtail_personalisation/ tests/ + black --check --exclude=/migrations setup.py src/wagtail_personalisation/ tests/