cacert-boardvoting/forms.go

171 lines
4.6 KiB
Go
Raw Normal View History

2019-07-31 15:30:58 +00:00
/*
Copyright 2017-2021 Jan Dittberner
2019-07-31 15:30:58 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this program except in compliance with the License.
You may obtain a copy of the License at
2019-07-31 15:30:58 +00:00
http://www.apache.org/licenses/LICENSE-2.0
2019-07-31 15:30:58 +00:00
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
2019-07-31 15:30:58 +00:00
*/
2017-04-18 00:34:21 +00:00
package main
import (
"fmt"
"strconv"
"strings"
"time"
)
const (
minimumContentLen = 3
minimumTitleLen = 3
base10 = 10
size8Bit = 8
size64Bit = 64
)
const (
hoursInADay = 24
dueThreeDays = 3
dueOneWeek = 7
dueTwoWeeks = 14
dueFourWeeks = 28
)
2017-04-18 00:34:21 +00:00
var validDueDurations = map[string]time.Duration{
"+3 days": time.Hour * hoursInADay * dueThreeDays,
"+7 days": time.Hour * hoursInADay * dueOneWeek,
"+14 days": time.Hour * hoursInADay * dueTwoWeeks,
"+28 days": time.Hour * hoursInADay * dueFourWeeks,
2017-04-18 00:34:21 +00:00
}
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) < minimumTitleLen {
f.Errors["Title"] = fmt.Sprintf("Please enter at least %d characters for Title.", minimumTitleLen)
2017-04-18 00:34:21 +00:00
}
data.Content = strings.TrimSpace(f.Content)
if len(strings.Fields(data.Content)) < minimumContentLen {
f.Errors["Content"] = fmt.Sprintf("Please enter at least %d words as Text.", minimumContentLen)
2017-04-18 00:34:21 +00:00
}
if voteType, err := strconv.ParseUint(f.VoteType, base10, size8Bit); 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) < minimumTitleLen {
f.Errors["Title"] = fmt.Sprintf("Please enter at least %d characters for Title.", minimumTitleLen)
2017-04-18 22:05:42 +00:00
}
data.Content = strings.TrimSpace(f.Content)
if len(strings.Fields(data.Content)) < minimumContentLen {
f.Errors["Content"] = fmt.Sprintf("Please enter at least %d words as Text.", minimumContentLen)
2017-04-18 22:05:42 +00:00
}
if voteType, err := strconv.ParseUint(f.VoteType, base10, size8Bit); 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)
const minimumJustificationLen = 3
var (
voter *Voter
err error
voterID, vote int64
)
2017-04-21 00:25:49 +00:00
data := &Vote{}
if voterID, err = strconv.ParseInt(f.Voter, base10, size64Bit); err != nil {
f.Errors["Voter"] = fmt.Sprintf("Please choose a valid voter: %v.", err)
} else if voter, err = GetVoterByID(voterID); err != nil {
f.Errors["Voter"] = fmt.Sprintf("Please choose a valid voter: %v.", err)
2017-04-21 00:25:49 +00:00
} else {
data.VoterID = voter.ID
2017-04-21 00:25:49 +00:00
}
if vote, err = strconv.ParseInt(f.Vote, base10, size8Bit); err != nil {
f.Errors["Vote"] = fmt.Sprintf("Please choose a valid vote: %v.", err)
2017-04-21 00:25:49 +00:00
} else if voteChoice, ok := VoteChoices[vote]; !ok {
f.Errors["Vote"] = "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) < minimumJustificationLen {
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
}