diff options
author | Kevin Chabowski <kevin@kch42.de> | 2013-08-30 23:46:47 +0200 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2013-08-30 23:46:47 +0200 |
commit | 945af900ee539fc270e82e906c6d961c51e4913d (patch) | |
tree | 10ae1fd5c43731790f5d8b1de43c3719b814b846 /jobs.go | |
parent | a007e53b037a08ce34efd0b936dfa03127c2b7d1 (diff) | |
download | mailremind-945af900ee539fc270e82e906c6d961c51e4913d.tar.gz mailremind-945af900ee539fc270e82e906c6d961c51e4913d.tar.bz2 mailremind-945af900ee539fc270e82e906c6d961c51e4913d.zip |
Added Joblist
Diffstat (limited to 'jobs.go')
-rw-r--r-- | jobs.go | 81 |
1 files changed, 81 insertions, 0 deletions
@@ -0,0 +1,81 @@ +package main + +import ( + "github.com/gorilla/sessions" + "kch42.de/gostuff/mailremind/model" + "net/http" +) + +type jobTpldata struct { + ID, Subject, Excerpt, Next string +} + +func jobToTpldata(job model.Job, user model.User) *jobTpldata { + excerpt := string(job.Content()) + if len(excerpt) > 100 { + excerpt = string([]rune(excerpt)[0:100]) + " (...)" + } + + return &jobTpldata{ + ID: job.ID().String(), + Subject: job.Subject(), + Excerpt: excerpt, + Next: job.Next().In(user.Location()).Format("2006-Jan-02 15:04:05"), + } +} + +type jobsTpldata struct { + Error, Success string + Jobs []*jobTpldata + Fatal bool +} + +func jobs(user model.User, sess *sessions.Session, req *http.Request) interface{} { + if user == nil { + return &jobsTpldata{Error: "You need to be logged in to do that.", Fatal: true} + } + + outdata := new(jobsTpldata) + + if req.Method == "POST" { + if err := req.ParseForm(); err != nil { + outdata.Error = "Could not understand form data." + goto listjobs + } + + if req.FormValue("Delconfirm") != "yes" { + goto listjobs + } + + for _, _id := range req.Form["Jobs"] { + id, err := db.ParseDBID(_id) + if err != nil { + outdata.Error = "Not all jobs could be deleted." + continue + } + + job, err := user.JobByID(id) + if err != nil { + outdata.Error = "Not all jobs could be deleted." + continue + } + + if job.Delete() != nil { + outdata.Error = "Not all jobs could be deleted." + continue + } + + outdata.Success = "Jobs deleted." + } + } + +listjobs: + jobs := user.Jobs() + outdata.Jobs = make([]*jobTpldata, len(jobs)) + + for i, job := range jobs { + outdata.Jobs[i] = jobToTpldata(job, user) + } + + return outdata +} |