Add/edit notifications
This commit is contained in:
parent
1ede36b8aa
commit
c7ad2aa1b6
19 changed files with 531 additions and 11 deletions
113
main.go
113
main.go
|
|
@ -25,6 +25,7 @@ import (
|
|||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -152,6 +153,8 @@ func main() { // {{{
|
|||
service.Register("/configuration", false, false, pageConfiguration)
|
||||
service.Register("/configuration/theme", false, false, actionConfigurationTheme)
|
||||
service.Register("/configuration/timezone", false, false, actionConfigurationTimezone)
|
||||
service.Register("/configuration/notification", false, false, pageConfigurationNotification)
|
||||
service.Register("/configuration/notification/update/{prio}", false, false, actionConfigurationNotificationUpdate)
|
||||
service.Register("/entry/{datapoint}", false, false, actionEntryDatapoint)
|
||||
|
||||
go nodataLoop()
|
||||
|
|
@ -379,9 +382,9 @@ func getPage(layout, page string) (tmpl *template.Template, err error) { // {{{
|
|||
filenames = append(filenames, componentFilenames...)
|
||||
logger.Info("template", "op", "parse", "layout", layout, "page", page, "filenames", filenames)
|
||||
if flagDev {
|
||||
tmpl, err = template.New("main.gotmpl").Funcs(funcMap).ParseFS(os.DirFS("."), filenames...)
|
||||
tmpl, err = template.New(layout+".gotmpl").Funcs(funcMap).ParseFS(os.DirFS("."), filenames...)
|
||||
} else {
|
||||
tmpl, err = template.New("main.gotmpl").Funcs(funcMap).ParseFS(viewFS, filenames...)
|
||||
tmpl, err = template.New(layout+".gotmpl").Funcs(funcMap).ParseFS(viewFS, filenames...)
|
||||
}
|
||||
if err != nil {
|
||||
err = werr.Wrap(err).Log()
|
||||
|
|
@ -1036,7 +1039,9 @@ func pageConfiguration(w http.ResponseWriter, r *http.Request, _ *session.T) { /
|
|||
PAGE: "configuration",
|
||||
CONFIG: smonConfig.Settings,
|
||||
Data: map[string]any{
|
||||
"Areas": areas,
|
||||
"Areas": areas,
|
||||
"NotificationServices": notificationManager.Services(),
|
||||
"AvailableServices": notification.AvailableServices(),
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -1071,3 +1076,105 @@ func actionConfigurationTimezone(w http.ResponseWriter, r *http.Request, _ *sess
|
|||
w.Header().Add("Location", "/configuration")
|
||||
w.WriteHeader(302)
|
||||
} // }}}
|
||||
func pageConfigurationNotification(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
|
||||
// This function is either receiving a type when creating a new service,
|
||||
// or a prio when editing an existing.
|
||||
notificationType := r.URL.Query().Get("type")
|
||||
prioStr := r.URL.Query().Get("prio")
|
||||
|
||||
var service notification.Service
|
||||
if notificationType != "" {
|
||||
// Create a new instance of the selected notification type.
|
||||
service = notification.GetInstance(notificationType)
|
||||
} else {
|
||||
// Find the existing service for editing.
|
||||
prio, err := strconv.Atoi(prioStr)
|
||||
if err != nil {
|
||||
httpError(w, werr.Wrap(err).Log())
|
||||
return
|
||||
}
|
||||
service = *notificationManager.GetService(prio)
|
||||
}
|
||||
|
||||
if service == nil {
|
||||
data := struct {
|
||||
typ string
|
||||
prio string
|
||||
}{notificationType, prioStr}
|
||||
err := fmt.Errorf("Invalid service")
|
||||
pageError(w, "/configuration", werr.Wrap(err).WithData(data).Log())
|
||||
return
|
||||
}
|
||||
|
||||
// Make it easier for the user by initiating the prio field
|
||||
// for new notifications to the highest prio + 1.
|
||||
if notificationType != "" {
|
||||
prio := 0
|
||||
for _, svc := range notificationManager.Services() {
|
||||
prio = max(prio, svc.GetPrio()+1)
|
||||
}
|
||||
service.SetPrio(prio)
|
||||
}
|
||||
|
||||
page := Page{
|
||||
LAYOUT: "notification",
|
||||
PAGE: "notification/" + strings.ToLower(service.GetType()),
|
||||
CONFIG: smonConfig.Settings,
|
||||
Data: map[string]any{
|
||||
"Service": service,
|
||||
},
|
||||
}
|
||||
|
||||
page.Render(w, r)
|
||||
} // }}}
|
||||
func actionConfigurationNotificationUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
|
||||
prioStr := r.PathValue("prio")
|
||||
prio, err := strconv.Atoi(prioStr)
|
||||
if err != nil {
|
||||
pageError(w, "/configuration", werr.Wrap(err).Log())
|
||||
return
|
||||
}
|
||||
|
||||
// prio -1 means a new service, not existing in database.
|
||||
var svc *notification.Service
|
||||
if prio == -1 {
|
||||
emptyService := notification.GetInstance(r.PostFormValue("type"))
|
||||
svc = &emptyService
|
||||
} else {
|
||||
svc = notificationManager.GetService(prio)
|
||||
if svc == nil {
|
||||
pageError(w, "/configuration", werr.New("Service with prio %d not found", prio).Log())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// The service is given all data to give it a chance to
|
||||
// validate and throwing errors.
|
||||
err = r.ParseForm()
|
||||
if err != nil {
|
||||
pageError(w, "/configuration", werr.Wrap(err).Log())
|
||||
return
|
||||
}
|
||||
|
||||
err = (*svc).Update(r.PostForm)
|
||||
if err != nil {
|
||||
pageError(w, "/configuration", werr.Wrap(err).Log())
|
||||
return
|
||||
}
|
||||
|
||||
var created bool
|
||||
created, err = UpdateNotificationService(*svc)
|
||||
if err != nil {
|
||||
pageError(w, "/configuration", werr.Wrap(err).Log())
|
||||
return
|
||||
}
|
||||
|
||||
(*svc).Commit()
|
||||
|
||||
if created {
|
||||
notificationManager.AddService(*svc)
|
||||
}
|
||||
|
||||
w.Header().Add("Location", "/configuration")
|
||||
w.WriteHeader(302)
|
||||
} // }}}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue