Also catch invalid f-string at serializer
And display in workflow edit modal
This commit is contained in:
parent
a8a53e1b54
commit
c68f7152c6
@ -99,7 +99,7 @@
|
|||||||
<input type="hidden" formControlName="id" />
|
<input type="hidden" formControlName="id" />
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<pngx-input-text i18n-title title="Assign title" formControlName="assign_title" i18n-hint hint="Can include some placeholders, see <a target='_blank' href='https://docs.paperless-ngx.com/usage/#workflows'>documentation</a>." [error]="error?.assign_title"></pngx-input-text>
|
<pngx-input-text i18n-title title="Assign title" formControlName="assign_title" i18n-hint hint="Can include some placeholders, see <a target='_blank' href='https://docs.paperless-ngx.com/usage/#workflows'>documentation</a>." [error]="error?.actions?.[i]?.assign_title"></pngx-input-text>
|
||||||
<pngx-input-tags [allowCreate]="false" i18n-title title="Assign tags" formControlName="assign_tags"></pngx-input-tags>
|
<pngx-input-tags [allowCreate]="false" i18n-title title="Assign tags" formControlName="assign_tags"></pngx-input-tags>
|
||||||
<pngx-input-select i18n-title title="Assign document type" [items]="documentTypes" [allowNull]="true" formControlName="assign_document_type"></pngx-input-select>
|
<pngx-input-select i18n-title title="Assign document type" [items]="documentTypes" [allowNull]="true" formControlName="assign_document_type"></pngx-input-select>
|
||||||
<pngx-input-select i18n-title title="Assign correspondent" [items]="correspondents" [allowNull]="true" formControlName="assign_correspondent"></pngx-input-select>
|
<pngx-input-select i18n-title title="Assign correspondent" [items]="correspondents" [allowNull]="true" formControlName="assign_correspondent"></pngx-input-select>
|
||||||
|
@ -1386,12 +1386,38 @@ class WorkflowActionSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
def validate(self, attrs):
|
def validate(self, attrs):
|
||||||
# Empty strings treated as None to avoid unexpected behavior
|
# Empty strings treated as None to avoid unexpected behavior
|
||||||
if (
|
if "assign_title" in attrs:
|
||||||
"assign_title" in attrs
|
if attrs["assign_title"] is not None and len(attrs["assign_title"]) == 0:
|
||||||
and attrs["assign_title"] is not None
|
|
||||||
and len(attrs["assign_title"]) == 0
|
|
||||||
):
|
|
||||||
attrs["assign_title"] = None
|
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 as e:
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
{"assign_title": f"Invalid f-string detected: {e.args[0]}"},
|
||||||
|
)
|
||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
@ -248,6 +248,45 @@ class TestApiWorkflows(DirectoriesMixin, APITestCase):
|
|||||||
|
|
||||||
self.assertEqual(WorkflowTrigger.objects.count(), 1)
|
self.assertEqual(WorkflowTrigger.objects.count(), 1)
|
||||||
|
|
||||||
|
def test_api_create_invalid_assign_title(self):
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- API request to create a workflow
|
||||||
|
- Invalid f-string for assign_title
|
||||||
|
WHEN:
|
||||||
|
- API is called
|
||||||
|
THEN:
|
||||||
|
- Correct HTTP 400 response
|
||||||
|
- No objects are created
|
||||||
|
"""
|
||||||
|
response = self.client.post(
|
||||||
|
self.ENDPOINT,
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"name": "Workflow 1",
|
||||||
|
"order": 1,
|
||||||
|
"triggers": [
|
||||||
|
{
|
||||||
|
"type": WorkflowTrigger.WorkflowTriggerType.DOCUMENT_UPDATED,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"assign_title": "{created_year]",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
content_type="application/json",
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
|
self.assertIn(
|
||||||
|
"Invalid f-string detected",
|
||||||
|
response.data["actions"][0]["assign_title"][0],
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(Workflow.objects.count(), 1)
|
||||||
|
|
||||||
def test_api_create_workflow_trigger_action_empty_fields(self):
|
def test_api_create_workflow_trigger_action_empty_fields(self):
|
||||||
"""
|
"""
|
||||||
GIVEN:
|
GIVEN:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user