# HG changeset patch # User Fadi Abbud # Date 1553535199 -3600 # Node ID 9adbe0633bf956b4c035130ddf93405bc62c7e1c # Parent 054f5d61452d9993a6cd0d5e170fad87e096dcd1 client:pdf-gen: improve template upload constraint * check the elements in the uploaded file if they match the predefined ones * let user upload the same template after upload is aborted due to incorrect json data or missing template-name (user wants to edit the file and reupload it) diff -r 054f5d61452d -r 9adbe0633bf9 client/src/components/systemconfiguration/PDFTemplates.vue --- a/client/src/components/systemconfiguration/PDFTemplates.vue Mon Mar 25 18:17:18 2019 +0100 +++ b/client/src/components/systemconfiguration/PDFTemplates.vue Mon Mar 25 18:33:19 2019 +0100 @@ -161,47 +161,80 @@ "Uploaded file does not contain valid json data." ) }); + //allow the user to upload the same file + //if user wants to upload the same file after edit it. + this.$refs.uploadTemplate.value = null; } if (template.name) { - this.uploading = true; - HTTP.post( - "/templates/print/" + template.name, - { - template_name: template.name, - template_data: template - }, - { - headers: { - "X-Gemma-Auth": localStorage.getItem("token"), - "Content-type": "text/xml; charset=UTF-8" - } - } - ) - .then(() => { - this.loadTemplates(); - displayInfo({ + //check if an element in the uploaded file does not match the predefind template-elements + let checkElement = false; + template.elements.forEach(e => { + if ( + [ + "text", + "box", + "textbox", + "image", + "bottleneck", + "legend", + "scalebar", + "scale", + "northarrow" + ].indexOf(e.type) === -1 + ) { + checkElement = true; + displayError({ + title: this.$gettext("Invalid element"), message: - template.name + " " + this.$gettext("uploaded successfully") + e.type + + this.$gettext(" does not match any template's element") }); - }) - .catch(e => { - const { status, data } = e.response; - if (status === 400) { - displayError({ - title: this.$gettext("Error"), - message: `${data.message || data}` + //allow the user to upload the same file + this.$refs.uploadTemplate.value = null; + } + }); + + if (!checkElement) { + this.uploading = true; + HTTP.post( + "/templates/print/" + template.name, + { + template_name: template.name, + template_data: template + }, + { + headers: { + "X-Gemma-Auth": localStorage.getItem("token"), + "Content-type": "text/xml; charset=UTF-8" + } + } + ) + .then(() => { + this.loadTemplates(); + displayInfo({ + message: + template.name + " " + this.$gettext("uploaded successfully") }); - } else { - displayError({ - title: this.$gettext("Backend Error"), - message: `${status}: ${data.message || data}` - }); - } - }) - .finally(() => { - this.uploading = false; - this.$refs.uploadTemplate.value = null; - }); + }) + .catch(e => { + const { status, data } = e.response; + if (status === 400) { + displayError({ + title: this.$gettext("Error"), + message: `${data.message || data}` + }); + } else { + displayError({ + title: this.$gettext("Backend Error"), + message: `${status}: ${data.message || data}` + }); + } + }) + .finally(() => { + this.uploading = false; + this.$refs.uploadTemplate.value = null; + }); + } } else { displayError({ title: this.$gettext("Format Error"), @@ -209,6 +242,8 @@ "The provided template has no name property." ) }); + //allow the user to upload the same file + this.$refs.uploadTemplate.value = null; } }; reader.onerror = error => console.log(error);