Mercurial > gemma
annotate pkg/controllers/report.go @ 5349:a352a695b69c extented-report
XLSX templater: Added column to coord conversions.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 21 Jun 2021 18:10:49 +0200 |
parents | 6c0f40676984 |
children | 699048c86848 |
rev | line source |
---|---|
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 // Copyright (C) 2021 by via donau |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package controllers |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
5333
6c0f40676984
Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5324
diff
changeset
|
17 "database/sql" |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "fmt" |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 "log" |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 "net/http" |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 "os" |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 "path/filepath" |
5324
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
23 "sort" |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
24 "strings" |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 "gemma.intevation.de/gemma/pkg/config" |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 "gemma.intevation.de/gemma/pkg/middleware" |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 "gemma.intevation.de/gemma/pkg/xlsx" |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 |
5324
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
30 mw "gemma.intevation.de/gemma/pkg/middleware" |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
31 |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 "github.com/360EntSecGroup-Skylar/excelize/v2" |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 "github.com/gorilla/mux" |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 ) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 |
5324
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
36 func listReports(req *http.Request) (jr mw.JSONResult, err error) { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
37 path := config.ReportPath() |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
38 if path == "" { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
39 err = mw.JSONError{ |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
40 Code: http.StatusNotFound, |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
41 Message: http.StatusText(http.StatusNotFound), |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
42 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
43 return |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
44 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
45 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
46 // This would be easier with Go 1.16+. |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
47 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
48 dir, err := os.Open(path) |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
49 if err != nil { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
50 log.Printf("error: %v\n", err) |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
51 err = mw.JSONError{ |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
52 Code: http.StatusInternalServerError, |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
53 Message: "Listing report templates failed.", |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
54 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
55 return |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
56 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
57 defer dir.Close() |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
58 files, err := dir.Readdirnames(-1) |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
59 if err != nil { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
60 log.Printf("error: %v\n", err) |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
61 err = mw.JSONError{ |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
62 Code: http.StatusInternalServerError, |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
63 Message: "Listing report templates failed.", |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
64 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
65 return |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
66 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
67 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
68 pairs := map[string]int{} |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
69 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
70 all: |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
71 for _, file := range files { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
72 var mask int |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
73 switch { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
74 case strings.HasSuffix(file, ".xlsx"): |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
75 mask = 1 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
76 case strings.HasSuffix(file, ".yaml"): |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
77 mask = 2 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
78 default: |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
79 continue all |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
80 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
81 basename := filepath.Base(file) |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
82 name := strings.TrimSuffix(basename, filepath.Ext(basename)) |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
83 pairs[name] |= mask |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
84 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
85 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
86 var reports []string |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
87 for name, mask := range pairs { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
88 if mask == 3 { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
89 reports = append(reports, name) |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
90 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
91 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
92 sort.Strings(reports) |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
93 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
94 out := struct { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
95 Reports []string `json:"reports"` |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
96 }{ |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
97 Reports: reports, |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
98 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
99 jr = mw.JSONResult{Result: out} |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
100 return |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
101 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
102 |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 func report(rw http.ResponseWriter, req *http.Request) { |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 path := config.ReportPath() |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
106 if path == "" { |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
107 http.NotFound(rw, req) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
108 return |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
109 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
110 |
5322
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
111 if stat, err := os.Stat(path); err != nil { |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
112 if os.IsNotExist(err) { |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
113 log.Printf("error: report dir '%s' does not exists.\n", path) |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
114 http.NotFound(rw, req) |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
115 } else { |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
116 log.Printf("error: %v\n", err) |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
117 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
118 } |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
119 return |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
120 } else if !stat.Mode().IsDir() { |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
121 log.Printf("error: report dir '%s' is not a directory.\n", path) |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
122 http.NotFound(rw, req) |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
123 return |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
125 |
5322
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
126 vars := mux.Vars(req) |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
127 name := vars["name"] |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
128 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
129 xlsxFilename := filepath.Join(path, name+".xlsx") |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
130 yamlFilename := filepath.Join(path, name+".yaml") |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
131 |
5322
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
132 for _, check := range []string{xlsxFilename, yamlFilename} { |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
133 if _, err := os.Stat(check); err != nil { |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
134 if os.IsNotExist(err) { |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
135 http.NotFound(rw, req) |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
136 } else { |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
137 log.Printf("error: %v\n", err) |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
138 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
139 } |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
140 return |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
141 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
142 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
143 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
144 template, err := excelize.OpenFile(xlsxFilename) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
145 if err != nil { |
5322
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
146 log.Printf("error: %v\n", err) |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
147 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
148 return |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
149 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
150 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
151 action, err := xlsx.ActionFromFile(yamlFilename) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
152 if err != nil { |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
153 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
154 log.Printf("error: %v\n", err) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
155 return |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
156 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
157 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
158 ctx := req.Context() |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
159 conn := middleware.GetDBConn(req) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
160 |
5333
6c0f40676984
Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5324
diff
changeset
|
161 tx, err := conn.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) |
6c0f40676984
Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5324
diff
changeset
|
162 defer tx.Rollback() |
6c0f40676984
Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5324
diff
changeset
|
163 |
6c0f40676984
Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5324
diff
changeset
|
164 if err := action.Execute(ctx, tx, template); err != nil { |
5322
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
165 log.Printf("error: %v\n", err) |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
166 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
167 return |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
168 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
169 rw.Header().Set( |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
170 "Content-Disposition", |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
171 fmt.Sprintf("attachment; filename=%s.xlsx", name)) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
172 rw.Header().Set( |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
173 "Content-Type", |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
174 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
175 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
176 if _, err := template.WriteTo(rw); err != nil { |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
177 log.Printf("error: %v\n", err) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
178 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
179 } |