diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f580ddc..5b591eb 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,9 @@ Next version - Added the ``django-prose-editor[sanitize]`` which automatically installs the ``nh3`` dependency. - Properly restored the textarea element when destroying the editor. +- Added more unittesting. +- Supported using the ``ProseEditorFormField`` with widget instances, not just + with widget classes. 0.6 (2024-07-26) diff --git a/django_prose_editor/fields.py b/django_prose_editor/fields.py index 1309084..7d735c8 100644 --- a/django_prose_editor/fields.py +++ b/django_prose_editor/fields.py @@ -56,9 +56,17 @@ class ProseEditorFormField(forms.CharField): def __init__(self, *args, **kwargs): config = kwargs.pop("config", {}) - - if (widget := kwargs.get("widget")) and issubclass( - widget, widgets.AdminTextareaWidget + widget = kwargs.get("widget") + + # We don't know if widget is set, and if it is, we do not know if it is + # a class or an instance of the widget. The following if statement + # should take all possibilities into account. + if widget and ( + ( + isinstance(widget, type) + and issubclass(widget, widgets.AdminTextareaWidget) + ) + or isinstance(widget, widgets.AdminTextareaWidget) ): kwargs["widget"] = AdminProseEditorWidget else: diff --git a/tests/testapp/admin.py b/tests/testapp/admin.py index e7818bd..f11e8da 100644 --- a/tests/testapp/admin.py +++ b/tests/testapp/admin.py @@ -1,7 +1,29 @@ +from django import forms from django.contrib import admin +from django_prose_editor.widgets import ProseEditorWidget from testapp import models -admin.register(models.ProseEditorModel) -admin.register(models.SanitizedProseEditorModel) +@admin.register(models.ProseEditorModel) +class ProseEditorModelAdmin(admin.ModelAdmin): + pass + + +class SanitizedProseEditorModelForm(forms.ModelForm): + class Meta: + widgets = { + "description": ProseEditorWidget( + config={ + "types": ["paragraph", "strong", "em", "link", "heading"], + "history": False, + "html": False, + "typographic": True, + } + ) + } + + +@admin.register(models.SanitizedProseEditorModel) +class SanitizedProseEditorModelAdmin(admin.ModelAdmin): + form = SanitizedProseEditorModelForm diff --git a/tests/testapp/test_prose_editor.py b/tests/testapp/test_prose_editor.py index 70d8174..ce5581b 100644 --- a/tests/testapp/test_prose_editor.py +++ b/tests/testapp/test_prose_editor.py @@ -1,4 +1,6 @@ from django import test +from django.contrib.auth.models import User +from django.test import Client from testapp.models import ProseEditorModel, SanitizedProseEditorModel @@ -35,3 +37,15 @@ def test_sanitized_field(self): m = SanitizedProseEditorModel(description="

hello

") m.full_clean() self.assertEqual(m.description, "

hello

") + + def test_admin(self): + client = Client() + client.force_login( + User.objects.create_superuser("admin", "admin@example.com", "password") + ) + + response = client.get("/admin/testapp/proseeditormodel/add/") + # print(response, response.content.decode("utf-8")) + self.assertContains( + response, 'href="/static/django_prose_editor/overrides.css"' + )