changeset 1901:71b722809b2b

Stretch import: Added stub.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 18 Jan 2019 15:52:51 +0100
parents 6a67cd819e93
children c4af342be999
files pkg/controllers/manualimports.go pkg/controllers/routes.go pkg/models/common.go pkg/models/stretch.go pkg/models/user.go
diffstat 5 files changed, 118 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/manualimports.go	Fri Jan 18 15:04:53 2019 +0100
+++ b/pkg/controllers/manualimports.go	Fri Jan 18 15:52:51 2019 +0100
@@ -144,6 +144,22 @@
 	return dma, due, retries, dmai.SendEmail
 }
 
+func importStretch(input interface{}) (interface{}, time.Time, int, bool) {
+	sti := input.(*models.StretchImport)
+	st := &imports.Stretch{
+		Name:      sti.Name,
+		From:      sti.From,
+		To:        sti.To,
+		ObjNam:    sti.ObjNam,
+		NObjNam:   sti.NObjNam,
+		Source:    sti.Source,
+		Date:      sti.Date,
+		Countries: sti.Countries,
+	}
+	due, retries := retry(sti.Attributes)
+	return st, due, retries, sti.SendEmail
+}
+
 func manualImport(
 	kind imports.JobKind,
 	setup func(interface{}) (interface{}, time.Time, int, bool),
--- a/pkg/controllers/routes.go	Fri Jan 18 15:04:53 2019 +0100
+++ b/pkg/controllers/routes.go	Fri Jan 18 15:52:51 2019 +0100
@@ -229,6 +229,12 @@
 		NoConn: true,
 	})).Methods(http.MethodPost)
 
+	api.Handle("/imports/stretch", sysAdmin(&JSONHandler{
+		Input:  func() interface{} { return new(models.StretchImport) },
+		Handle: manualImport(imports.STJobKind, importStretch),
+		NoConn: true,
+	})).Methods(http.MethodPost)
+
 	// Import scheduler configuration
 	api.Handle("/imports/config/{id:[0-9]+}/run",
 		waterwayAdmin(&JSONHandler{
--- a/pkg/models/common.go	Fri Jan 18 15:04:53 2019 +0100
+++ b/pkg/models/common.go	Fri Jan 18 15:52:51 2019 +0100
@@ -14,8 +14,11 @@
 package models
 
 import (
+	"database/sql/driver"
 	"encoding/json"
 	"errors"
+	"fmt"
+	"strings"
 	"time"
 )
 
@@ -29,7 +32,13 @@
 
 const DateFormat = "2006-01-02"
 
-type Date struct{ time.Time }
+type (
+	Date struct{ time.Time }
+	// Country is a valid country 2 letter code.
+	Country string
+	// UniqueCountries is a list of unique countries.
+	UniqueCountries []Country
+)
 
 func (srd Date) MarshalJSON() ([]byte, error) {
 	return json.Marshal(srd.Format(DateFormat))
@@ -46,3 +55,59 @@
 	}
 	return err
 }
+
+var (
+	validCountries = []string{
+		"AT", "BG", "DE", "HU", "HR",
+		"MD", "RO", "RS", "SK", "UA",
+	}
+	errNoValidCountry = errors.New("Not a valid country")
+)
+
+// UnmarshalJSON ensures that the given string forms a valid
+// two letter country code.
+func (c *Country) UnmarshalJSON(data []byte) error {
+	var s string
+	if err := json.Unmarshal(data, &s); err != nil {
+		return err
+	}
+	s = strings.ToUpper(s)
+	for _, v := range validCountries {
+		if v == s {
+			*c = Country(v)
+			return nil
+		}
+	}
+	return errNoValidCountry
+}
+
+// Value implements the driver.Valuer interface.
+func (c Country) Value() (driver.Value, error) {
+	return string(c), nil
+}
+
+// Scan implements the sql.Scanner interfaces.
+func (c *Country) Scan(src interface{}) (err error) {
+	if s, ok := src.(string); ok {
+		*c = Country(s)
+	} else {
+		err = errNoString
+	}
+	return
+}
+
+func (uc *UniqueCountries) UnmarshalJSON(data []byte) error {
+	var countries []Country
+	if err := json.Unmarshal(data, &countries); err != nil {
+		return err
+	}
+	unique := map[Country]struct{}{}
+	for _, c := range countries {
+		if _, found := unique[c]; found {
+			return fmt.Errorf("country '%s' is not unique", string(c))
+		}
+		unique[c] = struct{}{}
+	}
+	*uc = countries
+	return nil
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/models/stretch.go	Fri Jan 18 15:52:51 2019 +0100
@@ -0,0 +1,30 @@
+// 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 models
+
+import "gemma.intevation.de/gemma/pkg/common"
+
+type StretchImport struct {
+	Name      string          `json:"name"`
+	From      Isrs            `json:"from"`
+	To        Isrs            `json:"to"`
+	ObjNam    string          `json:"objnam"`
+	NObjNam   *string         `json:"nobjnam"`
+	Source    string          `json:"source-organization"`
+	Date      Date            `json:"date-info"`
+	Countries UniqueCountries `json:"countries"`
+
+	SendEmail  bool              `json:"send-email"`
+	Attributes common.Attributes `json:"attributes,omitempty"`
+}
--- a/pkg/models/user.go	Fri Jan 18 15:04:53 2019 +0100
+++ b/pkg/models/user.go	Fri Jan 18 15:52:51 2019 +0100
@@ -25,8 +25,6 @@
 type (
 	// Email is a string formed by a valid EMail address.
 	Email string
-	// Country is a valid country 2 letter code.
-	Country string
 	// Role is a string with a valid gemma role.
 	Role string
 	// UserName is a string forming a valid user name.
@@ -132,46 +130,6 @@
 }
 
 var (
-	validCountries = []string{
-		"AT", "BG", "DE", "HU", "HR",
-		"MD", "RO", "RS", "SK", "UA",
-	}
-	errNoValidCountry = errors.New("Not a valid country")
-)
-
-// UnmarshalJSON ensures that the given string forms a valid
-// two letter country code.
-func (c *Country) UnmarshalJSON(data []byte) error {
-	var s string
-	if err := json.Unmarshal(data, &s); err != nil {
-		return err
-	}
-	s = strings.ToUpper(s)
-	for _, v := range validCountries {
-		if v == s {
-			*c = Country(v)
-			return nil
-		}
-	}
-	return errNoValidCountry
-}
-
-// Value implements the driver.Valuer interface.
-func (c Country) Value() (driver.Value, error) {
-	return string(c), nil
-}
-
-// Scan implements the sql.Scanner interfaces.
-func (c *Country) Scan(src interface{}) (err error) {
-	if s, ok := src.(string); ok {
-		*c = Country(s)
-	} else {
-		err = errNoString
-	}
-	return
-}
-
-var (
 	validRoles = []string{
 		"waterway_user",
 		"waterway_admin",