Improve validation of summary field

main
Bob Mottram 2024-04-23 20:46:30 +01:00
parent 3c540aea3d
commit 85d92d0c22
2 changed files with 21 additions and 2 deletions

View File

@ -533,6 +533,16 @@ def get_media_descriptions_from_post(post_json_object: {}) -> str:
return descriptions.strip()
def _valid_summary(possible_summary: str) -> bool:
"""Returns true if the given summary field is valid
"""
if not isinstance(possible_summary, str):
return False
if len(possible_summary) < 2:
return False
return True
def get_summary_from_post(post_json_object: {}, system_language: str,
languages_understood: []) -> str:
"""Returns the summary from the post in the given language
@ -543,7 +553,7 @@ def get_summary_from_post(post_json_object: {}, system_language: str,
languages_understood, "summary")
if summary_str:
summary_str = summary_str.strip()
if len(summary_str) < 2:
if not _valid_summary(summary_str):
summary_str = ''
return summary_str

View File

@ -2716,7 +2716,16 @@ def individual_post_as_html(signing_priv_key_pem: str,
if isinstance(post_json_object['object']['sensitive'], bool):
# sensitive posts should have a summary
if post_json_object['object'].get('summary'):
post_is_sensitive = post_json_object['object']['sensitive']
possible_summary = \
get_summary_from_post(post_json_object,
system_language,
languages_understood)
if possible_summary:
post_is_sensitive = \
post_json_object['object']['sensitive']
else:
# clear the summary field if it is invalid
post_json_object['object']['summary'] = ''
if not post_json_object['object'].get('summary'):
post_json_object['object']['summary'] = ''