From 27d468971b414acbfa2d67078f7f3760cb135050 Mon Sep 17 00:00:00 2001 From: devketanpro Date: Tue, 24 Oct 2023 12:29:08 +0530 Subject: [PATCH] Add new method for update the richtext into html --- server/planning/content_profiles/utils.py | 12 ++++++++++ server/planning/feed_parsers/events_ml.py | 12 ++-------- .../feed_parsers/superdesk_planning_xml.py | 22 ++++++++----------- server/planning/feed_parsers/utils.py | 10 +++++++++ 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/server/planning/content_profiles/utils.py b/server/planning/content_profiles/utils.py index b5ee03682..f74440c0b 100644 --- a/server/planning/content_profiles/utils.py +++ b/server/planning/content_profiles/utils.py @@ -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" + ) + ) diff --git a/server/planning/feed_parsers/events_ml.py b/server/planning/feed_parsers/events_ml.py index 0c1317641..45640b78a 100644 --- a/server/planning/feed_parsers/events_ml.py +++ b/server/planning/feed_parsers/events_ml.py @@ -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 @@ -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: @@ -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 @@ -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 diff --git a/server/planning/feed_parsers/superdesk_planning_xml.py b/server/planning/feed_parsers/superdesk_planning_xml.py index 951088d20..ac177c064 100644 --- a/server/planning/feed_parsers/superdesk_planning_xml.py +++ b/server/planning/feed_parsers/superdesk_planning_xml.py @@ -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__) @@ -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: @@ -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: @@ -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")) diff --git a/server/planning/feed_parsers/utils.py b/server/planning/feed_parsers/utils.py index f8acc6481..3062dc7a7 100644 --- a/server/planning/feed_parsers/utils.py +++ b/server/planning/feed_parsers/utils.py @@ -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): @@ -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) + }) \ No newline at end of file