Enhancement: title assignment placeholder error handling, fallback (#5282)

This commit is contained in:
shamoon
2024-01-10 10:18:55 -08:00
committed by GitHub
parent 416ad13aaf
commit 6d5f4e92cc
7 changed files with 157 additions and 25 deletions

View File

@@ -1386,12 +1386,38 @@ class WorkflowActionSerializer(serializers.ModelSerializer):
def validate(self, attrs):
# Empty strings treated as None to avoid unexpected behavior
if (
"assign_title" in attrs
and attrs["assign_title"] is not None
and len(attrs["assign_title"]) == 0
):
attrs["assign_title"] = None
if "assign_title" in attrs:
if attrs["assign_title"] is not None and len(attrs["assign_title"]) == 0:
attrs["assign_title"] = None
else:
try:
# test against all placeholders, see consumer.py `parse_doc_title_w_placeholders`
attrs["assign_title"].format(
correspondent="",
document_type="",
added="",
added_year="",
added_year_short="",
added_month="",
added_month_name="",
added_month_name_short="",
added_day="",
added_time="",
owner_username="",
original_filename="",
created="",
created_year="",
created_year_short="",
created_month="",
created_month_name="",
created_month_name_short="",
created_day="",
created_time="",
)
except (ValueError, KeyError) as e:
raise serializers.ValidationError(
{"assign_title": f'Invalid f-string detected: "{e.args[0]}"'},
)
return attrs