cacert-boardvoting/forms.go

132 lines
3.4 KiB
Go
Raw Normal View History

2017-04-18 00:34:21 +00:00
package main
import (
"fmt"
"strconv"
"strings"
"time"
)
var validDueDurations = map[string]time.Duration{
"+3 days": time.Hour * 24 * 3,
"+7 days": time.Hour * 24 * 7,
"+14 days": time.Hour * 24 * 14,
"+28 days": time.Hour * 24 * 28,
}
type NewDecisionForm struct {
Title string
Content string
VoteType string
Due string
Errors map[string]string
}
func (f *NewDecisionForm) Validate() (bool, *Decision) {
f.Errors = make(map[string]string)
data := &Decision{}
data.Title = strings.TrimSpace(f.Title)
if len(data.Title) < 3 {
2017-04-30 00:37:29 +00:00
f.Errors["Title"] = "Please enter at least 3 characters for Title."
2017-04-18 00:34:21 +00:00
}
data.Content = strings.TrimSpace(f.Content)
if len(strings.Fields(data.Content)) < 3 {
2017-04-30 00:37:29 +00:00
f.Errors["Content"] = "Please enter at least 3 words as Text."
2017-04-18 00:34:21 +00:00
}
if voteType, err := strconv.ParseUint(f.VoteType, 10, 8); err != nil || (voteType != 0 && voteType != 1) {
2017-04-30 00:37:29 +00:00
f.Errors["VoteType"] = fmt.Sprint("Please choose a valid vote type.", err)
2017-04-18 00:34:21 +00:00
} else {
data.VoteType = VoteType(uint8(voteType))
}
if dueDuration, ok := validDueDurations[f.Due]; !ok {
2017-04-30 00:37:29 +00:00
f.Errors["Due"] = "Please choose a valid due date."
2017-04-18 00:34:21 +00:00
} else {
year, month, day := time.Now().UTC().Add(dueDuration).Date()
data.Due = time.Date(year, month, day, 23, 59, 59, 0, time.UTC)
2017-04-18 00:34:21 +00:00
}
return len(f.Errors) == 0, data
}
2017-04-18 22:05:42 +00:00
type EditDecisionForm struct {
Title string
Content string
VoteType string
Due string
Decision *Decision
Errors map[string]string
}
func (f *EditDecisionForm) Validate() (bool, *Decision) {
f.Errors = make(map[string]string)
data := f.Decision
data.Title = strings.TrimSpace(f.Title)
if len(data.Title) < 3 {
2017-04-30 00:37:29 +00:00
f.Errors["Title"] = "Please enter at least 3 characters for Title."
2017-04-18 22:05:42 +00:00
}
data.Content = strings.TrimSpace(f.Content)
if len(strings.Fields(data.Content)) < 3 {
2017-04-30 00:37:29 +00:00
f.Errors["Content"] = "Please enter at least 3 words as Text."
2017-04-18 22:05:42 +00:00
}
if voteType, err := strconv.ParseUint(f.VoteType, 10, 8); err != nil || (voteType != 0 && voteType != 1) {
2017-04-30 00:37:29 +00:00
f.Errors["VoteType"] = fmt.Sprint("Please choose a valid vote type.", err)
2017-04-18 22:05:42 +00:00
} else {
data.VoteType = VoteType(uint8(voteType))
}
if dueDuration, ok := validDueDurations[f.Due]; !ok {
2017-04-30 00:37:29 +00:00
f.Errors["Due"] = "Please choose a valid due date."
2017-04-18 22:05:42 +00:00
} else {
year, month, day := time.Now().UTC().Add(dueDuration).Date()
data.Due = time.Date(year, month, day, 23, 59, 59, 0, time.UTC)
}
return len(f.Errors) == 0, data
}
2017-04-21 00:25:49 +00:00
type ProxyVoteForm struct {
Voter string
Vote string
Justification string
Errors map[string]string
}
func (f *ProxyVoteForm) Validate() (bool, *Voter, *Vote, string) {
f.Errors = make(map[string]string)
data := &Vote{}
var voter *Voter
if voterId, err := strconv.ParseInt(f.Voter, 10, 64); err != nil {
2017-04-30 00:37:29 +00:00
f.Errors["Voter"] = fmt.Sprint("Please choose a valid voter.", err)
2017-04-21 00:25:49 +00:00
} else if voter, err = GetVoterById(voterId); err != nil {
2017-04-30 00:37:29 +00:00
f.Errors["Voter"] = fmt.Sprint("Please choose a valid voter.", err)
2017-04-21 00:25:49 +00:00
} else {
data.VoterId = voter.Id
}
if vote, err := strconv.ParseInt(f.Vote, 10, 8); err != nil {
2017-04-30 00:37:29 +00:00
f.Errors["Vote"] = fmt.Sprint("Please choose a valid vote.", err)
2017-04-21 00:25:49 +00:00
} else if voteChoice, ok := VoteChoices[vote]; !ok {
2017-04-30 00:37:29 +00:00
f.Errors["Vote"] = fmt.Sprint("Please choose a valid vote.")
2017-04-21 00:25:49 +00:00
} else {
data.Vote = voteChoice
}
justification := strings.TrimSpace(f.Justification)
if len(justification) < 3 {
2017-04-30 00:37:29 +00:00
f.Errors["Justification"] = "Please enter at least 3 characters for justification."
2017-04-21 00:25:49 +00:00
}
return len(f.Errors) == 0, voter, data, justification
}