Add subscribe participant script

This commit is contained in:
Andrea Fazzi 2020-12-29 18:17:11 +01:00
parent da71af9855
commit 1337e5b670
3 changed files with 80 additions and 0 deletions

View file

@ -0,0 +1 @@
*.csv

View file

@ -0,0 +1,79 @@
package main
import (
"flag"
"log"
"git.andreafazzi.eu/andrea/oef/client"
"git.andreafazzi.eu/andrea/oef/orm"
)
func alreadySubscribed(participant *orm.Participant, contestId uint) bool {
for _, id := range participant.ContestIDs {
if id == contestId {
return true
}
}
return false
}
func main() {
username := flag.String("username", "admin", "Username")
password := flag.String("password", "admin", "Password")
subscribe := flag.Bool("subscribe", true, "Subscribe to the given contest")
juniorContestId := flag.Int("jid", 0, "Contest ID for Junior category")
seniorContestId := flag.Int("sid", 0, "Contest ID for Senior category")
flag.Parse()
client, err := client.Dial(flag.Arg(0), *username, *password)
if err != nil {
panic(err)
}
participants := make([]*orm.Participant, 0)
err = client.ReadAll(&participants)
if err != nil {
panic(err)
}
var updated bool
if *subscribe {
for _, participant := range participants {
for _, contest := range participant.Contests {
participant.ContestIDs = append(participant.ContestIDs, contest.ID)
}
if *juniorContestId > 0 && participant.Category.Name == "Junior" {
if !alreadySubscribed(participant, uint(*juniorContestId)) {
log.Printf("Subscribe %s to contest ID %d", participant, *juniorContestId)
participant.ContestIDs = append(participant.ContestIDs, uint(*juniorContestId))
updated = true
}
}
if *seniorContestId > 0 && participant.Category.Name == "Senior" {
if !alreadySubscribed(participant, uint(*seniorContestId)) {
log.Printf("Subscribe %s to contest ID %d", participant, *seniorContestId)
participant.ContestIDs = append(participant.ContestIDs, uint(*seniorContestId))
updated = true
}
}
if updated {
id, err := client.Update(participant)
if err != nil {
panic(err)
}
log.Printf("Successfully updated participant ID %d", id)
updated = false
}
}
}
if !updated {
log.Println("Nothing to do.")
}
}

Binary file not shown.