Add missing newline in README
[cacert-boardvoting.git] / forms.go
1 /*
2 Copyright 2017-2019 Jan Dittberner
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this program except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16 package main
17
18 import (
19 "fmt"
20 "strconv"
21 "strings"
22 "time"
23 )
24
25 var validDueDurations = map[string]time.Duration{
26 "+3 days": time.Hour * 24 * 3,
27 "+7 days": time.Hour * 24 * 7,
28 "+14 days": time.Hour * 24 * 14,
29 "+28 days": time.Hour * 24 * 28,
30 }
31
32 type NewDecisionForm struct {
33 Title string
34 Content string
35 VoteType string
36 Due string
37 Errors map[string]string
38 }
39
40 func (f *NewDecisionForm) Validate() (bool, *Decision) {
41 f.Errors = make(map[string]string)
42
43 data := &Decision{}
44
45 data.Title = strings.TrimSpace(f.Title)
46 if len(data.Title) < 3 {
47 f.Errors["Title"] = "Please enter at least 3 characters for Title."
48 }
49
50 data.Content = strings.TrimSpace(f.Content)
51 if len(strings.Fields(data.Content)) < 3 {
52 f.Errors["Content"] = "Please enter at least 3 words as Text."
53 }
54
55 if voteType, err := strconv.ParseUint(f.VoteType, 10, 8); err != nil || (voteType != 0 && voteType != 1) {
56 f.Errors["VoteType"] = fmt.Sprint("Please choose a valid vote type.", err)
57 } else {
58 data.VoteType = VoteType(uint8(voteType))
59 }
60
61 if dueDuration, ok := validDueDurations[f.Due]; !ok {
62 f.Errors["Due"] = "Please choose a valid due date."
63 } else {
64 year, month, day := time.Now().UTC().Add(dueDuration).Date()
65 data.Due = time.Date(year, month, day, 23, 59, 59, 0, time.UTC)
66 }
67
68 return len(f.Errors) == 0, data
69 }
70
71 type EditDecisionForm struct {
72 Title string
73 Content string
74 VoteType string
75 Due string
76 Decision *Decision
77 Errors map[string]string
78 }
79
80 func (f *EditDecisionForm) Validate() (bool, *Decision) {
81 f.Errors = make(map[string]string)
82
83 data := f.Decision
84
85 data.Title = strings.TrimSpace(f.Title)
86 if len(data.Title) < 3 {
87 f.Errors["Title"] = "Please enter at least 3 characters for Title."
88 }
89
90 data.Content = strings.TrimSpace(f.Content)
91 if len(strings.Fields(data.Content)) < 3 {
92 f.Errors["Content"] = "Please enter at least 3 words as Text."
93 }
94
95 if voteType, err := strconv.ParseUint(f.VoteType, 10, 8); err != nil || (voteType != 0 && voteType != 1) {
96 f.Errors["VoteType"] = fmt.Sprint("Please choose a valid vote type.", err)
97 } else {
98 data.VoteType = VoteType(uint8(voteType))
99 }
100
101 if dueDuration, ok := validDueDurations[f.Due]; !ok {
102 f.Errors["Due"] = "Please choose a valid due date."
103 } else {
104 year, month, day := time.Now().UTC().Add(dueDuration).Date()
105 data.Due = time.Date(year, month, day, 23, 59, 59, 0, time.UTC)
106 }
107
108 return len(f.Errors) == 0, data
109 }
110
111 type ProxyVoteForm struct {
112 Voter string
113 Vote string
114 Justification string
115 Errors map[string]string
116 }
117
118 func (f *ProxyVoteForm) Validate() (bool, *Voter, *Vote, string) {
119 f.Errors = make(map[string]string)
120
121 data := &Vote{}
122
123 var voter *Voter
124 if voterId, err := strconv.ParseInt(f.Voter, 10, 64); err != nil {
125 f.Errors["Voter"] = fmt.Sprint("Please choose a valid voter.", err)
126 } else if voter, err = GetVoterById(voterId); err != nil {
127 f.Errors["Voter"] = fmt.Sprint("Please choose a valid voter.", err)
128 } else {
129 data.VoterId = voter.Id
130 }
131
132 if vote, err := strconv.ParseInt(f.Vote, 10, 8); err != nil {
133 f.Errors["Vote"] = fmt.Sprint("Please choose a valid vote.", err)
134 } else if voteChoice, ok := VoteChoices[vote]; !ok {
135 f.Errors["Vote"] = fmt.Sprint("Please choose a valid vote.")
136 } else {
137 data.Vote = voteChoice
138 }
139
140 justification := strings.TrimSpace(f.Justification)
141 if len(justification) < 3 {
142 f.Errors["Justification"] = "Please enter at least 3 characters for justification."
143 }
144
145 return len(f.Errors) == 0, voter, data, justification
146 }