-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy patheboard.go
More file actions
80 lines (73 loc) · 2.17 KB
/
eboard.go
File metadata and controls
80 lines (73 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
"net/http"
"slices"
"strings"
"github.com/gin-gonic/gin"
)
var votes map[string]float32
var voters []string
var OPTIONS = []string{"Pass", "Fail", "Abstain"}
func HandleGetEboardVote(c *gin.Context) {
user := GetUserData(c)
if !IsEboard(user) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "You need to be E-Board to access this page"})
return
}
if votes == nil {
votes = make(map[string]float32)
}
fmt.Println(user)
c.HTML(http.StatusOK, "eboard.tmpl", gin.H{
"Username": user.Username,
"EBoard": IsEboard(user),
"Voted": slices.Contains(voters, user.Username),
"Results": votes,
"VoteCount": len(voters),
"Options": OPTIONS,
})
}
func HandlePostEboardVote(c *gin.Context) {
user := GetUserData(c)
if !IsEboard(user) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "You need to be E-Board to access this page"})
return
}
if slices.Contains(voters, user.Username) {
c.JSON(http.StatusBadRequest, gin.H{"error": "You cannot vote again!"})
return
}
i := slices.IndexFunc(user.Groups, func(s string) bool {
return strings.Contains(s, "eboard-")
})
if i == -1 {
c.JSON(http.StatusBadRequest, gin.H{"error": "You have the eboard group but not an eboard-[position] group. What is wrong with you?"})
return
}
//get the eboard position, count the members, and divide one whole vote by the number of members in the position
position := user.Groups[i]
positionMembers := oidcClient.GetOIDCGroup(oidcClient.FindOIDCGroupID(position))
weight := 1.0 / float32(len(positionMembers))
fmt.Println(weight)
//post the vote
option := c.PostForm("option")
if option == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "You need to pick an option"})
}
votes[option] = votes[option] + weight
voters = append(voters, user.Username)
c.Redirect(http.StatusFound, "/eboard")
}
func HandleManageEboardVote(c *gin.Context) {
user := GetUserData(c)
if !IsEboard(user) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "You need to be E-Board to access this page"})
return
}
if c.PostForm("clear_vote") != "" {
votes = make(map[string]float32)
voters = make([]string, 0)
}
c.Redirect(http.StatusFound, "/eboard")
}