summaryrefslogtreecommitdiff
path: root/forms.go
diff options
context:
space:
mode:
authorJan Dittberner <jan@dittberner.info>2017-04-18 02:34:21 +0200
committerJan Dittberner <jan@dittberner.info>2017-04-22 00:12:32 +0200
commit471daf12ea98aa2a7fcb11deff9697403c5dd6a5 (patch)
tree995daa7635955bef5b973f38ecf88cf3b2a2d82f /forms.go
parent57e3d5324559255e57655f71b333edc6c54fabdf (diff)
downloadcacert-boardvoting-471daf12ea98aa2a7fcb11deff9697403c5dd6a5.tar.gz
cacert-boardvoting-471daf12ea98aa2a7fcb11deff9697403c5dd6a5.tar.xz
cacert-boardvoting-471daf12ea98aa2a7fcb11deff9697403c5dd6a5.zip
Partialy add new motion creation
Diffstat (limited to 'forms.go')
-rw-r--r--forms.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/forms.go b/forms.go
new file mode 100644
index 0000000..d5359ff
--- /dev/null
+++ b/forms.go
@@ -0,0 +1,54 @@
+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 {
+ f.Errors["Title"] = "Please enter at least 3 characters."
+ }
+
+ data.Content = strings.TrimSpace(f.Content)
+ if len(strings.Fields(data.Content)) < 3 {
+ f.Errors["Content"] = "Please enter at least 3 words."
+ }
+
+ if voteType, err := strconv.ParseUint(f.VoteType, 10, 8); err != nil || (voteType != 0 && voteType != 1) {
+ f.Errors["VoteType"] = fmt.Sprint("Please choose a valid vote type", err)
+ } else {
+ data.VoteType = VoteType(uint8(voteType))
+ }
+
+ if dueDuration, ok := validDueDurations[f.Due]; !ok {
+ f.Errors["Due"] = "Please choose a valid due date"
+ } 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
+}