Exemple #1
0
func jobs(user model.User, sess *sessions.Session, req *http.Request) (interface{}, model.User) {
	if user == nil {
		return &jobsTpldata{Error: "You need to be logged in to do that.", Fatal: true}, user
	}

	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, user
}
Exemple #2
0
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
}