2020-12-29 18:17:11 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2022-05-03 14:58:11 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-12-29 18:17:11 +01:00
|
|
|
"log"
|
2021-01-18 12:51:42 +01:00
|
|
|
"os"
|
2022-05-03 14:58:11 +02:00
|
|
|
"strings"
|
2020-12-29 18:17:11 +01:00
|
|
|
|
|
|
|
"git.andreafazzi.eu/andrea/oef/client"
|
|
|
|
"git.andreafazzi.eu/andrea/oef/orm"
|
2022-05-03 14:58:11 +02:00
|
|
|
"github.com/gocarina/gocsv"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RegionContest struct {
|
|
|
|
Id uint
|
|
|
|
Category string
|
|
|
|
Regions []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Username string `csv:"username"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
regionContests map[string]([]*RegionContest)
|
2020-12-29 18:17:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
2022-05-03 19:56:23 +02:00
|
|
|
region := flag.Bool("region", false, "Region based subscription")
|
2021-01-18 12:51:42 +01:00
|
|
|
unsubscribeAll := flag.Bool("unsubscribe-all", false, "Unsubscribe all participants from all contests")
|
2020-12-29 18:17:11 +01:00
|
|
|
|
|
|
|
juniorContestId := flag.Int("jid", 0, "Contest ID for Junior category")
|
|
|
|
seniorContestId := flag.Int("sid", 0, "Contest ID for Senior category")
|
|
|
|
|
2022-05-03 14:58:11 +02:00
|
|
|
participantsCSV := flag.String("participants-csv", "", "Import participants from CSV file")
|
|
|
|
// contestId := flag.Int("cid", 0, "Subscribe participants to the given contest ID")
|
|
|
|
|
2020-12-29 18:17:11 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
client, err := client.Dial(flag.Arg(0), *username, *password)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-05-03 15:15:45 +02:00
|
|
|
allParticipants := make([]*orm.Participant, 0)
|
2022-05-03 14:58:11 +02:00
|
|
|
|
|
|
|
log.Println("Read all participants...")
|
2022-05-03 15:15:45 +02:00
|
|
|
err = client.ReadAll(&allParticipants)
|
2020-12-29 18:17:11 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-05-03 14:58:11 +02:00
|
|
|
if *participantsCSV != "" {
|
2022-05-03 15:15:45 +02:00
|
|
|
participants := make([]*orm.Participant, 0)
|
2022-05-03 14:58:11 +02:00
|
|
|
users := make([]User, 0)
|
|
|
|
data, err := ioutil.ReadFile(*participantsCSV)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := gocsv.Unmarshal(strings.NewReader(string(data)), &users); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, u := range users {
|
2022-05-03 15:15:45 +02:00
|
|
|
for _, p := range allParticipants {
|
|
|
|
if u.Username == p.User.Username {
|
|
|
|
participants = append(participants, p)
|
|
|
|
}
|
|
|
|
}
|
2022-05-03 14:58:11 +02:00
|
|
|
}
|
2022-05-03 19:56:23 +02:00
|
|
|
allParticipants = participants
|
2022-05-03 14:58:11 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 18:17:11 +01:00
|
|
|
var updated bool
|
|
|
|
|
2021-01-18 12:51:42 +01:00
|
|
|
if *unsubscribeAll {
|
2022-05-03 19:56:23 +02:00
|
|
|
for _, participant := range allParticipants {
|
2021-01-18 12:51:42 +01:00
|
|
|
participant.ContestIDs = make(orm.ContestIDs, 0)
|
|
|
|
id, err := client.Update(participant)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
log.Printf("Successfully updated participant ID %d", id)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2022-05-03 14:58:11 +02:00
|
|
|
if *region {
|
|
|
|
data, err := ioutil.ReadFile("regions_contests.yaml")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(data, ®ionContests)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
regions_ids := make(map[string](map[string]uint), 0)
|
|
|
|
|
|
|
|
for _, contest := range regionContests["contests"] {
|
|
|
|
for _, region := range contest.Regions {
|
|
|
|
if regions_ids[region] == nil {
|
|
|
|
regions_ids[region] = make(map[string]uint)
|
|
|
|
}
|
|
|
|
if contest.Category == "Junior" {
|
|
|
|
regions_ids[region]["Junior"] = contest.Id
|
|
|
|
} else {
|
|
|
|
regions_ids[region]["Senior"] = contest.Id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 19:56:23 +02:00
|
|
|
for _, participant := range allParticipants {
|
2020-12-29 18:17:11 +01:00
|
|
|
for _, contest := range participant.Contests {
|
|
|
|
participant.ContestIDs = append(participant.ContestIDs, contest.ID)
|
|
|
|
}
|
|
|
|
|
2022-05-03 14:58:11 +02:00
|
|
|
if regions_ids[participant.School.Region.Name] == nil {
|
|
|
|
panic(fmt.Errorf("Region not found for participant %v", participant))
|
2020-12-29 18:17:11 +01:00
|
|
|
}
|
|
|
|
|
2022-05-03 14:58:11 +02:00
|
|
|
contestId := regions_ids[participant.School.Region.Name][participant.Category.Name]
|
|
|
|
|
|
|
|
if !alreadySubscribed(participant, contestId) {
|
|
|
|
// log.Printf("Subscribe %s to contest ID %d", participant, contestId)
|
|
|
|
participant.ContestIDs = append(participant.ContestIDs, contestId)
|
|
|
|
updated = true
|
|
|
|
}
|
2020-12-29 18:17:11 +01:00
|
|
|
if updated {
|
|
|
|
id, err := client.Update(participant)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
log.Printf("Successfully updated participant ID %d", id)
|
|
|
|
updated = false
|
|
|
|
}
|
|
|
|
}
|
2022-05-03 14:58:11 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
if *subscribe {
|
2022-05-03 19:56:23 +02:00
|
|
|
for _, participant := range allParticipants {
|
2022-05-03 14:58:11 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-29 18:17:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !updated {
|
|
|
|
log.Println("Nothing to do.")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|