Mercurial > gemma
changeset 3217:4c254651d80b
Added template types "map", "diagram", "report".
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 09 May 2019 12:08:02 +0200 |
parents | ee6a4e8af766 |
children | c2b65a549c6f |
files | pkg/controllers/common.go pkg/controllers/importqueue.go pkg/controllers/printtemplates.go pkg/controllers/routes.go schema/gemma.sql |
diffstat | 5 files changed, 125 insertions(+), 81 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/controllers/common.go Thu May 09 11:56:11 2019 +0200 +++ b/pkg/controllers/common.go Thu May 09 12:08:02 2019 +0200 @@ -15,7 +15,11 @@ import ( "fmt" + "log" + "strconv" "strings" + + "github.com/jackc/pgx/pgtype" ) type ( @@ -76,3 +80,47 @@ fn.filterNode.serialize(stmt, args) stmt.WriteByte(')') } + +func toInt8Array(txt string) *pgtype.Int8Array { + parts := strings.Split(txt, ",") + var ints []int64 + for _, part := range parts { + part = strings.TrimSpace(part) + v, err := strconv.ParseInt(part, 10, 64) + if err != nil { + continue + } + ints = append(ints, v) + } + var ia pgtype.Int8Array + if err := ia.Set(ints); err != nil { + log.Printf("warn: %v\n", err) + return nil + } + return &ia +} + +func toTextArray(txt string, allowed []string) *pgtype.TextArray { + parts := strings.Split(txt, ",") + var accepted []string + for _, part := range parts { + if part = strings.ToLower(strings.TrimSpace(part)); len(part) == 0 { + continue + } + for _, a := range allowed { + if part == a { + accepted = append(accepted, part) + break + } + } + } + if len(accepted) == 0 { + return nil + } + var ta pgtype.TextArray + if err := ta.Set(accepted); err != nil { + log.Printf("warn: %v\n", err) + return nil + } + return &ta +}
--- a/pkg/controllers/importqueue.go Thu May 09 11:56:11 2019 +0200 +++ b/pkg/controllers/importqueue.go Thu May 09 12:08:02 2019 +0200 @@ -25,7 +25,6 @@ "time" "github.com/gorilla/mux" - "github.com/jackc/pgx/pgtype" "gemma.intevation.de/gemma/pkg/auth" "gemma.intevation.de/gemma/pkg/imports" @@ -88,50 +87,6 @@ DELETE FROM import.imports WHERE id = $1` ) -func toInt8Array(txt string) *pgtype.Int8Array { - parts := strings.Split(txt, ",") - var ints []int64 - for _, part := range parts { - part = strings.TrimSpace(part) - v, err := strconv.ParseInt(part, 10, 64) - if err != nil { - continue - } - ints = append(ints, v) - } - var ia pgtype.Int8Array - if err := ia.Set(ints); err != nil { - log.Printf("warn: %v\n", err) - return nil - } - return &ia -} - -func toTextArray(txt string, allowed []string) *pgtype.TextArray { - parts := strings.Split(txt, ",") - var accepted []string - for _, part := range parts { - if part = strings.ToLower(strings.TrimSpace(part)); len(part) == 0 { - continue - } - for _, a := range allowed { - if part == a { - accepted = append(accepted, part) - break - } - } - } - if len(accepted) == 0 { - return nil - } - var ta pgtype.TextArray - if err := ta.Set(accepted); err != nil { - log.Printf("warn: %v\n", err) - return nil - } - return &ta -} - type filledStmt struct { stmt strings.Builder args []interface{}
--- a/pkg/controllers/printtemplates.go Thu May 09 11:56:11 2019 +0200 +++ b/pkg/controllers/printtemplates.go Thu May 09 12:08:02 2019 +0200 @@ -18,6 +18,7 @@ "database/sql" "encoding/json" "net/http" + "strings" "time" "github.com/gorilla/mux" @@ -32,52 +33,78 @@ listPrintTemplatesSQL = ` SELECT template_name, + template_type::varchar, date_info, country -FROM - users.templates -ORDER BY date_info DESC` +FROM users.templates +` hasPrintTemplateSQL = ` -SELECT true FROM users.templates WHERE template_name = $1` +SELECT true FROM users.templates +WHERE template_name = $1 AND template_type = $2::template_types` deletePrintTemplateSQL = ` -DELETE FROM users.templates WHERE template_name = $1` +DELETE FROM users.templates +WHERE template_name = $1 AND template_type = $2::template_types` selectPrintTemplateSQL = ` -SELECT template_data FROM users.templates WHERE template_name = $1` +SELECT template_data FROM users.templates +WHERE template_name = $1 AND template_type = $2::template_types` insertPrintTemplateSQL = ` -INSERT INTO users.templates (template_name, template_data, country) +INSERT INTO users.templates (template_name, template_type, template_data, country) SELECT $1, - $2, + $2::template_types, + $3, CASE WHEN pg_has_role('sys_admin', 'MEMBER') THEN NULL ELSE users.current_user_country() END` updatePrintTemplateSQL = ` -UPDATE user.templates template_data = $2 WHERE template_name = $1` +UPDATE user.templates template_data = $2 +WHERE template_name = $1 AND template_type = $2::template_types` ) +var templateTypes = []string{"map", "diagram", "report"} + func listPrintTemplates( _ interface{}, req *http.Request, conn *sql.Conn, ) (jr JSONResult, err error) { + ts := mux.Vars(req)["types"] + + if ts == "" { + if ts = req.FormValue("types"); ts == "" { + ts = strings.Join(templateTypes, ",") + } + } + + types := toTextArray(ts, templateTypes) + filter := buildFilterTerm("template_type = ANY($%d) ", types) + + var stmt strings.Builder + var args []interface{} + + stmt.WriteString(listPrintTemplatesSQL) + filter.serialize(&stmt, &args) + stmt.WriteString(" ORDER BY date_info DESC") + + var rows *sql.Rows + if rows, err = conn.QueryContext(req.Context(), stmt.String(), args...); err != nil { + return + } + defer rows.Close() + type template struct { Name string `json:"name"` + Type string `json:"type"` Time models.Time `json:"time"` Country *string `json:"country,omitempty"` } - var rows *sql.Rows - if rows, err = conn.QueryContext(req.Context(), listPrintTemplatesSQL); err != nil { - return - } - defer rows.Close() - templates := []*template{} for rows.Next() { @@ -104,11 +131,12 @@ conn *sql.Conn, ) (jr JSONResult, err error) { - ctx := req.Context() - name := mux.Vars(req)["name"] + vars := mux.Vars(req) + name, typ := vars["name"], vars["type"] + ctx := req.Context() var data pgtype.Bytea - err = conn.QueryRowContext(ctx, selectPrintTemplateSQL, name).Scan(&data) + err = conn.QueryRowContext(ctx, selectPrintTemplateSQL, name, typ).Scan(&data) switch { case err == sql.ErrNoRows: @@ -136,8 +164,9 @@ conn *sql.Conn, ) (jr JSONResult, err error) { - ctx := req.Context() - name := mux.Vars(req)["name"] + vars := mux.Vars(req) + name, typ := vars["name"], vars["type"] + in := input.(*json.RawMessage) if name == "" { @@ -154,6 +183,8 @@ } return } + + ctx := req.Context() var tx *sql.Tx if tx, err = conn.BeginTx(ctx, nil); err != nil { return @@ -161,7 +192,7 @@ defer tx.Rollback() var dummy bool - err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name).Scan(&dummy) + err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name, typ).Scan(&dummy) switch { case err == sql.ErrNoRows: @@ -177,7 +208,7 @@ } data := pgtype.Bytea{Bytes: *in, Status: pgtype.Present} - if _, err = tx.ExecContext(ctx, insertPrintTemplateSQL, name, &data); err != nil { + if _, err = tx.ExecContext(ctx, insertPrintTemplateSQL, name, typ, &data); err != nil { return } @@ -199,9 +230,10 @@ conn *sql.Conn, ) (jr JSONResult, err error) { + vars := mux.Vars(req) + name, typ := vars["name"], vars["type"] + ctx := req.Context() - name := mux.Vars(req)["name"] - var tx *sql.Tx if tx, err = conn.BeginTx(ctx, nil); err != nil { return @@ -209,7 +241,7 @@ defer tx.Rollback() var dummy bool - err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name).Scan(&dummy) + err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name, typ).Scan(&dummy) switch { case err == sql.ErrNoRows: @@ -228,7 +260,7 @@ return } - if _, err = tx.ExecContext(ctx, deletePrintTemplateSQL, name); err != nil { + if _, err = tx.ExecContext(ctx, deletePrintTemplateSQL, name, typ); err != nil { return } @@ -251,8 +283,9 @@ conn *sql.Conn, ) (jr JSONResult, err error) { - ctx := req.Context() - name := mux.Vars(req)["name"] + vars := mux.Vars(req) + name, typ := vars["name"], vars["type"] + in := input.(*json.RawMessage) if name == "" { @@ -269,6 +302,8 @@ } return } + + ctx := req.Context() var tx *sql.Tx if tx, err = conn.BeginTx(ctx, nil); err != nil { return @@ -276,7 +311,7 @@ defer tx.Rollback() var dummy bool - err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name).Scan(&dummy) + err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name, typ).Scan(&dummy) switch { case err == sql.ErrNoRows: @@ -296,7 +331,7 @@ } data := pgtype.Bytea{Bytes: *in, Status: pgtype.Present} - if _, err = tx.ExecContext(ctx, updatePrintTemplateSQL, name, &data); err != nil { + if _, err = tx.ExecContext(ctx, updatePrintTemplateSQL, name, typ, &data); err != nil { return }
--- a/pkg/controllers/routes.go Thu May 09 11:56:11 2019 +0200 +++ b/pkg/controllers/routes.go Thu May 09 12:08:02 2019 +0200 @@ -101,25 +101,31 @@ }).Methods(http.MethodGet) // Print templates - api.Handle("/templates/print", any(&JSONHandler{ + api.Handle("/templates", any(&JSONHandler{ Handle: listPrintTemplates, })).Methods(http.MethodGet) - api.Handle("/templates/print/{name}", any(&JSONHandler{ + tTypes := "{type:" + strings.Join(templateTypes, "|") + "}" + + api.Handle("/templates/"+tTypes, any(&JSONHandler{ + Handle: listPrintTemplates, + })).Methods(http.MethodGet) + + api.Handle("/templates/"+tTypes+"/{name}", any(&JSONHandler{ Handle: fetchPrintTemplate, })).Methods(http.MethodGet) - api.Handle("/templates/print/{name}", waterwayAdmin(&JSONHandler{ + api.Handle("/templates/"+tTypes+"/{name}", waterwayAdmin(&JSONHandler{ Input: func(*http.Request) interface{} { return &json.RawMessage{} }, Handle: createPrintTemplate, Limit: maxPrintTemplateSize, })).Methods(http.MethodPost) - api.Handle("/templates/print/{name}", waterwayAdmin(&JSONHandler{ + api.Handle("/templates/"+tTypes+"/{name}", waterwayAdmin(&JSONHandler{ Handle: deletePrintTemplate, })).Methods(http.MethodDelete) - api.Handle("/templates/print/{name}", waterwayAdmin(&JSONHandler{ + api.Handle("/templates/"+tTypes+"/{name}", waterwayAdmin(&JSONHandler{ Input: func(*http.Request) interface{} { return &json.RawMessage{} }, Handle: updatePrintTemplate, Limit: maxPrintTemplateSize,
--- a/schema/gemma.sql Thu May 09 11:56:11 2019 +0200 +++ b/schema/gemma.sql Thu May 09 12:08:02 2019 +0200 @@ -220,7 +220,7 @@ country char(2) REFERENCES countries, template_data bytea NOT NULL, date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, - UNIQUE (template_name, country) + UNIQUE (template_name, template_type, country) ) CREATE TRIGGER templates_date_info BEFORE UPDATE ON templates FOR EACH ROW EXECUTE PROCEDURE update_date_info()