Mercurial > gemma
annotate pkg/imports/report.go @ 5417:2d294ad81241 marking-single-beam
Dont loose Z values in markings.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 07 Jul 2021 18:14:04 +0200 |
parents | 8e30b926b94d |
children | 699048c86848 |
rev | line source |
---|---|
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
5387
8e30b926b94d
Added an import to run update stats scripts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5377
diff
changeset
|
7 // Copyright (C) 2021 by via donau |
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package imports |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
17 "bytes" |
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "context" |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 "database/sql" |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 "errors" |
5334
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
21 "fmt" |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
22 "log" |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
23 "os" |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
24 "path/filepath" |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
25 "strings" |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
26 "text/template" |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
27 "time" |
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 |
5376
e09e003948c7
Decouple and enforce roles in creating scheduled imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5375
diff
changeset
|
29 "gemma.intevation.de/gemma/pkg/auth" |
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 "gemma.intevation.de/gemma/pkg/common" |
5334
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
31 "gemma.intevation.de/gemma/pkg/config" |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
32 "gemma.intevation.de/gemma/pkg/misc" |
5329
795a0a0b5047
Embed scheduling type into report import to ensure persistence of schedule data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5328
diff
changeset
|
33 "gemma.intevation.de/gemma/pkg/models" |
5334
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
34 "gemma.intevation.de/gemma/pkg/xlsx" |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
35 |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
36 "github.com/360EntSecGroup-Skylar/excelize/v2" |
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 ) |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 type Report struct { |
5329
795a0a0b5047
Embed scheduling type into report import to ensure persistence of schedule data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5328
diff
changeset
|
40 models.QueueConfigurationType |
5377
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5376
diff
changeset
|
41 Name models.SafePath `json:"name"` |
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 } |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 const ReportJobKind JobKind = "report" |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 type reportJobCreator struct{} |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 |
5336
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
48 const ( |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
49 selectReportUsersSQL = ` |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
50 SELECT username, email_address |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
51 FROM users.list_users |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
52 WHERE report_reciever |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
53 ORDER BY country, username` |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
54 |
5336
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
55 selectCurrentUserSQL = ` |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
56 SELECT current_user, email_address |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
57 FROM users.list_users |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
58 WHERE username = current_user` |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
59 ) |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
60 |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
61 var reportMailTmpl = template.Must(template.New("report-mail"). |
5336
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
62 Parse(`Dear {{ .Receiver }} |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
63 |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
64 this is an automatically generated report from the Gemma system. |
5369
4d126cf58fbc
Re-phrased the text of the email send for reports a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5365
diff
changeset
|
65 You got this mail because you are listed as a report receiver. |
4d126cf58fbc
Re-phrased the text of the email send for reports a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5365
diff
changeset
|
66 If you received it without consent please |
5336
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
67 contact {{ .Admin }} under {{ .AdminEmail }}. |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
68 |
5369
4d126cf58fbc
Re-phrased the text of the email send for reports a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5365
diff
changeset
|
69 Find attached {{ .Attachment }} containing the {{ .Report }} report from {{ .When }}. |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
70 |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
71 Kind Regards`)) |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
72 |
5328
bc8c082487b2
Fixed compile errors. :-/
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5326
diff
changeset
|
73 func init() { RegisterJobCreator(ReportJobKind, reportJobCreator{}) } |
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 func (reportJobCreator) Description() string { return "report" } |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 func (reportJobCreator) AutoAccept() bool { return true } |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 func (reportJobCreator) Create() Job { return new(Report) } |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 func (reportJobCreator) Depends() [2][]string { return [2][]string{{}, {}} } |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 func (reportJobCreator) StageDone(context.Context, *sql.Tx, int64, Feedback) error { |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 return nil |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 } |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 |
5376
e09e003948c7
Decouple and enforce roles in creating scheduled imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5375
diff
changeset
|
87 // RequiresRoles enforces to be a sys_admin to run this . |
e09e003948c7
Decouple and enforce roles in creating scheduled imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5375
diff
changeset
|
88 func (*Report) RequiresRoles() auth.Roles { return auth.Roles{"sys_admin"} } |
e09e003948c7
Decouple and enforce roles in creating scheduled imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5375
diff
changeset
|
89 |
5377
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5376
diff
changeset
|
90 func (r *Report) Description() (string, error) { return string(r.Name), nil } |
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 |
5328
bc8c082487b2
Fixed compile errors. :-/
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5326
diff
changeset
|
92 func (*Report) CleanUp() error { return nil } |
bc8c082487b2
Fixed compile errors. :-/
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5326
diff
changeset
|
93 |
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 func (r *Report) MarshalAttributes(attrs common.Attributes) error { |
5329
795a0a0b5047
Embed scheduling type into report import to ensure persistence of schedule data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5328
diff
changeset
|
95 if err := r.QueueConfigurationType.MarshalAttributes(attrs); err != nil { |
795a0a0b5047
Embed scheduling type into report import to ensure persistence of schedule data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5328
diff
changeset
|
96 return err |
795a0a0b5047
Embed scheduling type into report import to ensure persistence of schedule data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5328
diff
changeset
|
97 } |
5377
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5376
diff
changeset
|
98 attrs.Set("name", string(r.Name)) |
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
99 return nil |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
100 } |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
102 func (r *Report) UnmarshalAttributes(attrs common.Attributes) error { |
5329
795a0a0b5047
Embed scheduling type into report import to ensure persistence of schedule data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5328
diff
changeset
|
103 if err := r.QueueConfigurationType.UnmarshalAttributes(attrs); err != nil { |
795a0a0b5047
Embed scheduling type into report import to ensure persistence of schedule data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5328
diff
changeset
|
104 return err |
795a0a0b5047
Embed scheduling type into report import to ensure persistence of schedule data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5328
diff
changeset
|
105 } |
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
106 name, found := attrs.Get("name") |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
107 if !found { |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
108 return errors.New("missing 'name' attribute") |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
109 } |
5377
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5376
diff
changeset
|
110 r.Name = models.SafePath(name) |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5376
diff
changeset
|
111 if !r.Name.Valid() { |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5376
diff
changeset
|
112 return fmt.Errorf("'%s' is not a safe path", name) |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5376
diff
changeset
|
113 } |
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 return nil |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 } |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
116 |
5334
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
117 func (r *Report) loadTemplate() (*excelize.File, *xlsx.Action, error) { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
118 path := config.ReportPath() |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
119 if path == "" { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
120 return nil, nil, errors.New("no report dir configured") |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
121 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
122 |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
123 if stat, err := os.Stat(path); err != nil { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
124 if os.IsNotExist(err) { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
125 return nil, nil, fmt.Errorf("report dir '%s' does not exists", path) |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
126 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
127 return nil, nil, err |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
128 } else if !stat.Mode().IsDir() { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
129 return nil, nil, fmt.Errorf("report dir '%s' is not a directory", path) |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
130 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
131 |
5377
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5376
diff
changeset
|
132 xlsxFilename := filepath.Join(path, string(r.Name)+".xlsx") |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5376
diff
changeset
|
133 yamlFilename := filepath.Join(path, string(r.Name)+".yaml") |
5334
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
134 |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
135 for _, check := range []string{xlsxFilename, yamlFilename} { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
136 if _, err := os.Stat(check); err != nil { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
137 if os.IsNotExist(err) { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
138 return nil, nil, fmt.Errorf("'%s' does not exists", check) |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
139 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
140 return nil, nil, err |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
141 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
142 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
143 |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
144 template, err := excelize.OpenFile(xlsxFilename) |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
145 if err != nil { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
146 return nil, nil, err |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
147 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
148 |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
149 action, err := xlsx.ActionFromFile(yamlFilename) |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
150 if err != nil { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
151 return nil, nil, err |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
152 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
153 |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
154 return template, action, nil |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
155 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
156 |
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
157 func (r *Report) Do( |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
158 ctx context.Context, |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
159 importID int64, |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
160 conn *sql.Conn, |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
161 feedback Feedback, |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
162 ) (interface{}, error) { |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
163 |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
164 start := time.Now() |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
165 |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
166 feedback.Info("Generating report %s.", r.Name) |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
167 |
5334
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
168 template, action, err := r.loadTemplate() |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
169 if err != nil { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
170 return nil, err |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
171 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
172 |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
173 tx, err := conn.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
174 if err != nil { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
175 return nil, err |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
176 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
177 defer tx.Rollback() |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
178 |
5336
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
179 // Fetch receivers |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
180 var users []misc.EmailReceiver |
5334
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
181 |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
182 if err := func() error { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
183 rows, err := tx.QueryContext(ctx, selectReportUsersSQL) |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
184 if err != nil { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
185 return err |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
186 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
187 defer rows.Close() |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
188 |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
189 for rows.Next() { |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
190 var u misc.EmailReceiver |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
191 if err := rows.Scan(&u.Name, &u.Address); err != nil { |
5334
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
192 return err |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
193 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
194 users = append(users, u) |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
195 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
196 return rows.Err() |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
197 }(); err != nil { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
198 return nil, err |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
199 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
200 |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
201 if len(users) == 0 { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
202 feedback.Warn("No users found to send reports to.") |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
203 return nil, nil |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
204 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
205 |
5336
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
206 // Fetch admin who is responsible for the report. |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
207 var admin misc.EmailReceiver |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
208 if err := tx.QueryRowContext( |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
209 ctx, selectCurrentUserSQL).Scan(&admin.Name, &admin.Address); err != nil { |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
210 log.Printf("error: Cannot find sender: %v\n") |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
211 return nil, fmt.Errorf("cannot find sender: %v", err) |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
212 } |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
213 |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
214 // Generate the actual report. |
5334
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
215 if err := action.Execute(ctx, tx, template); err != nil { |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
216 log.Printf("error: %v\n", err) |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
217 return nil, fmt.Errorf("Generating report failed: %v", err) |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
218 } |
45805c454436
Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5329
diff
changeset
|
219 |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
220 var buf bytes.Buffer |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
221 if _, err := template.WriteTo(&buf); err != nil { |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
222 log.Printf("error: %v\n", err) |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
223 return nil, fmt.Errorf("generating report failed: %v", err) |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
224 } |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
225 |
5371
0de64b3017ac
Logging cosmetics for the report import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5369
diff
changeset
|
226 feedback.Info("Sending report to %d receiver(s).", len(users)) |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
227 |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
228 now := start.UTC().Format("2006-01-02") |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
229 |
5377
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5376
diff
changeset
|
230 attached := string(r.Name) + "-" + now + ".xlsx" |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
231 |
5336
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
232 body := func(u misc.EmailReceiver) (string, error) { |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
233 fill := struct { |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
234 Receiver string |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
235 Attachment string |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
236 Report string |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
237 When string |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
238 Admin string |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
239 AdminEmail string |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
240 }{ |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
241 Receiver: u.Name, |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
242 Attachment: attached, |
5377
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5376
diff
changeset
|
243 Report: string(r.Name), |
5336
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
244 When: now, |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
245 Admin: admin.Name, |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
246 AdminEmail: admin.Address, |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
247 } |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
248 var sb strings.Builder |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
249 if err := reportMailTmpl.Execute(&sb, &fill); err != nil { |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
250 return "", err |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
251 } |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
252 return sb.String(), nil |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
253 } |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
254 |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
255 errorHandler := func(r misc.EmailReceiver, err error) error { |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
256 // We do not terminate the sending of the emails if |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
257 // sending failed. We only log it. |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
258 feedback.Warn("Sending report to %s failed: %v", r.Name, err) |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
259 return nil |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
260 } |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
261 |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
262 if err := misc.SendMailToAll( |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
263 users, |
5377
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5376
diff
changeset
|
264 "Report "+string(r.Name)+" from "+now, |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
265 body, |
5336
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
266 []misc.EmailAttachment{{ |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
267 Name: attached, |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
268 Content: buf.Bytes(), |
2ec8a34ae683
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5335
diff
changeset
|
269 }}, |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
270 errorHandler, |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
271 ); err != nil { |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
272 return nil, err |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
273 } |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
274 |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
275 feedback.Info("Generating and sending report took %v.", |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5334
diff
changeset
|
276 time.Since(start)) |
5326
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
277 |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
278 return nil, nil |
96ceb150ea46
Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
279 } |