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

feat: Remove explicit django CMS dependency #36

Merged
merged 13 commits into from
Dec 10, 2024
19 changes: 19 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,25 @@ rendering the above settings useless.
To completely disable the feature, set ``TEXT_HTML_SANITIZE = False``.


Usage outside django CMS
------------------------

django CMS Text can be used without django CMS installed. Without django CMS it
offers the ``HTMLField`` and ``HTMLFormField`` classes which can be used by any
Django model or form.

If django CMS is not installed with django CMS Text, add the following to your
``MIGRATION_MODULES`` setting::

MIGRATION_MODULES = [
fsbraun marked this conversation as resolved.
Show resolved Hide resolved
...,
"djangocms_text": None,
...
]

This will prevent the migration of the models for django CMS plugins.


Development
===========

Expand Down
2 changes: 1 addition & 1 deletion djangocms_text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
10. Github actions will publish the new package to pypi
"""

__version__ = "0.3.3"
__version__ = "0.4.0"
47 changes: 33 additions & 14 deletions djangocms_text/apps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.apps import AppConfig
from django.core.checks import Warning, register
from django.apps import AppConfig, apps
from django.core.checks import Error, Warning, register


class TextConfig(AppConfig):
Expand All @@ -10,6 +10,7 @@
def ready(self):
self.inline_models = discover_inline_editable_models()
register(check_ckeditor_settings)
register(check_no_cms_config)


def discover_inline_editable_models():
Expand Down Expand Up @@ -37,19 +38,21 @@
field_instance.__class__.__name__
)

from cms.plugin_pool import plugin_pool
if apps.is_installed("cms"):
# also check the plugins
from cms.plugin_pool import plugin_pool

for plugin in plugin_pool.plugins.values():
model = plugin.model
if model._meta.app_label in blacklist_apps:
continue
for field_name in getattr(plugin, "frontend_editable_fields", []):
form = plugin.form
field_instance = form.base_fields.get(field_name, None)
if field_instance.__class__.__name__ in registered_inline_fields:
inline_models[f"{model._meta.app_label}-{model._meta.model_name}-{field_name}"] = (
field_instance.__class__.__name__
)
for plugin in plugin_pool.plugins.values():
model = plugin.model
if model._meta.app_label in blacklist_apps:
continue

Check warning on line 48 in djangocms_text/apps.py

View check run for this annotation

Codecov / codecov/patch

djangocms_text/apps.py#L48

Added line #L48 was not covered by tests
for field_name in getattr(plugin, "frontend_editable_fields", []):
form = plugin.form
field_instance = form.base_fields.get(field_name, None)

Check warning on line 51 in djangocms_text/apps.py

View check run for this annotation

Codecov / codecov/patch

djangocms_text/apps.py#L50-L51

Added lines #L50 - L51 were not covered by tests
if field_instance.__class__.__name__ in registered_inline_fields:
inline_models[f"{model._meta.app_label}-{model._meta.model_name}-{field_name}"] = (

Check warning on line 53 in djangocms_text/apps.py

View check run for this annotation

Codecov / codecov/patch

djangocms_text/apps.py#L53

Added line #L53 was not covered by tests
field_instance.__class__.__name__
)

return inline_models

Expand Down Expand Up @@ -84,3 +87,19 @@
)

return warnings


def check_no_cms_config(app_configs, **kwargs):
from django.conf import settings

Check warning on line 93 in djangocms_text/apps.py

View check run for this annotation

Codecov / codecov/patch

djangocms_text/apps.py#L93

Added line #L93 was not covered by tests

if "cms" not in settings.INSTALLED_APPS:
migration_modules = getattr(settings, "MIGRATION_MODULES", {})

Check warning on line 96 in djangocms_text/apps.py

View check run for this annotation

Codecov / codecov/patch

djangocms_text/apps.py#L96

Added line #L96 was not covered by tests
if "djangocms_text" not in migration_modules or migration_modules["djangocms_text"] is not None:
return [

Check warning on line 98 in djangocms_text/apps.py

View check run for this annotation

Codecov / codecov/patch

djangocms_text/apps.py#L98

Added line #L98 was not covered by tests
Error(
"When using djangocms-text outside django-cms, deactivate migrations for it.",
hint="Add \"'djangocms_text': None\" to your MIGRATION_MODULES setting.",
id="djangocms_text.E001",
)
]
return []

Check warning on line 105 in djangocms_text/apps.py

View check run for this annotation

Codecov / codecov/patch

djangocms_text/apps.py#L105

Added line #L105 was not covered by tests
6 changes: 3 additions & 3 deletions djangocms_text/cms_toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _

from cms.cms_toolbars import CMSToolbar
from cms.toolbar.items import Button, ButtonList, TemplateItem
from cms.toolbar_base import CMSToolbar
from cms.toolbar_pool import toolbar_pool

from . import settings
from .utils import get_cancel_url, get_messages_url, get_render_plugin_url, get_url_endpoint
from .widgets import TextEditorWidget, rte_config
from .utils import get_cancel_url, get_messages_url, get_render_plugin_url
from .widgets import TextEditorWidget, get_url_endpoint, rte_config


class IconButton(Button):
Expand Down
3 changes: 1 addition & 2 deletions djangocms_text/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.utils.safestring import mark_safe

from .html import clean_html, render_dynamic_attributes
from .utils import get_url_endpoint
from .widgets import TextEditorWidget


Expand Down Expand Up @@ -66,7 +65,7 @@ def formfield(self, **kwargs):
# override the admin widget
if defaults["widget"] == admin_widgets.AdminTextareaWidget:
# In the admin the URL endpoint is available
defaults["widget"] = TextEditorWidget(configuration=self.configuration, url_endpoint=get_url_endpoint())
defaults["widget"] = TextEditorWidget(configuration=self.configuration)
return super().formfield(**defaults)

def clean(self, value, model_instance):
Expand Down
Loading
Loading