Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade for Wagtail 4.0 #49

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- [Wagtail 3.0 compatibility](https://github.com/torchbox/wagtail-ab-testing/pull/48)

## [0.7] - 2022-03-31

- [Add default_auto_field](https://github.com/torchbox/wagtail-ab-testing/pull/42)
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Key features:

## Usage

**The code examples below assume you are using Wagtail v3.0+** If you need information for an earlier version of Wagtail see the [Release notes](https://docs.wagtail.org/en/stable/releases/upgrading.html) for guidance.

Wagtail A/B Testing works with Django 2.2+, Wagtail 2.11+ on Python 3.7+ environments.

### Creating an A/B test

Any user with the "Create A/B test" permission can create an A/B test by clicking "Save and create A/B test" from the page's action menu.
Expand Down Expand Up @@ -72,7 +76,7 @@ from wagtail_ab_testing import urls as ab_testing_urls
urlpatterns = [
...

url(r'^abtesting/', include(ab_testing_urls)),
path('abtesting/', include(ab_testing_urls)),
]
```

Expand All @@ -99,7 +103,7 @@ add your goal type to the list of options shown to users when they create A/B te
```python
# myapp/wagtail_hooks.py

from wagtail.core import hooks
from wagtail import hooks
from wagtail_ab_testing.events import BaseEvent


Expand Down Expand Up @@ -143,7 +147,7 @@ Firstly, we need to register the event type. To do this, implement a handler for
```python
# myapp/wagtail_hooks.py

from wagtail.core import hooks
from wagtail import hooks
from wagtail_ab_testing.events import BaseEvent

from .models import ContactUsFormPage
Expand Down
16 changes: 10 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,26 @@
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Framework :: Django",
"Framework :: Django :: 2.2",
"Framework :: Django :: 3.0",
"Framework :: Django :: 3.2",
"Framework :: Django :: 4.0",
"Framework :: Django :: 4.1",
"Framework :: Wagtail",
"Framework :: Wagtail :: 2",
"Framework :: Wagtail :: 3.0",
"Framework :: Wagtail :: 4.0",
],
install_requires=[
"Django>=2.2,<3.3",
"Wagtail>=2.11,<2.17",
"Django>=3.0,<4.2",
"Wagtail>=2.15,<4.1",
"user-agents>=2.2,<2.3",
"numpy>=1.19.4,<1.20",
"scipy>=1.5.4,<1.6",
"numpy>=1.19.4,<1.23",
"scipy>=1.5.4,<1.9",
],
extras_require={
"testing": ["dj-database-url==0.5.0", "freezegun==0.3.15"],
"testing": ["dj-database-url==0.5.0", "freezegun==1.2.1"],
},
zip_safe=False,
)
9 changes: 7 additions & 2 deletions testmanage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

from django.core.management import execute_from_command_line

from wagtail import VERSION as WAGTAIL_VERSION

if WAGTAIL_VERSION >= (3, 0):
from wagtail.test.settings import STATIC_ROOT, MEDIA_ROOT
else:
from wagtail.tests.settings import STATIC_ROOT, MEDIA_ROOT


os.environ["DJANGO_SETTINGS_MODULE"] = "wagtail_ab_testing.test.settings"

Expand Down Expand Up @@ -56,8 +63,6 @@ def runtests():
try:
execute_from_command_line(argv)
finally:
from wagtail.tests.settings import STATIC_ROOT, MEDIA_ROOT

shutil.rmtree(STATIC_ROOT, ignore_errors=True)
shutil.rmtree(MEDIA_ROOT, ignore_errors=True)

Expand Down
20 changes: 14 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
skipsdist = True
usedevelop = True

envlist = py{37,38,39}-dj{22,30,32,main}-wa{211,215,216,main}-{sqlite,postgres}
envlist =
py{37,38,39,310}-dj{30,31,32}-wa{215}-{sqlite,postgres}
py{37,38,39,310}-dj{32,40}-wa{216,30}-{sqlite,postgres}
py{38,39,310}-dj{32,40,41}-wa{40,main}-{sqlite,postgres}

[flake8]
# E501: Line too long
Expand All @@ -17,21 +20,26 @@ commands = coverage run testmanage.py test --deprecation all
basepython =
py37: python3.7
py38: python3.8
py39: python3.9
py310: python3.10

deps =
coverage

dj22: Django>=2.2,<2.3
dj30: Django>=3.0,<3.1
dj31: Django>=3.1,<3.2
dj32: Django>=3.2,<3.3
dj40: Django>=4.0,<4.1
dj41: Django>=4.1,<4.2
djmain: git+https://github.com/django/django.git@main#egg=Django

wa211: wagtail>=2.11,<2.12
wa215: wagtail>=2.15rc1,<2.16
wa216: wagtail>=2.16.1,<2.17
wa215: wagtail>=2.15,<2.16
wa216: wagtail>=2.16,<2.17
wa30: wagtail>=3.0,<4.0
wa40: wagtail>=4.0,<4.1
wamain: git+https://github.com/wagtail/wagtail.git

postgres: psycopg2==2.8.6
postgres: psycopg2==2.9.3

setenv =
postgres: DATABASE_URL=postgres:///wagtail_ab_testing
Expand Down
5 changes: 4 additions & 1 deletion wagtail_ab_testing/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
default_app_config = "wagtail_ab_testing.apps.WagtailAbTestingAppConfig"
from django import VERSION as DJANGO_VERSION

if DJANGO_VERSION < (3, 2):
default_app_config = "wagtail_ab_testing.apps.WagtailAbTestingAppConfig"
13 changes: 11 additions & 2 deletions wagtail_ab_testing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
from rest_framework import fields, routers, serializers, status, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from wagtail.core.models import Page, Site

from wagtail import VERSION as WAGTAIL_VERSION

if WAGTAIL_VERSION >= (3, 0):
from wagtail.models import Page, Site
else:
from wagtail.core.models import Page, Site

from .models import AbTest

Expand Down Expand Up @@ -64,7 +70,10 @@ def serve_variant(self, request, pk=None):
test = self.get_object()
request.wagtail_ab_testing_test = test
request.wagtail_ab_testing_serving_variant = True
return test.variant_revision.as_page_object().serve(request)
if WAGTAIL_VERSION >= (4, 0):
return test.variant_revision.as_object().serve(request)
else:
return test.variant_revision.as_page_object().serve(request)

@action(detail=True, methods=['post'])
def add_participant(self, request, pk=None):
Expand Down
6 changes: 5 additions & 1 deletion wagtail_ab_testing/events.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from django.utils.translation import gettext_lazy as __
from wagtail import VERSION as WAGTAIL_VERSION

from wagtail.core import hooks
if WAGTAIL_VERSION >= (3, 0):
from wagtail import hooks
else:
from wagtail.core import hooks


class BaseEvent:
Expand Down
16 changes: 14 additions & 2 deletions wagtail_ab_testing/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext as _, gettext_lazy as __
from wagtail.core.signals import page_unpublished

from wagtail import VERSION as WAGTAIL_VERSION

if WAGTAIL_VERSION >= (3, 0):
from wagtail.signals import page_unpublished
else:
from wagtail.core.signals import page_unpublished

from .events import get_event_types


def get_revision_model():
if WAGTAIL_VERSION >= (4, 0):
return "wagtailcore.Revision"
return "wagtailcore.PageRevision"


class AbTestManager(models.Manager):
def get_current_for_page(self, page):
return self.get_queryset().filter(page=page).exclude(status__in=[AbTest.STATUS_CANCELLED, AbTest.STATUS_COMPLETED]).first()
Expand Down Expand Up @@ -73,7 +85,7 @@ class AbTest(models.Model):
page = models.ForeignKey('wagtailcore.Page', on_delete=models.CASCADE, related_name='ab_tests')
name = models.CharField(max_length=255)
hypothesis = models.TextField(blank=True)
variant_revision = models.ForeignKey('wagtailcore.PageRevision', on_delete=models.CASCADE, related_name='+')
variant_revision = models.ForeignKey(get_revision_model(), on_delete=models.CASCADE, related_name='+')
goal_event = models.CharField(max_length=255)
goal_page = models.ForeignKey('wagtailcore.Page', null=True, blank=True, on_delete=models.SET_NULL, related_name='+')
sample_size = models.PositiveIntegerField(validators=[MinValueValidator(1)])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ <h3>{% trans "Sample size" %}</h3>
{% endblock %}

{% block extra_css %}
{% if not is_wagtail_4 %}
{% include "wagtailadmin/pages/_editor_css.html" %}
{% endif %}
{% endblock %}

{% block extra_js %}
{% include "wagtailadmin/pages/_editor_js.html" %}
<script>
Expand Down
5 changes: 4 additions & 1 deletion wagtail_ab_testing/test/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
default_app_config = "wagtail_ab_testing.test.apps.WagtailAbTestingTestAppConfig"
from django import VERSION as DJANGO_VERSION

if DJANGO_VERSION < (3, 2):
default_app_config = "wagtail_ab_testing.test.apps.WagtailAbTestingTestAppConfig"
7 changes: 6 additions & 1 deletion wagtail_ab_testing/test/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from wagtail.core.models import Page
from wagtail import VERSION as WAGTAIL_VERSION

if WAGTAIL_VERSION >= (3, 0):
from wagtail.models import Page
else:
from wagtail.core.models import Page


class SimplePage(Page):
Expand Down
12 changes: 12 additions & 0 deletions wagtail_ab_testing/test/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import os
import dj_database_url

from wagtail import VERSION as WAGTAIL_VERSION

# Build paths inside the project like this: os.path.join(PROJECT_DIR, ...)
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
Expand Down Expand Up @@ -101,6 +103,13 @@
}
}

# while search isn't used in the tests, wagtail.core checks for the presence of it
# because it's in INSTALLED_APPS
WAGTAILSEARCH_BACKENDS = {
'default': {
'BACKEND': 'wagtail.search.backends.database',
}
}

# don't use the intentionally slow default password hasher
PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",)
Expand Down Expand Up @@ -158,3 +167,6 @@


WAGTAIL_SITE_NAME = "Wagtail A/B Testing test site"

if WAGTAIL_VERSION >= (3, 0):
WAGTAILADMIN_BASE_URL = "http://example.com"
7 changes: 6 additions & 1 deletion wagtail_ab_testing/test/tests/test_abtest_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

from django.test import TestCase
from freezegun import freeze_time
from wagtail.core.models import Page
from wagtail import VERSION as WAGTAIL_VERSION

if WAGTAIL_VERSION >= (3, 0):
from wagtail.models import Page
else:
from wagtail.core.models import Page

from wagtail_ab_testing.models import AbTest, AbTestHourlyLog

Expand Down
11 changes: 9 additions & 2 deletions wagtail_ab_testing/test/tests/test_add_abtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from django.urls import reverse
from wagtail.core.models import Page
from wagtail.tests.utils import WagtailTestUtils

from wagtail import VERSION as WAGTAIL_VERSION

if WAGTAIL_VERSION >= (3, 0):
from wagtail.models import Page
from wagtail.test.utils import WagtailTestUtils
else:
from wagtail.core.models import Page
from wagtail.tests.utils import WagtailTestUtils

from wagtail_ab_testing.models import AbTest
from wagtail_ab_testing.test.models import SimplePage
Expand Down
7 changes: 6 additions & 1 deletion wagtail_ab_testing/test/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
from django.urls import reverse
from freezegun import freeze_time
from rest_framework.test import APITestCase
from wagtail.core.models import Page
from wagtail import VERSION as WAGTAIL_VERSION

if WAGTAIL_VERSION >= (3, 0):
from wagtail.models import Page
else:
from wagtail.core.models import Page

from wagtail_ab_testing.models import AbTest
from wagtail_ab_testing.test.models import SimplePage
Expand Down
11 changes: 9 additions & 2 deletions wagtail_ab_testing/test/tests/test_compare_draft.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from django.test import TestCase
from django.urls import reverse
from wagtail.core.models import Page
from wagtail.tests.utils import WagtailTestUtils

from wagtail import VERSION as WAGTAIL_VERSION

if WAGTAIL_VERSION >= (3, 0):
from wagtail.models import Page
from wagtail.test.utils import WagtailTestUtils
else:
from wagtail.core.models import Page
from wagtail.tests.utils import WagtailTestUtils

from wagtail_ab_testing.models import AbTest
from wagtail_ab_testing.test.models import SimplePage
Expand Down
11 changes: 9 additions & 2 deletions wagtail_ab_testing/test/tests/test_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from django.urls import reverse
from wagtail.core.models import Page
from wagtail.tests.utils import WagtailTestUtils

from wagtail import VERSION as WAGTAIL_VERSION

if WAGTAIL_VERSION >= (3, 0):
from wagtail.models import Page
from wagtail.test.utils import WagtailTestUtils
else:
from wagtail.core.models import Page
from wagtail.tests.utils import WagtailTestUtils

from wagtail_ab_testing.models import AbTest
from wagtail_ab_testing.test.models import SimplePage
Expand Down
11 changes: 9 additions & 2 deletions wagtail_ab_testing/test/tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from django.urls import reverse
from wagtail.core.models import Page
from wagtail.tests.utils import WagtailTestUtils

from wagtail import VERSION as WAGTAIL_VERSION

if WAGTAIL_VERSION >= (3, 0):
from wagtail.models import Page
from wagtail.test.utils import WagtailTestUtils
else:
from wagtail.core.models import Page
from wagtail.tests.utils import WagtailTestUtils

from wagtail_ab_testing.models import AbTest
from wagtail_ab_testing.test.models import SimplePage
Expand Down
Loading