summaryrefslogtreecommitdiff
path: root/jobedit.go
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2013-08-31 21:52:40 +0200
committerKevin Chabowski <kevin@kch42.de>2013-08-31 21:52:40 +0200
commitf68f1f326a196882383e534ff5166bf91b4e079d (patch)
treed49c42dca58f6868222c5d3d8b20bf69b5c2e7e9 /jobedit.go
parent945af900ee539fc270e82e906c6d961c51e4913d (diff)
downloadmailremind-f68f1f326a196882383e534ff5166bf91b4e079d.tar.gz
mailremind-f68f1f326a196882383e534ff5166bf91b4e079d.tar.bz2
mailremind-f68f1f326a196882383e534ff5166bf91b4e079d.zip
Jobs can now be viewed in the job editor.
Diffstat (limited to 'jobedit.go')
-rw-r--r--jobedit.go103
1 files changed, 103 insertions, 0 deletions
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
+}