Skip to content

Commit

Permalink
feat(templates): allow disabling validation
Browse files Browse the repository at this point in the history
Add new parameter `disable_template_validation` on template upload. This
can be used if you cannot provide correct sample data upon upload, and/or
the magic validation does not work.
  • Loading branch information
winged committed Dec 17, 2020
1 parent 21596e9 commit f371b33
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
12 changes: 12 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ in the example above would become something like:
... ),
```

### Disabling template validation

Sometimes, templates contain advanced syntax that cannot be correctly validated
by the automatic mechanism. If you at the same time are also unable to provide
usable sample data, you can disable template validation entirely.

Please note that in this case, templates will be accepted that may cause errors
when actually used, so make sure to test them after uploading!

To disable template validation, pass in the additional parameter
`disable_template_validation` with the value `true` on template upload.


## Merging templates

Expand Down
10 changes: 10 additions & 0 deletions document_merge_service/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def __call__(self, serializer_field):


class TemplateSerializer(serializers.ModelSerializer):
disable_template_validation = serializers.BooleanField(
allow_null=True, default=False
)
group = serializers.CharField(allow_null=True, default=CurrentGroupDefault())
available_placeholders = serializers.ListField(allow_null=True, required=False)
sample_data = serializers.JSONField(allow_null=True, required=False)
Expand Down Expand Up @@ -63,6 +66,12 @@ def _(doc):
return sorted([x.replace(".[]", "[]") for x in _doc(sample_doc)])

def validate(self, data):
if data.pop("disable_template_validation", False):
# Some template structures cannot be validated automatically,
# or it would be impossible or too much effort to provide accurate
# sample data. For those cases, we allow disabling the validation.
return data

engine = data.get("engine", self.instance and self.instance.engine)
template = data.get("template", self.instance and self.instance.template)

Expand Down Expand Up @@ -108,6 +117,7 @@ class Meta:
"available_placeholders",
"sample_data",
"files",
"disable_template_validation",
)


Expand Down
47 changes: 47 additions & 0 deletions document_merge_service/api/tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,53 @@ def test_template_create(
Document(io.BytesIO(response.content))


@pytest.mark.parametrize(
"status_code, disable_validation",
[
(
status.HTTP_400_BAD_REQUEST,
"false",
),
(
status.HTTP_400_BAD_REQUEST,
"",
),
(
status.HTTP_201_CREATED,
"true",
),
],
)
def test_disable_validation(
db,
status_code,
admin_client,
settings,
disable_validation,
):
settings.REQUIRE_AUTHENTICATION = False
url = reverse("template-list")

template_file = django_file("docx-template-syntax.docx")
data = {
"slug": "test-slug",
"template": template_file.file,
"engine": models.Template.DOCX_TEMPLATE,
}
if disable_validation:
data["disable_template_validation"] = disable_validation

response = admin_client.post(url, data=data, format="multipart")
assert response.status_code == status_code

if status_code == status.HTTP_201_CREATED:
data = response.json()
template_link = data["template"]
response = admin_client.get(template_link)
assert response.status_code == status.HTTP_200_OK
Document(io.BytesIO(response.content))


@pytest.mark.parametrize(
"template_name,available_placeholders,sample_data,files,expect_missing_placeholders,engine,status_code",
[
Expand Down

0 comments on commit f371b33

Please sign in to comment.