changeset 2537:07f892bc4bb0

WP import: Made WP import an uploaded import.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 07 Mar 2019 12:44:07 +0100
parents 0a3debcfbe8f
children 29eb15039d0d
files pkg/controllers/routes.go pkg/controllers/uploadedimports.go pkg/controllers/wpimports.go
diffstat 3 files changed, 41 insertions(+), 116 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/routes.go	Thu Mar 07 12:31:34 2019 +0100
+++ b/pkg/controllers/routes.go	Thu Mar 07 12:44:07 2019 +0100
@@ -200,7 +200,7 @@
 		http.HandlerFunc(importSoundingResult))).Methods(http.MethodPost)
 
 	api.Handle("/imports/wp", waterwayAdmin(
-		http.HandlerFunc(importWaterwayProfiles))).Methods(http.MethodPost)
+		http.HandlerFunc(importWaterwayProfiles()))).Methods(http.MethodPost)
 
 	api.Handle("/imports/agm", waterwayAdmin(
 		http.HandlerFunc(importApprovedGaugeMeasurements()))).Methods(http.MethodPost)
--- a/pkg/controllers/uploadedimports.go	Thu Mar 07 12:31:34 2019 +0100
+++ b/pkg/controllers/uploadedimports.go	Thu Mar 07 12:44:07 2019 +0100
@@ -14,9 +14,11 @@
 package controllers
 
 import (
+	"fmt"
 	"log"
 	"net/http"
 	"os"
+	"strconv"
 	"time"
 
 	"gemma.intevation.de/gemma/pkg/auth"
@@ -29,6 +31,44 @@
 
 type BadUploadParameterError string
 
+func importWaterwayProfiles() http.HandlerFunc {
+	return uploadedImport(
+		imports.WPJobKind,
+		"wp.csv",
+		func(req *http.Request, dir string) (imports.Job, error) {
+			url := req.FormValue("url")
+			if url == "" {
+				return nil, BadUploadParameterError("missing 'url' parameter")
+			}
+
+			featureType := req.FormValue("feature-type")
+			if featureType == "" {
+				return nil, BadUploadParameterError("missing 'feature-type' parameter")
+			}
+
+			sortBy := req.FormValue("sort-by")
+
+			var precision *float64
+			if p := req.FormValue("precision"); p != "" {
+				v, err := strconv.ParseFloat(p, 64)
+				if err != nil {
+					return nil, BadUploadParameterError(
+						fmt.Sprintf("Invalid 'precision' parameter: %v", err))
+				}
+				precision = &v
+			}
+
+			return &imports.WaterwayProfiles{
+				Dir:         dir,
+				URL:         url,
+				FeatureType: featureType,
+				SortBy:      sortBy,
+				Precision:   precision,
+			}, nil
+		},
+	)
+}
+
 func importApprovedGaugeMeasurements() http.HandlerFunc {
 	return uploadedImport(
 		imports.AGMJobKind,
--- a/pkg/controllers/wpimports.go	Thu Mar 07 12:31:34 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,115 +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 (
-	"fmt"
-	"log"
-	"net/http"
-	"strconv"
-	"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 (
-	maxWaterwayProfilesSize = 25 * 1024 * 1024
-	waterwayProfilesName    = "wp"
-)
-
-func importWaterwayProfiles(rw http.ResponseWriter, req *http.Request) {
-
-	url := req.FormValue("url")
-	if url == "" {
-		http.Error(rw, "missing 'url' parameter", http.StatusBadRequest)
-		return
-	}
-
-	featureType := req.FormValue("feature-type")
-	if featureType == "" {
-		http.Error(rw, "missing 'feature-type' parameter", http.StatusBadRequest)
-		return
-	}
-
-	dir, err := misc.StoreUploadedFile(
-		req,
-		waterwayProfilesName,
-		"wp.csv",
-		maxWaterwayProfilesSize)
-	if err != nil {
-		log.Printf("error: %v\n", err)
-		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
-		return
-	}
-
-	sortBy := req.FormValue("sort-by")
-
-	var precision *float64
-	if p := req.FormValue("precision"); p != "" {
-		if v, err := strconv.ParseFloat(p, 64); err != nil {
-			http.Error(rw,
-				fmt.Sprintf("Invalid 'precision' parameter: %v", err),
-				http.StatusBadRequest)
-			return
-		} else {
-			precision = &v
-		}
-	}
-
-	wp := &imports.WaterwayProfiles{
-		Dir:         dir,
-		URL:         url,
-		FeatureType: featureType,
-		SortBy:      sortBy,
-		Precision:   precision,
-	}
-
-	serialized, err := common.ToJSONString(wp)
-	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.WPJobKind,
-		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)
-}