changeset 2256:0d272d7bcfb9

Uploaded imports: Unified agm, ubn, ufa and ugm import endpoints and removed a lot c&p code.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 13 Feb 2019 18:18:41 +0100
parents 33af355047e1
children ce6fd3d4a3a2
files pkg/controllers/agmimports.go pkg/controllers/routes.go pkg/controllers/ubnimports.go pkg/controllers/ufaimports.go pkg/controllers/ugmimports.go pkg/controllers/uploadedimports.go
diffstat 6 files changed, 130 insertions(+), 330 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/agmimports.go	Wed Feb 13 17:47:33 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,81 +0,0 @@
-// This is Free Software under GNU Affero General Public License v >= 3.0
-// without warranty, see README.md and license for details.
-//
-// SPDX-License-Identifier: AGPL-3.0-or-later
-// License-Filename: LICENSES/AGPL-3.0.txt
-//
-// Copyright (C) 2018 by via donau
-//   – Österreichische Wasserstraßen-Gesellschaft mbH
-// Software engineering by Intevation GmbH
-//
-// Author(s):
-//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
-
-package controllers
-
-import (
-	"log"
-	"net/http"
-	"time"
-
-	"gemma.intevation.de/gemma/pkg/auth"
-	"gemma.intevation.de/gemma/pkg/common"
-	"gemma.intevation.de/gemma/pkg/imports"
-	"gemma.intevation.de/gemma/pkg/misc"
-)
-
-const (
-	approvedGaugeMeasurementsName   = "agm"
-	maxApprovedGaugeMeasurementSize = 25 * 1024 * 1024
-)
-
-func importApprovedGaugeMeasurements(rw http.ResponseWriter, req *http.Request) {
-
-	dir, err := misc.StoreUploadedFile(
-		req,
-		approvedGaugeMeasurementsName,
-		"agm.csv",
-		maxApprovedGaugeMeasurementSize)
-	if err != nil {
-		log.Printf("error: %v\n", err)
-		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
-		return
-	}
-
-	agm := &imports.ApprovedGaugeMeasurements{Dir: dir}
-
-	serialized, err := common.ToJSONString(agm)
-	if err != nil {
-		log.Printf("error: %v\n", err)
-		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
-		return
-	}
-
-	session, _ := auth.GetSession(req)
-
-	sendEmail := req.FormValue("email") != ""
-
-	jobID, err := imports.AddJob(
-		imports.AGMJobKind,
-		time.Time{}, // due
-		nil,         // trys
-		nil,         // retry wait
-		session.User,
-		sendEmail,
-		serialized)
-
-	if err != nil {
-		log.Printf("error: %v\n", err)
-		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
-		return
-	}
-
-	log.Printf("info: added import #%d to queue\n", jobID)
-
-	result := struct {
-		ID int64 `json:"id"`
-	}{
-		ID: jobID,
-	}
-	SendJSON(rw, http.StatusCreated, &result)
-}
--- a/pkg/controllers/routes.go	Wed Feb 13 17:47:33 2019 +0100
+++ b/pkg/controllers/routes.go	Wed Feb 13 18:18:41 2019 +0100
@@ -199,20 +199,20 @@
 	api.Handle("/imports/sr", waterwayAdmin(
 		http.HandlerFunc(importSoundingResult))).Methods(http.MethodPost)
 
-	api.Handle("/imports/agm", waterwayAdmin(
-		http.HandlerFunc(importApprovedGaugeMeasurements))).Methods(http.MethodPost)
-
 	api.Handle("/imports/wp", waterwayAdmin(
 		http.HandlerFunc(importWaterwayProfiles))).Methods(http.MethodPost)
 
+	api.Handle("/imports/agm", waterwayAdmin(
+		http.HandlerFunc(importApprovedGaugeMeasurements()))).Methods(http.MethodPost)
+
 	api.Handle("/imports/ubn", waterwayAdmin(
-		http.HandlerFunc(importUploadedBottleneck))).Methods(http.MethodPost)
+		http.HandlerFunc(importUploadedBottleneck()))).Methods(http.MethodPost)
 
 	api.Handle("/imports/ufa", waterwayAdmin(
-		http.HandlerFunc(importUploadedFairwayAvailability))).Methods(http.MethodPost)
+		http.HandlerFunc(importUploadedFairwayAvailability()))).Methods(http.MethodPost)
 
 	api.Handle("/imports/ugm", waterwayAdmin(
-		http.HandlerFunc(importUploadedGaugeMeasurement))).Methods(http.MethodPost)
+		http.HandlerFunc(importUploadedGaugeMeasurement()))).Methods(http.MethodPost)
 
 	api.Handle("/imports/{kind:st}", sysAdmin(&JSONHandler{
 		Input:  importModel,
--- a/pkg/controllers/ubnimports.go	Wed Feb 13 17:47:33 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,81 +0,0 @@
-// This is Free Software under GNU Affero General Public License v >= 3.0
-// without warranty, see README.md and license for details.
-//
-// SPDX-License-Identifier: AGPL-3.0-or-later
-// License-Filename: LICENSES/AGPL-3.0.txt
-//
-// Copyright (C) 2018 by via donau
-//   – Österreichische Wasserstraßen-Gesellschaft mbH
-// Software engineering by Intevation GmbH
-//
-// Author(s):
-//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
-
-package controllers
-
-import (
-	"log"
-	"net/http"
-	"time"
-
-	"gemma.intevation.de/gemma/pkg/auth"
-	"gemma.intevation.de/gemma/pkg/common"
-	"gemma.intevation.de/gemma/pkg/imports"
-	"gemma.intevation.de/gemma/pkg/misc"
-)
-
-const (
-	maxUploadedBottleneckSize = 25 * 1024 * 1024
-	uploadBottleneckName      = "ubn"
-)
-
-func importUploadedBottleneck(rw http.ResponseWriter, req *http.Request) {
-
-	dir, err := misc.StoreUploadedFile(
-		req,
-		uploadBottleneckName,
-		"data.xml",
-		maxUploadedBottleneckSize)
-	if err != nil {
-		log.Printf("error: %v\n", err)
-		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
-		return
-	}
-
-	ubn := &imports.UploadedBottleneck{Dir: dir}
-
-	serialized, err := common.ToJSONString(ubn)
-	if err != nil {
-		log.Printf("error: %v\n", err)
-		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
-		return
-	}
-
-	session, _ := auth.GetSession(req)
-
-	sendEmail := req.FormValue("email") != ""
-
-	jobID, err := imports.AddJob(
-		imports.UBNJobKind,
-		time.Time{}, // due
-		nil,         // trys
-		nil,         // wait retry
-		session.User,
-		sendEmail,
-		serialized)
-
-	if err != nil {
-		log.Printf("error: %v\n", err)
-		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
-		return
-	}
-
-	log.Printf("info: added import #%d to queue\n", jobID)
-
-	result := struct {
-		ID int64 `json:"id"`
-	}{
-		ID: jobID,
-	}
-	SendJSON(rw, http.StatusCreated, &result)
-}
--- a/pkg/controllers/ufaimports.go	Wed Feb 13 17:47:33 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,81 +0,0 @@
-// This is Free Software under GNU Affero General Public License v >= 3.0
-// without warranty, see README.md and license for details.
-//
-// SPDX-License-Identifier: AGPL-3.0-or-later
-// License-Filename: LICENSES/AGPL-3.0.txt
-//
-// Copyright (C) 2018 by via donau
-//   – Österreichische Wasserstraßen-Gesellschaft mbH
-// Software engineering by Intevation GmbH
-//
-// Author(s):
-//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
-
-package controllers
-
-import (
-	"log"
-	"net/http"
-	"time"
-
-	"gemma.intevation.de/gemma/pkg/auth"
-	"gemma.intevation.de/gemma/pkg/common"
-	"gemma.intevation.de/gemma/pkg/imports"
-	"gemma.intevation.de/gemma/pkg/misc"
-)
-
-const (
-	maxUploadedFairwayAvailabilitySize = 25 * 1024 * 1024
-	uploadFairwayAvailabilityName      = "ufa"
-)
-
-func importUploadedFairwayAvailability(rw http.ResponseWriter, req *http.Request) {
-
-	dir, err := misc.StoreUploadedFile(
-		req,
-		uploadFairwayAvailabilityName,
-		"data.xml",
-		maxUploadedFairwayAvailabilitySize)
-	if err != nil {
-		log.Printf("error: %v\n", err)
-		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
-		return
-	}
-
-	ufa := &imports.UploadedFairwayAvailability{Dir: dir}
-
-	serialized, err := common.ToJSONString(ufa)
-	if err != nil {
-		log.Printf("error: %v\n", err)
-		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
-		return
-	}
-
-	session, _ := auth.GetSession(req)
-
-	sendEmail := req.FormValue("email") != ""
-
-	jobID, err := imports.AddJob(
-		imports.UFAJobKind,
-		time.Time{}, // due
-		nil,         // trys
-		nil,         // wait retry
-		session.User,
-		sendEmail,
-		serialized)
-
-	if err != nil {
-		log.Printf("error: %v\n", err)
-		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
-		return
-	}
-
-	log.Printf("info: added import #%d to queue\n", jobID)
-
-	result := struct {
-		ID int64 `json:"id"`
-	}{
-		ID: jobID,
-	}
-	SendJSON(rw, http.StatusCreated, &result)
-}
--- a/pkg/controllers/ugmimports.go	Wed Feb 13 17:47:33 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,81 +0,0 @@
-// This is Free Software under GNU Affero General Public License v >= 3.0
-// without warranty, see README.md and license for details.
-//
-// SPDX-License-Identifier: AGPL-3.0-or-later
-// License-Filename: LICENSES/AGPL-3.0.txt
-//
-// Copyright (C) 2018 by via donau
-//   – Österreichische Wasserstraßen-Gesellschaft mbH
-// Software engineering by Intevation GmbH
-//
-// Author(s):
-//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
-
-package controllers
-
-import (
-	"log"
-	"net/http"
-	"time"
-
-	"gemma.intevation.de/gemma/pkg/auth"
-	"gemma.intevation.de/gemma/pkg/common"
-	"gemma.intevation.de/gemma/pkg/imports"
-	"gemma.intevation.de/gemma/pkg/misc"
-)
-
-const (
-	maxUploadedGaugeMeasurementSize = 25 * 1024 * 1024
-	uploadedGaugeMeasurementName    = "ugm"
-)
-
-func importUploadedGaugeMeasurement(rw http.ResponseWriter, req *http.Request) {
-
-	dir, err := misc.StoreUploadedFile(
-		req,
-		uploadedGaugeMeasurementName,
-		"data.xml",
-		maxUploadedGaugeMeasurementSize)
-	if err != nil {
-		log.Printf("error: %v\n", err)
-		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
-		return
-	}
-
-	ugm := &imports.UploadedGaugeMeasurement{Dir: dir}
-
-	serialized, err := common.ToJSONString(ugm)
-	if err != nil {
-		log.Printf("error: %v\n", err)
-		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
-		return
-	}
-
-	session, _ := auth.GetSession(req)
-
-	sendEmail := req.FormValue("email") != ""
-
-	jobID, err := imports.AddJob(
-		imports.UGMJobKind,
-		time.Time{}, // due
-		nil,         // trys
-		nil,         // wait retry
-		session.User,
-		sendEmail,
-		serialized)
-
-	if err != nil {
-		log.Printf("error: %v\n", err)
-		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
-		return
-	}
-
-	log.Printf("info: added import #%d to queue\n", jobID)
-
-	result := struct {
-		ID int64 `json:"id"`
-	}{
-		ID: jobID,
-	}
-	SendJSON(rw, http.StatusCreated, &result)
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/controllers/uploadedimports.go	Wed Feb 13 18:18:41 2019 +0100
@@ -0,0 +1,124 @@
+// This is Free Software under GNU Affero General Public License v >= 3.0
+// without warranty, see README.md and license for details.
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// License-Filename: LICENSES/AGPL-3.0.txt
+//
+// Copyright (C) 2018 by via donau
+//   – Österreichische Wasserstraßen-Gesellschaft mbH
+// Software engineering by Intevation GmbH
+//
+// Author(s):
+//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+package controllers
+
+import (
+	"log"
+	"net/http"
+	"time"
+
+	"gemma.intevation.de/gemma/pkg/auth"
+	"gemma.intevation.de/gemma/pkg/common"
+	"gemma.intevation.de/gemma/pkg/imports"
+	"gemma.intevation.de/gemma/pkg/misc"
+)
+
+const maxUploadSize = 25 * 1024 * 1024
+
+func importApprovedGaugeMeasurements() http.HandlerFunc {
+	return uploadedImport(
+		imports.AGMJobKind,
+		"agm.csv",
+		func(dir string) imports.Job {
+			return &imports.ApprovedGaugeMeasurements{Dir: dir}
+		},
+	)
+}
+
+func importUploadedBottleneck() http.HandlerFunc {
+	return uploadedImport(
+		imports.UBNJobKind,
+		"data.xml",
+		func(dir string) imports.Job {
+			return &imports.UploadedBottleneck{Dir: dir}
+		},
+	)
+}
+
+func importUploadedFairwayAvailability() http.HandlerFunc {
+	return uploadedImport(
+		imports.UFAJobKind,
+		"data.xml",
+		func(dir string) imports.Job {
+			return &imports.UploadedFairwayAvailability{Dir: dir}
+		},
+	)
+}
+
+func importUploadedGaugeMeasurement() http.HandlerFunc {
+	return uploadedImport(
+		imports.UGMJobKind,
+		"data.xml",
+		func(dir string) imports.Job {
+			return &imports.UploadedGaugeMeasurement{Dir: dir}
+		},
+	)
+}
+
+func uploadedImport(
+	kind imports.JobKind,
+	fname string,
+	create func(string) imports.Job,
+) http.HandlerFunc {
+
+	return func(rw http.ResponseWriter, req *http.Request) {
+		dir, err := misc.StoreUploadedFile(
+			req,
+			string(kind),
+			fname,
+			maxUploadSize)
+		if err != nil {
+			log.Printf("error: %v\n", err)
+			http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
+			return
+		}
+
+		job := create(dir)
+
+		serialized, err := common.ToJSONString(job)
+		if err != nil {
+			log.Printf("error: %v\n", err)
+			http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
+			return
+		}
+
+		session, _ := auth.GetSession(req)
+
+		sendEmail := req.FormValue("email") != ""
+
+		jobID, err := imports.AddJob(
+			kind,
+			time.Time{}, // due
+			nil,         // trys
+			nil,         // retry wait
+			session.User,
+			sendEmail,
+			serialized)
+
+		if err != nil {
+			log.Printf("error: %v\n", err)
+			http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
+			return
+		}
+
+		log.Printf("info: added import #%d to queue\n", jobID)
+
+		result := struct {
+			ID int64 `json:"id"`
+		}{
+			ID: jobID,
+		}
+		SendJSON(rw, http.StatusCreated, &result)
+	}
+}