func (jt *jobeditTpldata) fillFromJob(job model.Job, u model.User) { jt.JobID = job.ID().String() jt.Subject = job.Subject() jt.Content = string(job.Content()) jt.Schedules = make([]scheduleTpldata, maxSchedules) for i, sched := range job.Schedule() { if i == maxSchedules { log.Printf("Job %s has more than %d schedule entries!", job.ID(), maxSchedules) break } jt.Schedules[i] = schedToSchedTL(sched, u) } }
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"), } }
func sendjob(job model.Job, t time.Time) bool { user := job.User() buf := new(bytes.Buffer) fmt.Fprintf(buf, "From: %s\n", MailFrom) fmt.Fprintf(buf, "To: %s\n", user.Email()) fmt.Fprintf(buf, "Subject: %s\n", job.Subject()) fmt.Fprintf(buf, "Date: %s\n", t.In(user.Location()).Format(time.RFC1123Z)) fmt.Fprintln(buf, "") buf.Write(job.Content()) return Mail(user.Email(), MailFrom, buf.Bytes()) }
func jobedit(user model.User, sess *sessions.Session, req *http.Request) (interface{}, model.User) { if user == nil { return &jobeditTpldata{Error: "You need to be logged in to do that.", Fatal: true}, user } outdata := &jobeditTpldata{Schedules: make([]scheduleTpldata, maxSchedules)} // 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}, user } if job, err = user.JobByID(id); err != nil { return &jobeditTpldata{Error: "Job not found", Fatal: true}, user } } if job != nil { outdata.fillFromJob(job, user) } if req.Method == "POST" { if (job == nil) && (jobsLimit >= 0) && (user.CountJobs() >= jobsLimit) { outdata.Error = "You have reached the limit of jobs per user." outdata.Fatal = true return outdata, user } if err := req.ParseForm(); err != nil { outdata.Error = "Could not understand forma data." return outdata, user } subject, content, mc, ok := outdata.interpretForm(req.Form, user) if ok { next := mc.NextAfter(time.Now()) if next.IsZero() { outdata.Error = "The schedule would not send any mail." } else if job != nil { if logfail("setting subject", job.SetSubject(subject)) && logfail("setting content", job.SetContent(content)) && logfail("setting schedule", job.SetSchedule(mc)) && logfail("setting next", job.SetNext(next)) { outdata.Success = "Changes saved" } else { outdata.Error = "Could not save everything." } } else { if job, err := user.AddJob(subject, content, mc, next); logfail("creating new job", err) { outdata.fillFromJob(job, user) outdata.Success = "Job created" } else { outdata.Error = "Failed creating the job." } } } } return outdata, user }