From f35e83ae7b2d70ba845976d7fd6575a8bdf55c42 Mon Sep 17 00:00:00 2001 From: Frank Date: Tue, 18 May 2021 11:04:52 -0500 Subject: [PATCH] =?UTF-8?q?Fixed=20#3734=20--=20Automated=20check=20that?= =?UTF-8?q?=20PlaceholderFields=20use=20Placeholder=E2=80=A6=20(#6380)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: Fixed #3734: Automated check that PlaceholderFields use PlaceholderAdminMixin Co-authored-by: Angelo Dini Co-authored-by: Vinit Kumar Co-authored-by: Florian Delizy --- cms/tests/test_check.py | 14 ++++++++++++++ cms/utils/check.py | 25 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/cms/tests/test_check.py b/cms/tests/test_check.py index 0305d9dc546..715aa13a4ae 100644 --- a/cms/tests/test_check.py +++ b/cms/tests/test_check.py @@ -3,6 +3,7 @@ from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.test import TestCase +from django.test.utils import isolate_apps from cms.api import add_plugin from cms.models.pluginmodel import CMSPlugin @@ -117,6 +118,19 @@ def test_non_numeric_site_id(self): with self.settings(SITE_ID='broken'): self.assertCheck(False, warnings=0, errors=1) + @isolate_apps("test_app") + def test_placeholder_field(self): + from django.db import models + from django.contrib import admin + from cms.models.fields import PlaceholderField + + class ModelTest(models.Model): + field_a = PlaceholderField(slotname="test") + + admin.site.register(ModelTest) + self.assertCheck(False, warnings=0, errors=1) + admin.site.unregister(ModelTest) + class CheckWithDatabaseTests(CheckAssertMixin, TestCase): diff --git a/cms/utils/check.py b/cms/utils/check.py index 708d09c4204..b58f9c56326 100644 --- a/cms/utils/check.py +++ b/cms/utils/check.py @@ -339,6 +339,31 @@ def get_class(method_name, model): 'https://django-cms.readthedocs.io/en/latest/extending_cms/extending_page_title.html#handling-relations.') # noqa +@define_check +def check_placeholder_fields(output): + """ + ModelAdmin instances that are using PlaceholderField fields + should be also a subclass of PlaceholderAdminMixin + """ + from django.contrib.admin import site + from cms.models.fields import PlaceholderField + from cms.admin.placeholderadmin import PlaceholderAdminMixin + + with output.section("PlaceholderField") as section: + for model, model_admin in site._registry.items(): + ph_fields = [field for field in model._meta.get_fields() if isinstance(field, PlaceholderField)] + if len(ph_fields) == 0: + continue + + if not isinstance(model_admin, PlaceholderAdminMixin): + section.error( + "%s does not subclass of PlaceholderAdminMixin" % model_admin + ) + + if section.successful: + section.finish_success("PlaceholderField configuration okay") + + def check(output): """ Checks the configuration/environment of this django CMS installation.