Skip to content

Commit 10f49bc

Browse files
authored
feat: Add migration for djangocms-text-ckeditor fields (#13)
1 parent 1731774 commit 10f49bc

File tree

6 files changed

+63
-15
lines changed

6 files changed

+63
-15
lines changed

.github/workflows/lint.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ jobs:
1717
run: pip install --upgrade ruff
1818
- name: Run ruff
1919
run: |
20-
ruff djangocms_text
21-
ruff djangocms_text_ckeditor
22-
ruff tests
20+
ruff check djangocms_text
21+
ruff check djangocms_text_ckeditor
22+
ruff check tests

djangocms_text/cms_plugins.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
HttpResponse,
1515
HttpResponseBadRequest,
1616
HttpResponseForbidden,
17-
HttpResponseRedirect,
17+
HttpResponseRedirect, JsonResponse,
1818
)
1919
from django.shortcuts import get_object_or_404
2020
from django.template import RequestContext
@@ -545,14 +545,14 @@ def get_available_urls(self, request):
545545
"text": " " * (0 if search else len(page_content.page.node.path) // 4 - 1)
546546
+ page_content.title,
547547
"url": page_content.get_absolute_url(),
548-
"value": f"cms.page:{page_content.page.pk}",
548+
"id": f"cms.page:{page_content.page.pk}",
549549
"verbose": page_content.title,
550550
} for page_content in qs
551551
]
552552
}
553553
]
554554
}
555-
return HttpResponse(json.dumps(urls))
555+
return JsonResponse(urls)
556556

557557
@classmethod
558558
def get_child_plugin_candidates(cls, slot, page):

djangocms_text/html.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def __call__(self) -> dict[str, Union[dict[str, set[str]], set[str], None]]:
9090

9191
def clean_html(data: str, full: bool = False, cleaner: NH3Parser = cms_parser) -> str:
9292
"""
93-
Cleans HTML from XSS vulnerabilities using lxml
93+
Cleans HTML from XSS vulnerabilities using nh3
9494
If full is False, only the contents inside <body> will be returned (without
9595
the <body> tags).
9696
"""
@@ -185,9 +185,10 @@ def dynamic_href(elem: Element, obj: models.Model, attr: str) -> None:
185185

186186
def dynamic_src(elem: Element, obj: models.Model, attr: str) -> None:
187187
"""
188-
This method modifies the provided element by setting the value of the specified attribute based on the provided object.
189-
If the object has a "get_absolute_url" method and it returns a non-empty value, the attribute of the element will be set to the URL returned by the method.
190-
Otherwise, the "data-cms-error" attribute of the XML element will be set to "ref-not-found".
188+
This method modifies the provided element by setting the value of the specified attribute based on the provided
189+
object. If the object has a "get_absolute_url" method, and it returns a non-empty value, the attribute of the
190+
element will be set to the URL returned by the method. Otherwise, the "data-cms-error" attribute of the XML
191+
element will be set to "ref-not-found".
191192
192193
:param elem: The XML element to modify.
193194
:type elem: Element
@@ -217,7 +218,8 @@ def render_dynamic_attributes(
217218
Parameters:
218219
- dyn_html (str): The HTML content with dynamic attributes
219220
- admin_objects (bool) (optional): Flag to indicate whether to fetch data from admin objects (default: False)
220-
- remove_attr (bool) (optional): Flag to indicate whether to remove dynamic attributes from the final HTML (default: True)
221+
- remove_attr (bool) (optional): Flag to indicate whether to remove dynamic attributes from the final HTML
222+
(default: True)
221223
222224
Returns:
223225
- str: The updated HTML content with dynamic attributes
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Generated by Django 3.2.25 on 2024-07-02 14:09
2+
3+
from django.db import migrations, models, OperationalError
4+
5+
6+
def migrate_text_ckeditor_fields(apps, schema_editor):
7+
class CKEditorText(models.Model):
8+
class Meta:
9+
managed = False
10+
db_table = "djangocms_text_ckeditor_text"
11+
cmsplugin_ptr_id = models.PositiveIntegerField(primary_key=True)
12+
body = models.TextField()
13+
14+
class Text_Text(models.Model): # Name must not be used elsewhere as model
15+
class Meta:
16+
managed = False
17+
db_table = "djangocms_text_text"
18+
cmsplugin_ptr_id = models.PositiveIntegerField(primary_key=True)
19+
body = models.TextField()
20+
json = models.JSONField(blank=True, null=True)
21+
rte = models.CharField(max_length=16, blank=True)
22+
23+
try:
24+
existing_texts = Text_Text.objects.all().values_list("cmsplugin_ptr_id", flat=True)
25+
qs = CKEditorText.objects.using(schema_editor.connection.alias).exclude(cmsplugin_ptr_id__in=existing_texts)
26+
Text_Text.objects.using(schema_editor.connection.alias).bulk_create(
27+
Text_Text(body=ckeditor_text.body, rte="text_ckeditor4", cmsplugin_ptr_id=ckeditor_text.cmsplugin_ptr_id)
28+
for ckeditor_text in qs
29+
)
30+
except OperationalError as e:
31+
if "no such table" in str(e):
32+
return
33+
raise e
34+
35+
36+
class Migration(migrations.Migration):
37+
38+
dependencies = [
39+
('djangocms_text', '0002_text_json_text_rte'),
40+
]
41+
42+
operations = [
43+
migrations.RunPython(
44+
code=migrate_text_ckeditor_fields,
45+
)
46+
]

djangocms_text/widgets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Media:
3535
}
3636
js = (
3737
static_with_version("cms/js/dist/bundle.admin.base.min.js"),
38-
static("djangocms_text/bundles/bundle.editor.min.js"),
38+
"djangocms_text/bundles/bundle.editor.min.js",
3939
*(static(js) for js in rte_config.js),
4040
)
4141

@@ -72,7 +72,7 @@ def __init__(
7272

7373
super().__init__(attrs)
7474

75-
self.installed_plugins = installed_plugins # general
75+
self.installed_plugins = installed_plugins or [] # general
7676
self.pk = pk # specific
7777
self.placeholder = (
7878
placeholder.pk if isinstance(placeholder, models.Model) else placeholder

private/js/cms.linkfield.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ class LinkField {
8686
_addResult(result) {
8787
const item = document.createElement('div');
8888
item.textContent = result.text;
89-
if (result.value) {
89+
if (result.id) {
9090
item.classList.add('cms-linkfield-option');
91-
item.setAttribute('data-value', result.value);
91+
item.setAttribute('data-value', result.id);
9292
item.setAttribute('data-href', result.url);
9393
item.setAttribute('data-text', result.verbose);
9494
item.addEventListener('click', this.handleSelection.bind(this));

0 commit comments

Comments
 (0)