changeset 2802:9adbe0633bf9

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)
author Fadi Abbud <fadi.abbud@intevation.de>
date Mon, 25 Mar 2019 18:33:19 +0100
parents 054f5d61452d
children e3c5efd21cb4
files client/src/components/systemconfiguration/PDFTemplates.vue
diffstat 1 files changed, 71 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- 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);