summaryrefslogtreecommitdiff
path: root/forms.go
diff options
context:
space:
mode:
authorJan Dittberner <jandd@cacert.org>2021-01-09 15:49:19 +0100
committerJan Dittberner <jandd@cacert.org>2021-01-09 15:49:19 +0100
commit03827874cfc51b4be6709459d5263ef87f31dbe4 (patch)
tree521618919516cf130aba35c0fca119d1e660d157 /forms.go
parent594df29dc132c1573dddda3883eb4a31cdd99756 (diff)
downloadcacert-boardvoting-03827874cfc51b4be6709459d5263ef87f31dbe4.tar.gz
cacert-boardvoting-03827874cfc51b4be6709459d5263ef87f31dbe4.tar.xz
cacert-boardvoting-03827874cfc51b4be6709459d5263ef87f31dbe4.zip
Configure golangci-lint and apply suggestions
Diffstat (limited to 'forms.go')
-rw-r--r--forms.go85
1 files changed, 53 insertions, 32 deletions
diff --git a/forms.go b/forms.go
index 9af6cf1..8d573bc 100644
--- a/forms.go
+++ b/forms.go
@@ -1,18 +1,19 @@
/*
- Copyright 2017-2019 Jan Dittberner
+Copyright 2017-2021 Jan Dittberner
- 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
+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
- http://www.apache.org/licenses/LICENSE-2.0
+ http://www.apache.org/licenses/LICENSE-2.0
- 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.
+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.
*/
+
package main
import (
@@ -22,11 +23,24 @@ import (
"time"
)
+const (
+ minimumContentLen = 3
+ minimumTitleLen = 3
+)
+
+const (
+ hoursInADay = 24
+ dueThreeDays = 3
+ dueOneWeek = 7
+ dueTwoWeeks = 14
+ dueFourWeeks = 28
+)
+
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,
+ "+3 days": time.Hour * hoursInADay * dueThreeDays,
+ "+7 days": time.Hour * hoursInADay * dueOneWeek,
+ "+14 days": time.Hour * hoursInADay * dueTwoWeeks,
+ "+28 days": time.Hour * hoursInADay * dueFourWeeks,
}
type NewDecisionForm struct {
@@ -43,13 +57,13 @@ func (f *NewDecisionForm) Validate() (bool, *Decision) {
data := &Decision{}
data.Title = strings.TrimSpace(f.Title)
- if len(data.Title) < 3 {
- f.Errors["Title"] = "Please enter at least 3 characters for Title."
+ if len(data.Title) < minimumTitleLen {
+ f.Errors["Title"] = fmt.Sprintf("Please enter at least %d characters for Title.", minimumTitleLen)
}
data.Content = strings.TrimSpace(f.Content)
- if len(strings.Fields(data.Content)) < 3 {
- f.Errors["Content"] = "Please enter at least 3 words as Text."
+ if len(strings.Fields(data.Content)) < minimumContentLen {
+ f.Errors["Content"] = fmt.Sprintf("Please enter at least %d words as Text.", minimumContentLen)
}
if voteType, err := strconv.ParseUint(f.VoteType, 10, 8); err != nil || (voteType != 0 && voteType != 1) {
@@ -83,13 +97,13 @@ func (f *EditDecisionForm) Validate() (bool, *Decision) {
data := f.Decision
data.Title = strings.TrimSpace(f.Title)
- if len(data.Title) < 3 {
- f.Errors["Title"] = "Please enter at least 3 characters for Title."
+ if len(data.Title) < minimumTitleLen {
+ f.Errors["Title"] = fmt.Sprintf("Please enter at least %d characters for Title.", minimumTitleLen)
}
data.Content = strings.TrimSpace(f.Content)
- if len(strings.Fields(data.Content)) < 3 {
- f.Errors["Content"] = "Please enter at least 3 words as Text."
+ if len(strings.Fields(data.Content)) < minimumContentLen {
+ f.Errors["Content"] = fmt.Sprintf("Please enter at least %d words as Text.", minimumContentLen)
}
if voteType, err := strconv.ParseUint(f.VoteType, 10, 8); err != nil || (voteType != 0 && voteType != 1) {
@@ -118,27 +132,34 @@ type ProxyVoteForm struct {
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
+ )
+
data := &Vote{}
- var voter *Voter
- if voterId, err := strconv.ParseInt(f.Voter, 10, 64); err != nil {
- f.Errors["Voter"] = fmt.Sprint("Please choose a valid voter.", err)
- } else if voter, err = GetVoterById(voterId); err != nil {
- f.Errors["Voter"] = fmt.Sprint("Please choose a valid voter.", err)
+ if voterID, err = strconv.ParseInt(f.Voter, 10, 64); 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)
} else {
- data.VoterId = voter.Id
+ data.VoterID = voter.ID
}
- if vote, err := strconv.ParseInt(f.Vote, 10, 8); err != nil {
- f.Errors["Vote"] = fmt.Sprint("Please choose a valid vote.", err)
+ if vote, err = strconv.ParseInt(f.Vote, 10, 8); err != nil {
+ f.Errors["Vote"] = fmt.Sprintf("Please choose a valid vote: %v.", err)
} else if voteChoice, ok := VoteChoices[vote]; !ok {
- f.Errors["Vote"] = fmt.Sprint("Please choose a valid vote.")
+ f.Errors["Vote"] = "Please choose a valid vote."
} else {
data.Vote = voteChoice
}
justification := strings.TrimSpace(f.Justification)
- if len(justification) < 3 {
+ if len(justification) < minimumJustificationLen {
f.Errors["Justification"] = "Please enter at least 3 characters for justification."
}