Skip to content

Commit

Permalink
Add support for using the prose editor form field with widget instances
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Jul 30, 2024
1 parent 46c2c08 commit 3f85f36
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions django_prose_editor/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 24 additions & 2 deletions tests/testapp/admin.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions tests/testapp/test_prose_editor.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -35,3 +37,15 @@ def test_sanitized_field(self):
m = SanitizedProseEditorModel(description="<p>hello</p>")
m.full_clean()
self.assertEqual(m.description, "<p>hello</p>")

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"'
)

0 comments on commit 3f85f36

Please sign in to comment.