diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 95bdace5..0c983a2a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,4 +33,4 @@ jobs: run: coverage run -m pytest - name: Upload Coverage to Codecov - uses: codecov/codecov-action@v4 + uses: codecov/codecov-action@v5 diff --git a/djangocms_text/apps.py b/djangocms_text/apps.py index 037ea121..1435a35e 100644 --- a/djangocms_text/apps.py +++ b/djangocms_text/apps.py @@ -27,9 +27,7 @@ def discover_inline_editable_models(): for field_name in getattr(modeladmin, "frontend_editable_fields", []): try: - form = modeladmin.get_form( - request=None, fields=(field_name,) - ) # Worth a try + form = modeladmin.get_form(request=None, fields=(field_name,)) # Worth a try except Exception: form = getattr(modeladmin, "form", None) if form: diff --git a/djangocms_text/html.py b/djangocms_text/html.py index 472115d8..22d6fd4c 100644 --- a/djangocms_text/html.py +++ b/djangocms_text/html.py @@ -34,6 +34,7 @@ class NH3Parser: Methods: - __init__: Initializes the NH3Parser object. """ + def __init__( self, additional_attributes: Optional[dict[str, set[str]]] = None, @@ -49,9 +50,7 @@ def __init__( if additional_attributes: self.ALLOWED_TAGS |= set(additional_attributes.keys()) for tag, attributes in additional_attributes.items(): - self.ALLOWED_ATTRIBUTES[tag] = ( - self.ALLOWED_ATTRIBUTES.get(tag, set()) | attributes - ) + self.ALLOWED_ATTRIBUTES[tag] = self.ALLOWED_ATTRIBUTES.get(tag, set()) | attributes def __call__(self) -> dict[str, Union[dict[str, set[str]], set[str], None]]: """ @@ -80,7 +79,7 @@ def __call__(self) -> dict[str, Union[dict[str, set[str]], set[str], None]]: additional_attributes={ "a": {"href", "target", "rel"}, "cms-plugin": {"id", "title", "name", "alt", "render-plugin", "type"}, - "*": {"style", "class"} + "*": {"style", "class"}, }, generic_attribute_prefixes={"data-"}, ) @@ -208,9 +207,7 @@ def dynamic_src(elem: Element, obj: models.Model, attr: str) -> None: elem.attrib["data-cms-error"] = "ref-not-found" -def render_dynamic_attributes( - dyn_html: str, admin_objects: bool = False, remove_attr=True -) -> str: +def render_dynamic_attributes(dyn_html: str, admin_objects: bool = False, remove_attr=True) -> str: """ Render method to update dynamic attributes in HTML @@ -239,7 +236,7 @@ def render_dynamic_attributes( for elem in tree.xpath(xpath): for attr, value in elem.attrib.items(): - if attr.startswith(prefix): + if attr in dynamic_attr_pool: try: model, pk = value.rsplit(":", 1) if model.strip() in req_model_obj: @@ -252,8 +249,8 @@ def render_dynamic_attributes( from_db = get_data_from_db(req_model_obj, admin_objects=admin_objects) for elem in update_queue: for attr, value in elem.attrib.items(): - if attr.startswith(prefix): - target_attr = attr[len(prefix):] + if attr in dynamic_attr_pool: + target_attr = attr[len(prefix) :] try: model, pk = value.rsplit(":", 1) obj = from_db[model.strip()][int(pk.strip())] @@ -307,9 +304,7 @@ def extract_images(data, plugin): width = img.getAttribute("width") height = img.getAttribute("height") # extract the image data - data_re = re.compile( - r'data:(?P[^"]*);(?P[^"]*),(?P[^"]*)' - ) + data_re = re.compile(r'data:(?P[^"]*);(?P[^"]*),(?P[^"]*)') m = data_re.search(src) dr = m.groupdict() mime_type = dr["mime_type"] @@ -353,14 +348,10 @@ def extract_images(data, plugin): # render the new html for the plugin new_img_html = plugin_to_tag(image_plugin) # replace the original image node with the newly created cms plugin html - img.parentNode.replaceChild( - parser.parseFragment(new_img_html).childNodes[0], img - ) + img.parentNode.replaceChild(parser.parseFragment(new_img_html).childNodes[0], img) found = True if found: - return "".join( - [y.toxml() for y in dom.getElementsByTagName("body")[0].childNodes] - ) + return "".join([y.toxml() for y in dom.getElementsByTagName("body")[0].childNodes]) else: return data diff --git a/djangocms_text/migrations/0003_auto_20240702_1409.py b/djangocms_text/migrations/0003_auto_20240702_1409.py index f4e38d50..19787118 100644 --- a/djangocms_text/migrations/0003_auto_20240702_1409.py +++ b/djangocms_text/migrations/0003_auto_20240702_1409.py @@ -8,6 +8,7 @@ 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() @@ -15,6 +16,7 @@ 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) @@ -30,9 +32,8 @@ class Meta: class Migration(migrations.Migration): - dependencies = [ - ('djangocms_text', '0002_text_json_text_rte'), + ("djangocms_text", "0002_text_json_text_rte"), ] operations = [ diff --git a/djangocms_text/models.py b/djangocms_text/models.py index c3207000..9530a927 100644 --- a/djangocms_text/models.py +++ b/djangocms_text/models.py @@ -63,9 +63,7 @@ class Meta: abstract = True def __str__(self): - return Truncator(strip_tags(self.body).replace("­", "")).words( - 3, truncate="..." - ) + return Truncator(strip_tags(self.body).replace("­", "")).words(3, truncate="...") def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)