Skip to content

Commit

Permalink
Merge branch 'main' into sourcery-ai/issue-27
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun authored Nov 26, 2024
2 parents 480bccb + badded7 commit 6234e7a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions djangocms_text/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 10 additions & 19 deletions djangocms_text/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class NH3Parser:
Methods:
- __init__: Initializes the NH3Parser object.
"""

def __init__(
self,
additional_attributes: Optional[dict[str, set[str]]] = None,
Expand All @@ -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]]:
"""
Expand Down Expand Up @@ -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-"},
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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())]
Expand Down Expand Up @@ -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<mime_type>[^"]*);(?P<encoding>[^"]*),(?P<data>[^"]*)'
)
data_re = re.compile(r'data:(?P<mime_type>[^"]*);(?P<encoding>[^"]*),(?P<data>[^"]*)')
m = data_re.search(src)
dr = m.groupdict()
mime_type = dr["mime_type"]
Expand Down Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions djangocms_text/migrations/0003_auto_20240702_1409.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ 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)
Expand All @@ -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 = [
Expand Down
4 changes: 1 addition & 3 deletions djangocms_text/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ class Meta:
abstract = True

def __str__(self):
return Truncator(strip_tags(self.body).replace("&shy;", "")).words(
3, truncate="..."
)
return Truncator(strip_tags(self.body).replace("&shy;", "")).words(3, truncate="...")

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down

0 comments on commit 6234e7a

Please sign in to comment.