Example #1
0
func schedToSchedTL(sched schedule.Schedule, u model.User) scheduleTpldata {
	loc := u.Location()

	schedtl := scheduleTpldata{
		Start: sched.Start.In(loc).Format(bestTimeFmtEver),
	}

	if f := sched.Freq; f.Count > 0 {
		schedtl.RepetitionEnabled = true
		schedtl.Count = int(f.Count)
		switch f.Unit {
		case schedule.Minute:
			schedtl.UnitIsMinute = true
		case schedule.Hour:
			schedtl.UnitIsHour = true
		case schedule.Day:
			schedtl.UnitIsDay = true
		case schedule.Week:
			schedtl.UnitIsWeek = true
		case schedule.Month:
			schedtl.UnitIsMonth = true
		case schedule.Year:
			schedtl.UnitIsYear = true
		}
	}

	if end := sched.End; !end.IsZero() {
		schedtl.EndEnabled = true
		schedtl.End = end.In(loc).Format(bestTimeFmtEver)
	}

	return schedtl
}
Example #2
0
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"),
	}
}
Example #3
0
func (jt *jobeditTpldata) interpretForm(form url.Values, u model.User) (subject string, content []byte, ms schedule.MultiSchedule, ok bool) {
	loc := u.Location()

	l1 := len(form["Start"])
	l2 := len(form["RepetitionEnabled"])
	l3 := len(form["Count"])
	l4 := len(form["Unit"])
	l5 := len(form["EndEnabled"])
	l6 := len(form["End"])

	if (l1 != l2) || (l2 != l3) || (l3 != l4) || (l4 != l5) || (l5 != l6) {
		jt.Error = "Form corrupted. Changes were not saved"
		ok = false
		return
	}

	subject = form.Get("Subject")
	content = []byte(form.Get("Content"))
	jt.Subject = subject
	jt.Content = string(content)

	ok = true
	for i, _start := range form["Start"] {
		if i >= maxSchedules {
			break
		}

		if _start == "" {
			continue
		}

		start, err := time.ParseInLocation(bestTimeFmtEver, _start, loc)
		if err != nil {
			ok = false
			continue
		}

		count := uint64(0)
		var unit schedule.TimeUnit
		var end time.Time
		if form["RepetitionEnabled"][i] == "yes" {
			if count, err = strconv.ParseUint(form["Count"][i], 10, 64); err != nil {
				ok = false
				continue
			}

			switch form["Unit"][i] {
			case "Minute":
				unit = schedule.Minute
			case "Hour":
				unit = schedule.Hour
			case "Day":
				unit = schedule.Day
			case "Week":
				unit = schedule.Week
			case "Month":
				unit = schedule.Month
			case "Year":
				unit = schedule.Year
			default:
				ok = false
				continue
			}

			if form["EndEnabled"][i] == "yes" {
				if end, err = time.ParseInLocation(bestTimeFmtEver, form["End"][i], loc); err != nil {
					ok = false
					continue
				}
			}
		}

		sched := schedule.Schedule{
			Start: start,
			Freq: schedule.Frequency{
				Count: uint(count),
				Unit:  unit,
			},
			End: end,
		}
		ms = append(ms, sched)
		jt.Schedules[i] = schedToSchedTL(sched, u)
	}

	if !ok {
		jt.Error = "Some schedules were wrong (wrong time format, negative repetition counts)"
		return
	}

	if len(ms) == 0 {
		jt.Error = "No schedule."
		ok = false
	}

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

	outdata := &settingsTpldata{Timezones: make(map[string]bool)}
	tznow := user.Location().String()
	for _, tz := range timeLocs {
		outdata.Timezones[tz] = (tz == tznow)
	}

	if req.Method != "POST" {
		return outdata, user
	}

	if err := req.ParseForm(); err != nil {
		outdata.Error = "Could not parse form"
		return outdata, user
	}

	switch req.FormValue("M") {
	case "setpasswd":
		if req.FormValue("Password") == "" {
			outdata.Error = "Password must not be empty."
			return outdata, user
		}

		if req.FormValue("Password") != req.FormValue("RepeatPassword") {
			outdata.Error = "Passwords must be equal."
			return outdata, user
		}

		hash, err := bcrypt.GenerateFromPassword([]byte(req.FormValue("Password")), bcrypt.DefaultCost)
		if err != nil {
			log.Printf("Error hashing password: %s", err)
			outdata.Error = "Error while saving password."
			return outdata.Error, user
		}

		if err := user.SetPWHash(hash); err != nil {
			log.Printf("Error setting pwhash: %s", err)
			outdata.Error = "Could not save new password."
		} else {
			outdata.Success = "Password changed"
		}
	case "settimezone":
		loc, err := time.LoadLocation(req.FormValue("Timezone"))
		if err != nil {
			outdata.Error = "Unknown Timezone"
			return outdata, user
		}

		if err := user.SetLocation(loc); err != nil {
			log.Printf("Error setting location: %s", err)
			outdata.Error = "Could not save new timezone."
		} else {
			outdata.Success = "New timezone saved."
		}
	}

	return outdata, user
}