Skip to content

Commit

Permalink
Add new method for update the richtext into html
Browse files Browse the repository at this point in the history
  • Loading branch information
devketanpro committed Oct 24, 2023
1 parent 453bf9d commit 27d4689
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
12 changes: 12 additions & 0 deletions server/planning/content_profiles/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,15 @@ def get_multilingual_fields(resource: str) -> Set[str]:
)
)
)

def get_editor3_fields(resource: str) -> Set[str]:
content_type = get_planning_schema(resource)
resource_schema = content_type.get("schema") or {}
return set(
field_name
for field_name, field_schema in resource_schema.items()
if (
is_field_enabled(field_name, content_type)
and ((field_schema or {})).get("field_type", "") == "editor_3"
)
)
12 changes: 2 additions & 10 deletions server/planning/feed_parsers/events_ml.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
)
from superdesk.errors import ParserError
from superdesk.utc import local_to_utc, utc_to_local
from superdesk.text_utils import plain_text_to_html
from planning.content_profiles.utils import get_planning_schema, is_field_enabled, is_field_editor_3
from planning.content_profiles.utils import get_planning_schema, is_field_enabled
from planning.common import POST_STATE

from . import utils
Expand Down Expand Up @@ -95,7 +94,7 @@ def parse(self, tree: Element, provider=None):
self.parse_content_meta(tree, item)
self.parse_concept(tree, item)
self.parse_event_details(tree, item)

utils.upgrade_rich_text_fields(item, "event")
return [item]

except Exception as ex:
Expand Down Expand Up @@ -155,10 +154,6 @@ def parse_concept(self, tree, item):

try:
definition = concept.find(self.qname("definition")).text or ""
# if is_field_editor_3("event", "definition_short"):
if is_field_editor_3("definition_short", get_planning_schema("event")):
definition = plain_text_to_html(definition)

item["definition_short"] = definition
except Exception:
pass
Expand Down Expand Up @@ -253,9 +248,6 @@ def parse_registration_details(self, event_details, item):
registration = event_details.find(self.qname("registration"))
if registration is not None and registration.text:
registration_details = registration.text
if is_field_editor_3("registration_details", event_type):
registration_details = plain_text_to_html(registration_details)

item["registration_details"] = registration_details
except Exception:
pass
22 changes: 9 additions & 13 deletions server/planning/feed_parsers/superdesk_planning_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
POST_STATE,
)

from planning.content_profiles.utils import get_planning_schema, is_field_editor_3
from superdesk.text_utils import plain_text_to_html
from planning.content_profiles.utils import get_planning_schema
from .utils import upgrade_rich_text_fields

utc = pytz.UTC
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -96,6 +96,10 @@ def parse(self, tree: Element, provider=None):
self.parse_news_coverage_set(tree, item)
self.parse_news_coverage_status(tree, item)

upgrade_rich_text_fields(item, "planning")
for coverage in item.get("coverages") or []:
upgrade_rich_text_fields(coverage.get("planning") or {}, "coverage")

return [item]

except Exception as ex:
Expand All @@ -115,9 +119,7 @@ def parse_item_meta(self, tree, item, planning_type):

editor_note = meta.find(self.qname("edNote"))
if editor_note is not None and editor_note.text:
item["ednote"] = (
plain_text_to_html(editor_note.text) if is_field_editor_3("edNote", planning_type) else editor_note.text
)
item["ednote"] = editor_note.text

item_class_elt = meta.find(self.qname("itemClass"))
if item_class_elt is not None:
Expand Down Expand Up @@ -151,17 +153,11 @@ def parse_content_meta(self, tree, item, planning_type):
slugline_elt = content_meta.find(self.qname("headline"))
if slugline_elt is not None and slugline_elt.text:
item["slugline"] = slugline_elt.text
item["name"] = (
plain_text_to_html(slugline_elt.text) if is_field_editor_3("name", planning_type) else slugline_elt.text
)
item["name"] = slugline_elt.text

description_elt = content_meta.find(self.qname("description"))
if description_elt is not None and description_elt.text:
item["description_text"] = (
plain_text_to_html(description_elt.text)
if is_field_editor_3("description_text", planning_type)
else description_elt.text
)
item["description_text"] = description_elt.text

# Assigning the planning date from subject
subject_elt = content_meta.find(self.qname("subject"))
Expand Down
10 changes: 10 additions & 0 deletions server/planning/feed_parsers/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import re
import pytz
import datetime
from typing import Dict, Any
from planning.content_profiles.utils import get_editor3_fields
from superdesk.text_utils import plain_text_to_html


def has_time(value: str):
Expand All @@ -25,3 +28,10 @@ def parse_duration(value: str) -> datetime.timedelta:
if kwargs:
return datetime.timedelta(**kwargs)
raise ValueError(f"Could not parse duration string {value!r}")

def upgrade_rich_text_fields(item: Dict[str, Any], resource: str):
item.update({
field: plain_text_to_html(item[field])
for field in get_editor3_fields(resource)
if item.get(field)
})

0 comments on commit 27d4689

Please sign in to comment.