From f68f1f326a196882383e534ff5166bf91b4e079d Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Sat, 31 Aug 2013 21:52:40 +0200 Subject: Jobs can now be viewed in the job editor. --- jobedit.go | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 2 ++ tpls.go | 2 ++ tpls/jobedit.tpl | 51 +++++++++++++++++++++++++++ tpls/jobs.tpl | 2 +- 5 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 jobedit.go create mode 100644 tpls/jobedit.tpl diff --git a/jobedit.go b/jobedit.go new file mode 100644 index 0000000..ca4e445 --- /dev/null +++ b/jobedit.go @@ -0,0 +1,103 @@ +package main + +import ( + "github.com/gorilla/mux" + "github.com/gorilla/sessions" + "kch42.de/gostuff/mailremind/chronos" + "kch42.de/gostuff/mailremind/model" + "log" + "net/http" +) + +type scheduleTpldata struct { + Start, End string + Count int + UnitIsMinute, UnitIsHour, UnitIsDay, UnitIsWeek, UnitIsMonth, UnitIsYear bool + RepetitionEnabled, EndEnabled bool +} + +const maxSchedules = 10 +const bestTimeFmtEver = "2006-01-02 15:04:05" + +type jobeditTpldata struct { + Error, Success string + Fatal bool + JobID, Subject, Content string + Schedules [maxSchedules]scheduleTpldata +} + +func (jt *jobeditTpldata) fillFromJob(job model.Job, u model.User) { + jt.JobID = job.ID().String() + jt.Subject = job.Subject() + jt.Content = string(job.Content()) + + loc := u.Location() + + for i, chron := range job.Chronos() { + if i == 10 { + log.Printf("Job %s has more than %d Chronos entries!", job.ID(), maxSchedules) + break + } + + schedule := scheduleTpldata{ + Start: chron.Start.In(loc).Format(bestTimeFmtEver), + } + + if f := chron.Freq; f.Count > 0 { + schedule.RepetitionEnabled = true + schedule.Count = int(f.Count) + switch f.Unit { + case chronos.Minute: + schedule.UnitIsMinute = true + case chronos.Hour: + schedule.UnitIsHour = true + case chronos.Day: + schedule.UnitIsDay = true + case chronos.Week: + schedule.UnitIsWeek = true + case chronos.Month: + schedule.UnitIsMonth = true + case chronos.Year: + schedule.UnitIsYear = true + } + } + + if end := chron.End; !end.IsZero() { + schedule.EndEnabled = true + schedule.End = end.In(loc).Format(bestTimeFmtEver) + } + + jt.Schedules[i] = schedule + } +} + +func jobedit(user model.User, sess *sessions.Session, req *http.Request) interface{} { + if user == nil { + return &jobeditTpldata{Error: "You need to be logged in to do that.", Fatal: true} + } + + outdata := new(jobeditTpldata) + + // Try to load job, if given + _id := mux.Vars(req)["ID"] + var job model.Job + if _id != "" { + id, err := db.ParseDBID(_id) + if err != nil { + return &jobeditTpldata{Error: "Job not found", Fatal: true} + } + + if job, err = user.JobByID(id); err != nil { + return &jobeditTpldata{Error: "Job not found", Fatal: true} + } + } + + if req.Method == "POST" { + // TODO: Enable editing... + } + + if job != nil { + outdata.fillFromJob(job, user) + } + return outdata +} diff --git a/main.go b/main.go index d8cec10..1cdccc2 100644 --- a/main.go +++ b/main.go @@ -90,6 +90,8 @@ func main() { router.HandleFunc("/pwreset", mkHttpHandler(pwreset, tplPwreset)) router.HandleFunc("/forgotpw", mkHttpHandler(forgotpw, tplForgotpw)) router.HandleFunc("/jobs", mkHttpHandler(jobs, tplJobs)) + router.HandleFunc("/jobedit", mkHttpHandler(jobedit, tplJobedit)) + router.HandleFunc("/jobedit/{ID}", mkHttpHandler(jobedit, tplJobedit)) http.Handle("/", router) diff --git a/tpls.go b/tpls.go index fdc6ca8..8d337cf 100644 --- a/tpls.go +++ b/tpls.go @@ -24,6 +24,7 @@ var ( tplPwreset *template.Template tplForgotpw *template.Template tplJobs *template.Template + tplJobedit *template.Template ) func initTpls() { @@ -39,6 +40,7 @@ func initTpls() { tplPwreset = loadTpl(tplpath, "pwreset") tplForgotpw = loadTpl(tplpath, "forgotpw") tplJobs = loadTpl(tplpath, "jobs") + tplJobedit = loadTpl(tplpath, "jobedit") } type msgTpldata struct { diff --git a/tpls/jobedit.tpl b/tpls/jobedit.tpl new file mode 100644 index 0000000..ef1d394 --- /dev/null +++ b/tpls/jobedit.tpl @@ -0,0 +1,51 @@ +{{define "title"}}{{if .JobID}}Edit Job{{else}}Create Job{{end}}{{end}} + +{{define "content"}} + {{if .Error}} +
{{.Error}}
+ {{end}} + + {{if not .Fatal}} + {{if .Success}} +
{{.Success}}
+ {{end}} + +
+

Mail

+

+ Subject:
+ +

+

+ Content:
+ +

+ +

Schedule

+ {{range .Schedules}} +

+ Start: +
+ + Repetition: + + +
+ + End: + +

+ {{end}} + +

Save

+ +
+ {{end}} +{{end}} \ No newline at end of file diff --git a/tpls/jobs.tpl b/tpls/jobs.tpl index 578791e..9408e48 100644 --- a/tpls/jobs.tpl +++ b/tpls/jobs.tpl @@ -23,7 +23,7 @@ {{range .Jobs}} - {{.Subject}} + {{.Subject}} {{.Excerpt}} {{.Next}} {{end}} -- cgit v1.2.3-54-g00ecf