From d4dbb3794c9ce50016ee92cef28bfd07dc892cf0 Mon Sep 17 00:00:00 2001 From: Siburg Date: Sun, 4 Jun 2023 14:48:43 +1200 Subject: [PATCH] Ensure compatibility with Wagtail 3.0 - Apply Wagtail 3.0 changes to module paths - Replace ImageChooserPanel with FieldPanel - Remove obsolete Wagtail SiteMiddleware references - Remove default_app_config for Django 3.2 - Update requirements.txt and setting for example app - Update external links in base.html - Start updating README.rst and setup.cfg - Fix linter complaints --- README.rst | 4 +-- example/localsite/__init__.py | 1 - example/localsite/apps.py | 4 +-- example/puput_migrations/0001_initial.py | 6 ++-- .../0002_auto_20150919_0925.py | 4 +-- example/requirements.txt | 8 ++--- example/settings.py | 12 ++++++-- example/templates/puput/base.html | 6 ++-- setup.cfg | 15 +++++----- wagtailmetadata/models.py | 29 ++++++------------- wagtailmetadata/tests/models.py | 2 +- wagtailmetadata/tests/settings.py | 3 +- wagtailmetadata/tests/test_mixin.py | 5 ++-- wagtailmetadata/tests/urls.py | 2 +- 14 files changed, 48 insertions(+), 53 deletions(-) diff --git a/README.rst b/README.rst index d4fd26d..34d2d26 100644 --- a/README.rst +++ b/README.rst @@ -19,7 +19,7 @@ The current version is tested for compatiblily with the following: - Wagtail versions 2.7 to 2.15 - Django versions 2.2 to 3.2 -- Python versions 3.6 and 3.9 +- Python versions 3.7 to 3.10 Authored by `Basil Shubin `_, and some great `contributors `_. @@ -74,7 +74,7 @@ Usage # models.py - from wagtail.core.models import Page, PageBase + from wagtail.models import Page, PageBase from wagtailmetadata.models import MetadataPageMixin diff --git a/example/localsite/__init__.py b/example/localsite/__init__.py index bc46824..e69de29 100644 --- a/example/localsite/__init__.py +++ b/example/localsite/__init__.py @@ -1 +0,0 @@ -default_app_config = "localsite.apps.DefaultConfig" diff --git a/example/localsite/apps.py b/example/localsite/apps.py index 31ef080..aed4574 100644 --- a/example/localsite/apps.py +++ b/example/localsite/apps.py @@ -3,7 +3,7 @@ from django.template.defaultfilters import truncatewords -def mixin(cls, mixins): +def add_mixins(cls, mixins): for mixin in mixins: if mixin not in cls.__bases__: cls.__bases__ = (mixin,) + cls.__bases__ @@ -13,7 +13,7 @@ def mixin(cls, mixins): def handle_blog_model(sender, instance, **kwargs): from wagtailmetadata.models import MetadataMixin - sender = mixin(sender, [MetadataMixin]) + sender = add_mixins(sender, [MetadataMixin]) def get_meta_description(cls): return cls.search_description or truncatewords(cls.description, 20) diff --git a/example/puput_migrations/0001_initial.py b/example/puput_migrations/0001_initial.py index 39ce988..84f986d 100644 --- a/example/puput_migrations/0001_initial.py +++ b/example/puput_migrations/0001_initial.py @@ -4,7 +4,7 @@ import modelcluster.contrib.taggit import modelcluster.fields import puput.routes -import wagtail.core.fields +import wagtail.fields from django.db import migrations, models @@ -111,11 +111,11 @@ class Migration(migrations.Migration): on_delete=models.CASCADE, ), ), - ("body", wagtail.core.fields.RichTextField(verbose_name="body")), + ("body", wagtail.fields.RichTextField(verbose_name="body")), ("date", models.DateTimeField(default=datetime.datetime.today, verbose_name="Post date")), ( "excerpt", - wagtail.core.fields.RichTextField( + wagtail.fields.RichTextField( help_text="Used to display on puput pages list. If this field is not filled, a truncate version of body text will be used.", verbose_name="excerpt", blank=True, diff --git a/example/puput_migrations/0002_auto_20150919_0925.py b/example/puput_migrations/0002_auto_20150919_0925.py index 17681b2..8597d4b 100644 --- a/example/puput_migrations/0002_auto_20150919_0925.py +++ b/example/puput_migrations/0002_auto_20150919_0925.py @@ -1,4 +1,4 @@ -import wagtail.core.fields +import wagtail.fields from django.db import migrations, models @@ -42,7 +42,7 @@ class Migration(migrations.Migration): migrations.AlterField( model_name="entrypage", name="excerpt", - field=wagtail.core.fields.RichTextField( + field=wagtail.fields.RichTextField( help_text="Entry excerpt to be displayed on entries list. If this field is not filled, a truncate version of body text will be used.", verbose_name="excerpt", blank=True, diff --git a/example/requirements.txt b/example/requirements.txt index 5931305..7c0433c 100644 --- a/example/requirements.txt +++ b/example/requirements.txt @@ -1,4 +1,4 @@ -django==3.0.14 -wagtail>2.11,<3.0 -django-meta>1.7.0 # https://github.com/nephila/django-meta -puput==1.1.1 # https://github.com/APSL/puput +Django == 3.2.* +wagtail == 4.1.* +django-meta >= 2.1.0 # https://github.com/nephila/django-meta +puput == 1.2.0 # https://github.com/APSL/puput diff --git a/example/settings.py b/example/settings.py index ccaee70..4105d9a 100644 --- a/example/settings.py +++ b/example/settings.py @@ -59,7 +59,6 @@ "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", # 'django.middleware.security.SecurityMiddleware', - "wagtail.core.middleware.SiteMiddleware", "wagtail.contrib.redirects.middleware.RedirectMiddleware", ] @@ -88,7 +87,12 @@ # Database # https://docs.djangoproject.com/en/1.8/ref/settings/#databases -DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": os.path.join(BASE_DIR, "db.sqlite3")}} +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": os.path.join(BASE_DIR, "db.sqlite3"), + } +} MIGRATION_MODULES = {"puput": "example.puput_migrations"} @@ -134,3 +138,7 @@ # wagtail WAGTAIL_SITE_NAME = "Puput Blog" +# Using an empty string is only useful to suppress Wagtail warning for missing +# setting. Set this to something realistic, if necessary. See +# https://docs.wagtail.org/en/latest/reference/settings.html#wagtailadmin-base-url +WAGTAILADMIN_BASE_URL = '' diff --git a/example/templates/puput/base.html b/example/templates/puput/base.html index 7214bd4..b5e7afc 100644 --- a/example/templates/puput/base.html +++ b/example/templates/puput/base.html @@ -110,9 +110,9 @@

{% trans 'Archive' %}

diff --git a/setup.cfg b/setup.cfg index e6ebbd0..4245b85 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = wagtail-metadata-mixin -version = 2.0.2 +version = 3.0 description = OpenGraph, Twitter Card and Schema.org snippet tags for Wagtail CMS pages long_description = file: README.rst, CHANGES.rst long_description_content_type = text/x-rst @@ -17,17 +17,16 @@ classifiers = Operating System :: OS Independent Programming Language :: Python Programming Language :: Python :: 3 :: Only - Programming Language :: Python :: 3.6 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.1 Framework :: Django :: 3.2 + Framework :: Django :: 4 Framework :: Wagtail - Framework :: Wagtail :: 2 + Framework :: Wagtail :: 3 + Framework :: Wagtail :: 4 Topic :: Internet :: WWW/HTTP Topic :: Internet :: WWW/HTTP :: Dynamic Content Topic :: Software Development :: Libraries :: Python Modules @@ -37,8 +36,8 @@ zip_safe = False include_package_data = True packages = find: install_requires = - django-meta>1.7.0 - wagtail>1.4,<3.0 + django-meta>=2.1.0 + wagtail>=3.0 [options.packages.find] exclude = example* diff --git a/wagtailmetadata/models.py b/wagtailmetadata/models.py index 0548d08..12cfd8d 100644 --- a/wagtailmetadata/models.py +++ b/wagtailmetadata/models.py @@ -6,9 +6,9 @@ from meta import settings as meta_settings from meta import utils from meta.models import ModelMeta -from wagtail.core.models import Site +from wagtail.admin.panels import FieldPanel +from wagtail.models import Site from wagtail.images import get_image_model_string -from wagtail.images.edit_handlers import ImageChooserPanel class MetadataMixin(ModelMeta): @@ -84,16 +84,6 @@ def get_meta_schemaorg_type(self): def get_meta_site_name(self): request = utils.get_request() - site = getattr(request, "site", None) - if request and isinstance(site, Site): - if bool(request.site.site_name) is True: - return request.site.site_name - - site = self.get_site() - if isinstance(site, Site): - if bool(site.site_name) is True: - return site.site_name - if request: site = Site.find_for_request(request) if isinstance(site, Site): @@ -120,12 +110,9 @@ def get_meta_custom_namespace(self): def get_domain(self): request = utils.get_request() - if request and getattr(request, "site", None): - return request.site.hostname - - site = self.get_site() - if site is not None: - if bool(site.hostname) is True: + if request: + site = Site.find_for_request(request) + if isinstance(site, Site): return site.hostname if not meta_settings.SITE_DOMAIN: @@ -175,7 +162,7 @@ class MetadataPageMixin(MetadataMixin, models.Model): related_name="+", ) - panels = [ImageChooserPanel("search_image")] + panels = [FieldPanel("search_image")] _metadata = { "published_time": "published_time", @@ -205,7 +192,9 @@ def get_meta_url(self): def get_meta_image(self): if self.search_image is not None: return self.build_absolute_uri( - self.search_image.get_rendition(getattr(settings, "META_SEARCH_IMAGE_RENDITION", "fill-800x450")).url + self.search_image.get_rendition( + getattr(settings, "META_SEARCH_IMAGE_RENDITION", "fill-800x450") + ).url ) return super().get_meta_image() diff --git a/wagtailmetadata/tests/models.py b/wagtailmetadata/tests/models.py index 05241e4..b41d101 100644 --- a/wagtailmetadata/tests/models.py +++ b/wagtailmetadata/tests/models.py @@ -1,4 +1,4 @@ -from wagtail.core.models import Page +from wagtail.models import Page from wagtailmetadata.models import MetadataPageMixin diff --git a/wagtailmetadata/tests/settings.py b/wagtailmetadata/tests/settings.py index 3ae246c..2748b01 100644 --- a/wagtailmetadata/tests/settings.py +++ b/wagtailmetadata/tests/settings.py @@ -20,7 +20,7 @@ "django.contrib.staticfiles", "taggit", "modelcluster", - "wagtail.core", + "wagtail", "wagtail.users", "wagtail.images", "wagtail.admin", @@ -34,7 +34,6 @@ "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", - "wagtail.core.middleware.SiteMiddleware", "wagtail.contrib.redirects.middleware.RedirectMiddleware", ] diff --git a/wagtailmetadata/tests/test_mixin.py b/wagtailmetadata/tests/test_mixin.py index 3f87128..c3e0f07 100644 --- a/wagtailmetadata/tests/test_mixin.py +++ b/wagtailmetadata/tests/test_mixin.py @@ -2,9 +2,9 @@ from django.test import TestCase from django.utils import timezone from meta import settings as meta_settings -from wagtail.core.models import Site from wagtail.images.models import Image from wagtail.images.tests.utils import get_test_image_file +from wagtail.models import Site from wagtailmetadata.tests.models import SimplePage @@ -53,7 +53,8 @@ def test_get_meta_image(self): self.page.search_image = self.image self.assertEqual( - self.page.get_meta_image(), self.page.build_absolute_uri(self.image.get_rendition("fill-800x450").url) + self.page.get_meta_image(), + self.page.build_absolute_uri(self.image.get_rendition("fill-800x450").url) ) def test_get_meta_image_with_settings(self): diff --git a/wagtailmetadata/tests/urls.py b/wagtailmetadata/tests/urls.py index e610648..ead976f 100644 --- a/wagtailmetadata/tests/urls.py +++ b/wagtailmetadata/tests/urls.py @@ -1,4 +1,4 @@ from django.urls import include, path -from wagtail.core import urls as wagtail_urls +from wagtail import urls as wagtail_urls urlpatterns = [path("", include(wagtail_urls))]