Skip to content

Commit

Permalink
feat: Add migration for djangocms-text-ckeditor fields (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun authored Jul 24, 2024
1 parent 1731774 commit 10f49bc
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ jobs:
run: pip install --upgrade ruff
- name: Run ruff
run: |
ruff djangocms_text
ruff djangocms_text_ckeditor
ruff tests
ruff check djangocms_text
ruff check djangocms_text_ckeditor
ruff check tests
6 changes: 3 additions & 3 deletions djangocms_text/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
HttpResponse,
HttpResponseBadRequest,
HttpResponseForbidden,
HttpResponseRedirect,
HttpResponseRedirect, JsonResponse,
)
from django.shortcuts import get_object_or_404
from django.template import RequestContext
Expand Down Expand Up @@ -545,14 +545,14 @@ def get_available_urls(self, request):
"text": " " * (0 if search else len(page_content.page.node.path) // 4 - 1)
+ page_content.title,
"url": page_content.get_absolute_url(),
"value": f"cms.page:{page_content.page.pk}",
"id": f"cms.page:{page_content.page.pk}",
"verbose": page_content.title,
} for page_content in qs
]
}
]
}
return HttpResponse(json.dumps(urls))
return JsonResponse(urls)

@classmethod
def get_child_plugin_candidates(cls, slot, page):
Expand Down
12 changes: 7 additions & 5 deletions djangocms_text/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __call__(self) -> dict[str, Union[dict[str, set[str]], set[str], None]]:

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

def dynamic_src(elem: Element, obj: models.Model, attr: str) -> None:
"""
This method modifies the provided element by setting the value of the specified attribute based on the provided object.
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.
Otherwise, the "data-cms-error" attribute of the XML element will be set to "ref-not-found".
This method modifies the provided element by setting the value of the specified attribute based on the provided
object. 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. Otherwise, the "data-cms-error" attribute of the XML
element will be set to "ref-not-found".
:param elem: The XML element to modify.
:type elem: Element
Expand Down Expand Up @@ -217,7 +218,8 @@ def render_dynamic_attributes(
Parameters:
- dyn_html (str): The HTML content with dynamic attributes
- admin_objects (bool) (optional): Flag to indicate whether to fetch data from admin objects (default: False)
- remove_attr (bool) (optional): Flag to indicate whether to remove dynamic attributes from the final HTML (default: True)
- remove_attr (bool) (optional): Flag to indicate whether to remove dynamic attributes from the final HTML
(default: True)
Returns:
- str: The updated HTML content with dynamic attributes
Expand Down
46 changes: 46 additions & 0 deletions djangocms_text/migrations/0003_auto_20240702_1409.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by Django 3.2.25 on 2024-07-02 14:09

from django.db import migrations, models, OperationalError


def migrate_text_ckeditor_fields(apps, schema_editor):
class CKEditorText(models.Model):
class Meta:
managed = False
db_table = "djangocms_text_ckeditor_text"
cmsplugin_ptr_id = models.PositiveIntegerField(primary_key=True)
body = models.TextField()

class Text_Text(models.Model): # Name must not be used elsewhere as model
class Meta:
managed = False
db_table = "djangocms_text_text"
cmsplugin_ptr_id = models.PositiveIntegerField(primary_key=True)
body = models.TextField()
json = models.JSONField(blank=True, null=True)
rte = models.CharField(max_length=16, blank=True)

try:
existing_texts = Text_Text.objects.all().values_list("cmsplugin_ptr_id", flat=True)
qs = CKEditorText.objects.using(schema_editor.connection.alias).exclude(cmsplugin_ptr_id__in=existing_texts)
Text_Text.objects.using(schema_editor.connection.alias).bulk_create(
Text_Text(body=ckeditor_text.body, rte="text_ckeditor4", cmsplugin_ptr_id=ckeditor_text.cmsplugin_ptr_id)
for ckeditor_text in qs
)
except OperationalError as e:
if "no such table" in str(e):
return
raise e


class Migration(migrations.Migration):

dependencies = [
('djangocms_text', '0002_text_json_text_rte'),
]

operations = [
migrations.RunPython(
code=migrate_text_ckeditor_fields,
)
]
4 changes: 2 additions & 2 deletions djangocms_text/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Media:
}
js = (
static_with_version("cms/js/dist/bundle.admin.base.min.js"),
static("djangocms_text/bundles/bundle.editor.min.js"),
"djangocms_text/bundles/bundle.editor.min.js",
*(static(js) for js in rte_config.js),
)

Expand Down Expand Up @@ -72,7 +72,7 @@ def __init__(

super().__init__(attrs)

self.installed_plugins = installed_plugins # general
self.installed_plugins = installed_plugins or [] # general
self.pk = pk # specific
self.placeholder = (
placeholder.pk if isinstance(placeholder, models.Model) else placeholder
Expand Down
4 changes: 2 additions & 2 deletions private/js/cms.linkfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ class LinkField {
_addResult(result) {
const item = document.createElement('div');
item.textContent = result.text;
if (result.value) {
if (result.id) {
item.classList.add('cms-linkfield-option');
item.setAttribute('data-value', result.value);
item.setAttribute('data-value', result.id);
item.setAttribute('data-href', result.url);
item.setAttribute('data-text', result.verbose);
item.addEventListener('click', this.handleSelection.bind(this));
Expand Down

0 comments on commit 10f49bc

Please sign in to comment.