From 71ebf98ce7f6a350d993750ee9676b62edfb9a64 Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Sun, 12 Oct 2014 21:17:55 -0700 Subject: [PATCH] Guess timezone. --- app/account.go | 4 +++- app/githop.go | 27 +++++++++++++++++++++++++++ app/templates/index.html | 20 ++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/app/account.go b/app/account.go index 7ebebc7..6d5bb02 100644 --- a/app/account.go +++ b/app/account.go @@ -21,6 +21,7 @@ type Account struct { OAuthToken oauth.Token `datastore:"-,"` TimezoneName string `datastore:",noindex"` TimezoneLocation *time.Location `datastore:"-,"` + HasTimezoneSet bool `datastore:"-,"` ExcludedRepoIds []int `datastore:",noindex"` DigestEmailAddress string Frequency string @@ -48,7 +49,8 @@ func initAccount(account *Account) error { if err != nil { return err } - if len(account.TimezoneName) == 0 { + account.HasTimezoneSet = len(account.TimezoneName) > 0 + if !account.HasTimezoneSet { account.TimezoneName = "America/Los_Angeles" } if len(account.Frequency) == 0 { diff --git a/app/githop.go b/app/githop.go index 33f12d0..979d588 100644 --- a/app/githop.go +++ b/app/githop.go @@ -51,6 +51,7 @@ func init() { router.HandleFunc("/account/settings", settingsHandler).Name("settings").Methods("GET") router.HandleFunc("/account/settings", saveSettingsHandler).Name("save-settings").Methods("POST") + router.HandleFunc("/account/set-initial-timezone", setInitialTimezoneHandler).Name("set-initial-timezone").Methods("POST") router.HandleFunc("/account/delete", deleteAccountHandler).Name("delete-account").Methods("POST") router.HandleFunc("/admin/digest", digestAdminHandler) @@ -231,6 +232,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { var data = map[string]interface{}{ "User": user, "SettingsSummary": settingsSummary, + "DetectTimezone": !account.HasTimezoneSet, } if err := templates["index"].Execute(w, data); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -513,6 +515,31 @@ func saveSettingsHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, settingsUrl.String(), http.StatusFound) } +func setInitialTimezoneHandler(w http.ResponseWriter, r *http.Request) { + session, _ := sessionStore.Get(r, sessionConfig.CookieName) + userId := session.Values[sessionConfig.UserIdKey].(int) + c := appengine.NewContext(r) + account, err := getAccount(c, userId) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + timezoneName := r.FormValue("timezone_name") + _, err = time.LoadLocation(timezoneName) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + account.TimezoneName = timezoneName + + err = account.Put(c) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + func deleteAccountHandler(w http.ResponseWriter, r *http.Request) { session, _ := sessionStore.Get(r, sessionConfig.CookieName) userId := session.Values[sessionConfig.UserIdKey].(int) diff --git a/app/templates/index.html b/app/templates/index.html index 09d06c2..fb73e8b 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -29,4 +29,24 @@ Change settings

+{{if .DetectTimezone }} + + + + +{{end}} + {{end}}