annotate pkg/imports/report.go @ 5670:b75d0b303328

Various fixes and improvements of gauges import: - Allow update of erased data (and thereby set erased to false) - Fix source_organization to work with ERDMS2 - Give ISRS of new and updated gauges in summary - Fixed reference of null pointers if revlevels are missing - Fixed reference of null pointer on update errors - Added ISRS to reference_code warning
author Sascha Wilde <wilde@sha-bang.de>
date Fri, 08 Dec 2023 17:29:56 +0100
parents 1222b777f51f
children 2dd155cc95ec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 "os"
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
23 "path/filepath"
5335
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
24 "strings"
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
25 "text/template"
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
26 "time"
5326
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27
5376
e09e003948c7 Decouple and enforce roles in creating scheduled imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5375
diff changeset
28 "gemma.intevation.de/gemma/pkg/auth"
5326
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 "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
30 "gemma.intevation.de/gemma/pkg/config"
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5478
diff changeset
31 "gemma.intevation.de/gemma/pkg/log"
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
5478
699048c86848 Updated 3rd-party libs of the gemma server to the latest bug fix release.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5387
diff changeset
36 "github.com/xuri/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
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5600
diff changeset
39 // Report is a job to generate a report and send emails to the
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5600
diff changeset
40 // receivers.
5326
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 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
42 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
43 Name models.SafePath `json:"name"`
5326
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 }
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5600
diff changeset
46 // ReportJobKind is the unique name of this import job type.
5326
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 const ReportJobKind JobKind = "report"
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 type reportJobCreator struct{}
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50
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
51 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
52 selectReportUsersSQL = `
5335
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
53 SELECT username, email_address
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
54 FROM users.list_users
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
55 WHERE report_reciever
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
56 ORDER BY country, username`
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
57
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
58 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
59 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
60 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
61 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
62 )
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
63
5335
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
64 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
65 Parse(`Dear {{ .Receiver }}
5335
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
66
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
67 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
68 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
69 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
70 contact {{ .Admin }} under {{ .AdminEmail }}.
5335
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
71
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
72 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
73
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
74 Kind Regards`))
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
75
5328
bc8c082487b2 Fixed compile errors. :-/
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5326
diff changeset
76 func init() { RegisterJobCreator(ReportJobKind, reportJobCreator{}) }
5326
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 func (reportJobCreator) Description() string { return "report" }
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 func (reportJobCreator) AutoAccept() bool { return true }
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 func (reportJobCreator) Create() Job { return new(Report) }
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 func (reportJobCreator) Depends() [2][]string { return [2][]string{{}, {}} }
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 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
87 return nil
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 }
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89
5376
e09e003948c7 Decouple and enforce roles in creating scheduled imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5375
diff changeset
90 // 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
91 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
92
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5600
diff changeset
93 // Description gives a short info about relevant facts of this import.
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
94 func (r *Report) Description([]string) (string, error) { return string(r.Name), nil }
5326
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5600
diff changeset
96 // CleanUp is an empty implementation.
5328
bc8c082487b2 Fixed compile errors. :-/
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5326
diff changeset
97 func (*Report) CleanUp() error { return nil }
bc8c082487b2 Fixed compile errors. :-/
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5326
diff changeset
98
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5600
diff changeset
99 // MarshalAttributes implements a DB marshaling of this job.
5326
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 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
101 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
102 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
103 }
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
104 attrs.Set("name", string(r.Name))
5326
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 return nil
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 }
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5600
diff changeset
108 // UnmarshalAttributes implements a DB unmarshaling of this job.
5326
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 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
110 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
111 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
112 }
5326
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 name, found := attrs.Get("name")
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 if !found {
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 return errors.New("missing 'name' attribute")
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 }
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
117 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
118 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
119 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
120 }
5326
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 return nil
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 }
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123
5334
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
124 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
125 path := config.ReportPath()
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
126 if path == "" {
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, 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
128 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
129
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
130 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
131 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
132 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
133 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
134 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
135 } 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
136 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
137 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
138
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
139 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
140 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
141
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
142 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
143 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
144 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
145 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
146 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
147 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
148 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
149 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
150
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
151 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
152 if err != nil {
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
153 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
154 }
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 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
157 if err != nil {
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
158 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
159 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
160
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
161 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
162 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
163
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5600
diff changeset
164 // Do executes the actual report generation.
5326
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 func (r *Report) Do(
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166 ctx context.Context,
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167 importID int64,
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
168 conn *sql.Conn,
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
169 feedback Feedback,
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
170 ) (interface{}, error) {
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171
5335
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
172 start := time.Now()
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
173
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
174 feedback.Info("Generating report %s.", r.Name)
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
175
5334
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
176 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
177 if err != nil {
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
178 return nil, err
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
179 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
180
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
181 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
182 if err != nil {
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
183 return nil, err
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
184 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
185 defer tx.Rollback()
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
186
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
187 // Fetch receivers
5335
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
188 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
189
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
190 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
191 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
192 if err != nil {
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
193 return err
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
194 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
195 defer rows.Close()
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
196
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
197 for rows.Next() {
5335
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
198 var u misc.EmailReceiver
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
199 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
200 return err
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
201 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
202 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
203 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
204 return rows.Err()
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
205 }(); err != nil {
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
206 return nil, err
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
207 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
208
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
209 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
210 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
211 return nil, nil
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
212 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
213
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
214 // 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
215 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
216 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
217 ctx, selectCurrentUserSQL).Scan(&admin.Name, &admin.Address); err != nil {
5600
9967a78e43f4 Fix format issue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5599
diff changeset
218 log.Errorf("cannot find sender: %v\n", err)
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
219 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
220 }
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
221
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
222 // 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
223 if err := action.Execute(ctx, tx, template); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5478
diff changeset
224 log.Errorf("%v\n", err)
5599
ceecc205011c Fixed error strings (found by staticcheck).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5560
diff changeset
225 return nil, fmt.Errorf("generating report failed: %v", err)
5334
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
226 }
45805c454436 Load users from database who should receive a report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5329
diff changeset
227
5335
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
228 var buf bytes.Buffer
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
229 if _, err := template.WriteTo(&buf); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5478
diff changeset
230 log.Errorf("%v\n", err)
5335
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
231 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
232 }
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
233
5371
0de64b3017ac Logging cosmetics for the report import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5369
diff changeset
234 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
235
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
236 now := start.UTC().Format("2006-01-02")
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
237
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
238 attached := string(r.Name) + "-" + now + ".xlsx"
5335
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
239
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
240 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
241 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
242 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
243 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
244 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
245 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
246 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
247 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
248 }{
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 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
250 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
251 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
252 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
253 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
254 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
255 }
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
256 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
257 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
258 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
259 }
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
260 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
261 }
5335
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
262
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
263 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
264 // 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
265 // sending failed. We only log it.
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
266 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
267 return nil
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
268 }
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
269
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
270 if err := misc.SendMailToAll(
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
271 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
272 "Report "+string(r.Name)+" from "+now,
5335
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
273 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
274 []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
275 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
276 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
277 }},
5335
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
278 errorHandler,
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
279 ); err != nil {
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
280 return nil, err
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
281 }
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
282
dcd5692a2889 Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5334
diff changeset
283 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
284 time.Since(start))
5326
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
285
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
286 return nil, nil
96ceb150ea46 Added infrastructure for report 'import'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
287 }