Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Implementation

## Configuration

Copy `.env.example` to `.env` and fill out the info
Copy `.env.example` to `.env` and fill out the fields.

### Dev Overrides
`DEV_DISABLE_ACTIVE_FILTERS="true"` will disable the requirements that you be active to vote
Expand All @@ -38,10 +38,9 @@ go vet *.go

## To-Dos

- [ ] Don't let the user fuck it up
- [ ] Show E-Board polls with a higher priority
- [x] Move Hide Vote to create instead of after you vote :skull:
- [X] Display the reason why a user is on the results page of a running poll
- [x] Display the reason why a user is on the results page of a running poll
- [ ] Display minimum time left that a poll is open
- [ ] Move routes to their own functions
- [X] Change HTTP resposne codes to be `http.something` instead of just a number
- [x] Move routes to their own functions
- [x] Change HTTP resposne codes to be `http.something` instead of just a number
10 changes: 5 additions & 5 deletions database/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
// votes in this round is the winner. If all options have the same, then it is
// unfortunately a tie, and the vote is not resolvable, as there is no lowest
// option to eliminate.
func calculateRankedResult(ctx context.Context, votesRaw []RankedVote) ([]map[string]int, error) {

Check failure on line 206 in database/poll.go

View check run for this annotation

CSH-Sonarqube-Community / SonarQube Code Analysis

database/poll.go#L206

Refactor this method to reduce its Cognitive Complexity from 29 to the 15 allowed.
// We want to store those that were eliminated so we don't accidentally reinclude them
eliminated := make([]string, 0)
votes := make([][]string, 0)
Expand All @@ -216,19 +216,19 @@
votes = append(votes, optionList)
}

votesCast := len(votes)

round := 0
// Iterate until we have a winner
for {
round = round + 1
// Contains candidates to number of votes in this round
tallied := make(map[string]int)
voteCount := 0
for _, picks := range votes {
// Go over picks until we find a non-eliminated candidate
for _, candidate := range picks {
if !slices.Contains(eliminated, candidate) {
tallied[candidate]++
voteCount += 1
break
}
}
Expand All @@ -249,7 +249,7 @@
finalResult = append(finalResult, tallied)

// TODO this should probably include some poll identifier
logging.Logger.WithFields(logrus.Fields{"round": round, "tallies": tallied, "threshold": voteCount / 2}).Debug("round report")
logging.Logger.WithFields(logrus.Fields{"round": round, "tallies": tallied, "threshold": votesCast / 2}).Debug("round report")

// If one person has all the votes, they win
if len(tallied) == 1 {
Expand All @@ -258,8 +258,8 @@

end := true
for str, val := range tallied {
// if any particular entry is above half remaining votes, they win and it ends
if val > (voteCount / 2) {
// if any particular entry is above half of votes cast, they win and it ends
if val > (votesCast / 2) {
finalResult = append(finalResult, map[string]int{str: val})
end = true
break
Expand Down
12 changes: 12 additions & 0 deletions database/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,15 @@ func HasVoted(ctx context.Context, pollId, userId string) (bool, error) {
}
return count > 0, nil
}

func CastAbstain(ctx context.Context, voter *Voter) error {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

_, err := Client.Database(db).Collection("voters").InsertOne(ctx, voter)
if err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ services:
- "MONGO_INITDB_ROOT_USERNAME=vote"
- "MONGO_INITDB_ROOT_PASSWORD=c1f66aac6b4fafbef3c659371b8a50ed"
ports:
- "127.0.0.1:27017:27017"
- "27017:27017"
volumes:
- type: volume
source: mongodb
Expand Down
37 changes: 18 additions & 19 deletions api.go → routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,17 @@

switch c.PostForm("options") {
case "pass-fail-conditional":
poll.Options = []string{"Pass", "Fail/Conditional", "Abstain"}
poll.Options = []string{"Pass", "Fail/Conditional"}
case "fail-conditional":
poll.Options = []string{"Fail", "Conditional", "Abstain"}
poll.Options = []string{"Fail", "Conditional"}
case "custom":
poll.Options = []string{}
for opt := range strings.SplitSeq(c.PostForm("customOptions"), ",") {
poll.Options = append(poll.Options, strings.TrimSpace(opt))
if !slices.Contains(poll.Options, "Abstain") && (poll.VoteType == database.POLL_TYPE_SIMPLE) {
poll.Options = append(poll.Options, "Abstain")
}
}
case "pass-fail":
default:
poll.Options = []string{"Pass", "Fail", "Abstain"}
poll.Options = []string{"Pass", "Fail"}
}
if poll.Gatekeep {
if !IsEboard(user) {
Expand Down Expand Up @@ -340,11 +337,12 @@
return
}

if poll.VoteType == database.POLL_TYPE_SIMPLE {
switch poll.VoteType {
case database.POLL_TYPE_SIMPLE:
processSimpleVote(c, poll, pId, user)
} else if poll.VoteType == database.POLL_TYPE_RANKED {
case database.POLL_TYPE_RANKED:
processRankedVote(c, poll, pId, user)
} else {
default:
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unknown Poll Type"})
return
}
Expand Down Expand Up @@ -462,7 +460,12 @@
UserId: user.Username,
}

if hasOption(poll, c.PostForm("option")) {
if c.PostForm("abstain") != "" {
database.CastAbstain(c, &voter)
return
}

if slices.Contains(poll.Options, c.PostForm("option")) {
vote.Option = c.PostForm("option")
} else if poll.AllowWriteIns && c.PostForm("option") == "writein" {
vote.Option = c.PostForm("writeinOption")
Expand All @@ -474,7 +477,7 @@
}

// ProcessRankedVote Parses the ranked choice ballot, validates it, and then sends it to the database
func processRankedVote(c *gin.Context, poll *database.Poll, pId primitive.ObjectID, user cshAuth.CSHUserInfo) {

Check failure on line 480 in routes.go

View check run for this annotation

CSH-Sonarqube-Community / SonarQube Code Analysis

routes.go#L480

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.
vote := database.RankedVote{
Id: "",
PollId: pId,
Expand All @@ -485,6 +488,11 @@
UserId: user.Username,
}

if c.PostForm("abstain") != "" {
database.CastAbstain(c, &voter)
return
}

// Populate vote
for _, option := range poll.Options {
optionRankStr := c.PostForm(option)
Expand Down Expand Up @@ -612,12 +620,3 @@
}
return false
}

func hasOption(poll *database.Poll, option string) bool {
for _, opt := range poll.Options {
if opt == option {
return true
}
}
return false
}
27 changes: 18 additions & 9 deletions templates/poll.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@
<h4>{{ .Description | MakeLinks }}</h4>
{{ end }}
{{ if eq .PollType "ranked" }}
<p>This is a Ranked Choice vote. Rank the candidates in order of your preference. 1 is most preferred, and {{ .RankedMax }} is least preferred. You may leave an option blank
if you do not prefer it at all.</p>
<p>This is a Ranked Choice vote. Rank the candidates in order of your preference.
1 is most preferred, and {{ .RankedMax }} is least preferred.
You must rank in increasing order from 1, you cannot skip numbers.
You may leave an option blank if you do not prefer it at all (you have "No Confidence" in the option(s)).
If you like none of the options, you can submit the ballot without ranking any option.
Do not write in a "No Confidence" or "I like none of the candidates" in that case.</p>
{{ end }}

<br />
<br />

<form action="/poll/{{ .Id }}" method="POST">
<form action="/poll/{{ .Id }}" class="mx-4" method="POST">
{{ if eq .PollType "simple" }}
{{ range $i, $option := .Options }}
<div class="form-check fs-4 lh-sm">
<div class="form-check fs-5 lh-sm">
<input class="form-check-input" type="radio" name="option" id="{{ $option }}" value="{{ $option }}" />
<label class="form-check-label" for="{{ $option }}">{{ $option }}</label>
</div>
<br />
{{ end }}
{{ if .AllowWriteIns }}
<div class="form-check fs-4 lh-sm">
<div class="form-check fs-5 lh-sm">
<input class="form-check-input" type="radio" name="option" value="writein" />
<input
type="text"
Expand All @@ -38,7 +42,7 @@
{{ if eq .PollType "ranked" }}
{{ $rankedMax := .RankedMax }}
{{ range $i, $option := .Options }}
<div class="form-check d-inline-flex align-items-center my-2">
<div class="d-inline-flex align-items-center my-2">
<div>
<input
type="number"
Expand All @@ -50,7 +54,7 @@
style="min-width: 70px"
/>
</div>
<div class="mx-4">
<div class="mx-4 fs-5">
<label class="form-check-label" for="{{ $option }}">{{ $option }}</label>
</div>
</div>
Expand Down Expand Up @@ -81,7 +85,13 @@
{{ end }}
{{ end }}
<br />
<button type="submit" class="btn btn-primary my-4 mx-4">Submit</button>
<br />
<button type="submit" class="btn btn-danger mt-4" name="abstain" value="true">Abstain</button>
<div class="col-6 mt-2">
<span><strong>Note</strong>: Pressing Abstain will submit this page. Abstain will add you to quorum but will empty your ballot options and you will not actually vote for anyone or count as a Vote Cast.</span>
</div>
<br />
<button type="submit" class="btn btn-primary my-4">Submit Ballot</button>
</form>
{{ if and (.CanModify) (not .Gatekeep) }}
<form action="/poll/{{ .Id }}/close" method="POST">
Expand All @@ -91,4 +101,3 @@
</div>
</body>
</html>

Loading