From 1569241e091139f4f9764c26f22d5f7f6e21bd42 Mon Sep 17 00:00:00 2001 From: Matthias Kestenholz Date: Mon, 16 Dec 2024 15:52:45 +0100 Subject: [PATCH] Do not needlessly override the widget if it's set explicitly --- django_prose_editor/fields.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/django_prose_editor/fields.py b/django_prose_editor/fields.py index 10b37c9..ec03f9b 100644 --- a/django_prose_editor/fields.py +++ b/django_prose_editor/fields.py @@ -56,6 +56,14 @@ def formfield(self, **kwargs): return super().formfield(**defaults) +def _is(widget, widget_class): + return ( + issubclass(widget, widget_class) + if isinstance(widget, type) + else isinstance(widget, widget_class) + ) + + class ProseEditorFormField(forms.CharField): widget = ProseEditorWidget @@ -67,15 +75,9 @@ def __init__(self, *args, **kwargs): # 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) - ): + if widget and _is(widget, widgets.AdminTextareaWidget): kwargs["widget"] = AdminProseEditorWidget - else: + elif not widget or not _is(widget, ProseEditorWidget): kwargs["widget"] = ProseEditorWidget super().__init__(*args, **kwargs)