From 2ad7eb25666526163945aac9722b902e89553140 Mon Sep 17 00:00:00 2001
From: Fabian Braun <fsbraun@gmx.de>
Date: Wed, 11 Sep 2024 18:30:15 +0200
Subject: [PATCH] Decouple from djangocms_text_ckeditor

---
 djangocms_blog/migrations/0001_initial.py       |  6 +++---
 .../0001_squashed_0047_migrate_config.py        |  7 +++----
 .../migrations/0006_auto_20150214_1907.py       |  5 +++--
 .../migrations/0007_auto_20150719_0933.py       |  7 +++----
 .../migrations/0008_auto_20150814_0831.py       |  4 ++--
 .../migrations/0040_auto_20211128_1503.py       |  5 +++--
 djangocms_blog/migrations/0043_postcontent.py   |  5 ++---
 djangocms_blog/models.py                        | 17 ++++++++++++++++-
 8 files changed, 35 insertions(+), 21 deletions(-)

diff --git a/djangocms_blog/migrations/0001_initial.py b/djangocms_blog/migrations/0001_initial.py
index e9bd088a..f0174687 100644
--- a/djangocms_blog/migrations/0001_initial.py
+++ b/djangocms_blog/migrations/0001_initial.py
@@ -1,6 +1,5 @@
 import cms.models.fields
 import django.utils.timezone
-import djangocms_text_ckeditor.fields
 import filer.fields.image
 import meta.models
 import taggit_autosuggest.managers
@@ -8,6 +7,7 @@
 from django.db import migrations, models
 from filer.settings import FILER_IMAGE_MODEL
 
+from djangocms_blog.models import HTMLField
 from djangocms_blog.models import thumbnail_model
 
 ACTUAL_FILER_IMAGE_MODEL = FILER_IMAGE_MODEL or "filer.Image"
@@ -243,7 +243,7 @@ class Migration(migrations.Migration):
                 ),
                 ("title", models.CharField(max_length=255, verbose_name="Title")),
                 ("slug", models.SlugField(verbose_name="slug", blank=True)),
-                ("abstract", djangocms_text_ckeditor.fields.HTMLField(verbose_name="Abstract")),
+                ("abstract", HTMLField(verbose_name="Abstract")),
                 ("meta_description", models.TextField(default="", verbose_name="Post meta description", blank=True)),
                 ("meta_keywords", models.TextField(default="", verbose_name="Post meta keywords", blank=True)),
                 (
@@ -256,7 +256,7 @@ class Migration(migrations.Migration):
                         blank=True,
                     ),
                 ),
-                ("post_text", djangocms_text_ckeditor.fields.HTMLField(default="", verbose_name="Text", blank=True)),
+                ("post_text", HTMLField(default="", verbose_name="Text", blank=True)),
                 (
                     "master",
                     models.ForeignKey(
diff --git a/djangocms_blog/migrations/0001_squashed_0047_migrate_config.py b/djangocms_blog/migrations/0001_squashed_0047_migrate_config.py
index 7ee597fd..be5ea154 100644
--- a/djangocms_blog/migrations/0001_squashed_0047_migrate_config.py
+++ b/djangocms_blog/migrations/0001_squashed_0047_migrate_config.py
@@ -2,7 +2,6 @@
 
 import django.db.models.deletion
 import django.db.models.expressions
-import djangocms_text_ckeditor.fields
 import filer.fields.image
 import meta.models
 import parler.fields
@@ -642,7 +641,7 @@ class Migration(migrations.Migration):
                 ),
                 (
                     "abstract",
-                    djangocms_text_ckeditor.fields.HTMLField(blank=True, default="", verbose_name="abstract"),
+                    djangocms_blog.models.HTMLField(blank=True, default="", verbose_name="abstract"),
                 ),
             ],
             options={
@@ -690,7 +689,7 @@ class Migration(migrations.Migration):
                 ("subtitle", models.CharField(blank=True, default="", max_length=767, verbose_name="subtitle")),
                 (
                     "abstract",
-                    djangocms_text_ckeditor.fields.HTMLField(blank=True, default="", verbose_name="abstract"),
+                    djangocms_blog.models.HTMLField(blank=True, default="", verbose_name="abstract"),
                 ),
                 ("meta_description", models.TextField(blank=True, default="", verbose_name="post meta description")),
                 ("meta_keywords", models.TextField(blank=True, default="", verbose_name="post meta keywords")),
@@ -704,7 +703,7 @@ class Migration(migrations.Migration):
                         verbose_name="post meta title",
                     ),
                 ),
-                ("post_text", djangocms_text_ckeditor.fields.HTMLField(blank=True, default="", verbose_name="text")),
+                ("post_text", djangocms_blog.models.HTMLField(blank=True, default="", verbose_name="text")),
                 ("post", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="djangocms_blog.post")),
             ],
             options={
diff --git a/djangocms_blog/migrations/0006_auto_20150214_1907.py b/djangocms_blog/migrations/0006_auto_20150214_1907.py
index 98906b61..940ba6eb 100644
--- a/djangocms_blog/migrations/0006_auto_20150214_1907.py
+++ b/djangocms_blog/migrations/0006_auto_20150214_1907.py
@@ -1,6 +1,7 @@
-import djangocms_text_ckeditor.fields
 from django.db import migrations
 
+from djangocms_blog.models import HTMLField
+
 
 class Migration(migrations.Migration):
     dependencies = [
@@ -11,7 +12,7 @@ class Migration(migrations.Migration):
         migrations.AlterField(
             model_name="posttranslation",
             name="abstract",
-            field=djangocms_text_ckeditor.fields.HTMLField(verbose_name="Abstract", blank=True, default=""),
+            field=HTMLField(verbose_name="Abstract", blank=True, default=""),
             preserve_default=True,
         ),
     ]
diff --git a/djangocms_blog/migrations/0007_auto_20150719_0933.py b/djangocms_blog/migrations/0007_auto_20150719_0933.py
index 3bcd5f3a..3b597b5b 100644
--- a/djangocms_blog/migrations/0007_auto_20150719_0933.py
+++ b/djangocms_blog/migrations/0007_auto_20150719_0933.py
@@ -1,12 +1,11 @@
 import django.db.models.deletion
 import django.utils.timezone
-import djangocms_text_ckeditor.fields
 import filer.fields.image
 from django.conf import settings
 from django.db import migrations, models
 from filer.settings import FILER_IMAGE_MODEL
 
-from djangocms_blog.models import thumbnail_model
+from djangocms_blog.models import HTMLField, thumbnail_model
 
 ACTUAL_FILER_IMAGE_MODEL = FILER_IMAGE_MODEL or "filer.Image"
 
@@ -168,7 +167,7 @@ class Migration(migrations.Migration):
         migrations.AlterField(
             model_name="posttranslation",
             name="abstract",
-            field=djangocms_text_ckeditor.fields.HTMLField(verbose_name="abstract"),
+            field=HTMLField(verbose_name="abstract"),
             preserve_default=True,
         ),
         migrations.AlterField(
@@ -204,7 +203,7 @@ class Migration(migrations.Migration):
         migrations.AlterField(
             model_name="posttranslation",
             name="post_text",
-            field=djangocms_text_ckeditor.fields.HTMLField(blank=True, verbose_name="text", default=""),
+            field=HTMLField(blank=True, verbose_name="text", default=""),
             preserve_default=True,
         ),
         migrations.AlterField(
diff --git a/djangocms_blog/migrations/0008_auto_20150814_0831.py b/djangocms_blog/migrations/0008_auto_20150814_0831.py
index f2f9c74f..9abbc627 100644
--- a/djangocms_blog/migrations/0008_auto_20150814_0831.py
+++ b/djangocms_blog/migrations/0008_auto_20150814_0831.py
@@ -1,6 +1,6 @@
-import djangocms_text_ckeditor.fields
 from django.db import migrations
 
+import djangocms_blog.models
 
 class Migration(migrations.Migration):
     dependencies = [
@@ -11,7 +11,7 @@ class Migration(migrations.Migration):
         migrations.AlterField(
             model_name="posttranslation",
             name="abstract",
-            field=djangocms_text_ckeditor.fields.HTMLField(default=b"", verbose_name="abstract", blank=True),
+            field=djangocms_blog.models.HTMLField(default=b"", verbose_name="abstract", blank=True),
             preserve_default=True,
         ),
     ]
diff --git a/djangocms_blog/migrations/0040_auto_20211128_1503.py b/djangocms_blog/migrations/0040_auto_20211128_1503.py
index 2e99e664..6e082994 100644
--- a/djangocms_blog/migrations/0040_auto_20211128_1503.py
+++ b/djangocms_blog/migrations/0040_auto_20211128_1503.py
@@ -1,11 +1,12 @@
 # Generated by Django 3.0.14 on 2021-11-28 14:03
 
 import django.db.models.deletion
-import djangocms_text_ckeditor.fields
 import filer.fields.image
 from django.conf import settings
 from django.db import migrations, models
 
+import djangocms_blog.models
+
 
 class Migration(migrations.Migration):
     dependencies = [
@@ -57,7 +58,7 @@ class Migration(migrations.Migration):
         migrations.AddField(
             model_name="blogcategorytranslation",
             name="abstract",
-            field=djangocms_text_ckeditor.fields.HTMLField(blank=True, default="", verbose_name="abstract"),
+            field=djangocms_blog.models.HTMLField(blank=True, default="", verbose_name="abstract"),
         ),
         migrations.AddField(
             model_name="post",
diff --git a/djangocms_blog/migrations/0043_postcontent.py b/djangocms_blog/migrations/0043_postcontent.py
index 0e6b1aed..8725d195 100644
--- a/djangocms_blog/migrations/0043_postcontent.py
+++ b/djangocms_blog/migrations/0043_postcontent.py
@@ -1,7 +1,6 @@
 # Generated by Django 3.2 on 2023-02-03 12:15
 
 import django.db.models.deletion
-import djangocms_text_ckeditor.fields
 from django.db import migrations, models
 
 import djangocms_blog.models
@@ -58,7 +57,7 @@ class Migration(migrations.Migration):
                 ("subtitle", models.CharField(blank=True, default="", max_length=767, verbose_name="subtitle")),
                 (
                     "abstract",
-                    djangocms_text_ckeditor.fields.HTMLField(blank=True, default="", verbose_name="abstract"),
+                    djangocms_blog.models.HTMLField(blank=True, default="", verbose_name="abstract"),
                 ),
                 ("meta_description", models.TextField(blank=True, default="", verbose_name="post meta description")),
                 ("meta_keywords", models.TextField(blank=True, default="", verbose_name="post meta keywords")),
@@ -72,7 +71,7 @@ class Migration(migrations.Migration):
                         verbose_name="post meta title",
                     ),
                 ),
-                ("post_text", djangocms_text_ckeditor.fields.HTMLField(blank=True, default="", verbose_name="text")),
+                ("post_text", djangocms_blog.models.HTMLField(blank=True, default="", verbose_name="text")),
                 ("post", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="djangocms_blog.post")),
             ],
             bases=(djangocms_blog.models.BlogMetaMixin, models.Model),
diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py
index 1ee040f6..97e20e27 100644
--- a/djangocms_blog/models.py
+++ b/djangocms_blog/models.py
@@ -19,7 +19,6 @@
 from django.utils.html import strip_tags
 from django.utils.timezone import now
 from django.utils.translation import get_language, gettext, gettext_lazy as _
-from djangocms_text_ckeditor.fields import HTMLField
 from filer.fields.image import FilerImageField
 from filer.models import ThumbnailOption
 from meta.models import ModelMeta
@@ -52,6 +51,22 @@ class KnockerModel:
 
         pass
 
+# HTMLField is a custom field that allows to use a rich text editor
+# Probe for djangocms_text first, then for djangocms_text_ckeditor
+# and finally fallback to a simple textarea
+try:
+    from djangocms_text.fields import HTMLField
+except ImportError:  # pragma: no cover
+    try:
+        from djangocms_text_ckeditor.fields import HTMLField
+    except ImportError:
+        from django import forms
+
+        class HTMLField(models.TextField):
+            def __init__(self, *args, **kwargs):
+                kwargs.setdefault("widget", forms.Textarea)
+                super().__init__(*args, **kwargs)
+
 
 def _get_language(instance, language):
     available_languages = instance.get_available_languages()