From 22f6b6a4132643d814cb4029c2091ec09e489341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Thu, 27 Jun 2024 10:02:11 +0200 Subject: [PATCH] Add update of timezone setting --- configuration.go | 6 ++++++ main.go | 20 ++++++++++++++++++++ views/pages/configuration.gotmpl | 8 +++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/configuration.go b/configuration.go index 46002d6..bf83893 100644 --- a/configuration.go +++ b/configuration.go @@ -54,3 +54,9 @@ func (cfg *Configuration) SetTheme(theme string) (err error) { _, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='THEME'`, theme) return } + +func (cfg *Configuration) SetTimezone(tz string) (err error) { + cfg.Settings["TIMEZONE"] = tz + _, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='TIMEZONE'`, tz) + return +} diff --git a/main.go b/main.go index 5ea259f..b462b08 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( "io/fs" "log/slog" "net/http" + "net/url" "os" "path" "slices" @@ -149,6 +150,7 @@ func main() { // {{{ service.Register("/configuration", false, false, pageConfiguration) service.Register("/configuration/theme", false, false, actionConfigurationTheme) + service.Register("/configuration/timezone", false, false, actionConfigurationTimezone) service.Register("/entry/{datapoint}", false, false, actionEntryDatapoint) go nodataLoop() @@ -1021,3 +1023,21 @@ func actionConfigurationTheme(w http.ResponseWriter, r *http.Request, _ *session w.Header().Add("Location", "/configuration") w.WriteHeader(302) } // }}} +func actionConfigurationTimezone(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ + timezone := r.FormValue("timezone") + + _, err := time.LoadLocation(timezone) + if err != nil { + pageError(w, "/configuration", werr.Wrap(err).Log()) + return + } + + err = smonConfig.SetTimezone(timezone) + if err != nil { + pageError(w, "/configuration", werr.Wrap(err).Log()) + return + } + + w.Header().Add("Location", "/configuration") + w.WriteHeader(302) +} // }}} diff --git a/views/pages/configuration.gotmpl b/views/pages/configuration.gotmpl index 5129f2e..3a9de49 100644 --- a/views/pages/configuration.gotmpl +++ b/views/pages/configuration.gotmpl @@ -95,11 +95,17 @@

Theme

-
+
+

Timezone

+
+ + +
+ {{ end }}