From 463bc9d696ded42251e2727fe3d3b2c30cc3d485 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Wed, 16 Mar 2022 09:53:29 +0100 Subject: [PATCH 1/6] Add regions_contests.yaml configuration file --- scripts/subscribe_participants/regions_contests.yaml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 scripts/subscribe_participants/regions_contests.yaml diff --git a/scripts/subscribe_participants/regions_contests.yaml b/scripts/subscribe_participants/regions_contests.yaml new file mode 100644 index 00000000..bfd4f7a6 --- /dev/null +++ b/scripts/subscribe_participants/regions_contests.yaml @@ -0,0 +1,7 @@ +contests: + - id: 1 + regions: [Basilicata,Calabria,Lombardia,Piemonte,Val d'Aosta,Veneto] + - id: 2 + regions: [Campania,Emilia Romagna,Friuli Venezia Giulia,Liguria,Sardegna,Toscana,Trentino Alto Adige] + - id: 3 + regions: [Abruzzo,Lazio,Marche,Molise,Puglia, Sicilia,Umbria] \ No newline at end of file From 6151284f8e03fc5a889179678b3cd603dce9a2ce Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Wed, 16 Mar 2022 11:28:00 +0100 Subject: [PATCH 2/6] [auto] bumped version to 0.9.9-136-g463bc9d-master --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 6bb10a25..5c5d296a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.9.9-134-g373188f-master +0.9.9-136-g463bc9d-master From 9ab95fd06365f181185ef0bb84a37dd84c1d5a9b Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Tue, 3 May 2022 14:58:11 +0200 Subject: [PATCH 3/6] Preload User when reading all participants --- orm/participant.go | 1 + scripts/subscribe_participants/main.go | 123 ++++++++++++++++-- .../regions_contests.yaml | 24 +++- 3 files changed, 128 insertions(+), 20 deletions(-) diff --git a/orm/participant.go b/orm/participant.go index 796f0467..5e500fc0 100644 --- a/orm/participant.go +++ b/orm/participant.go @@ -330,6 +330,7 @@ func (model *Participant) ReadAll(db *Database, args map[string]string, w http.R } } else { if err := db._db. + Preload("User"). Preload("Category"). Preload("School"). Preload("School.Region"). diff --git a/scripts/subscribe_participants/main.go b/scripts/subscribe_participants/main.go index af05515d..67b5e63c 100644 --- a/scripts/subscribe_participants/main.go +++ b/scripts/subscribe_participants/main.go @@ -2,11 +2,30 @@ package main import ( "flag" + "fmt" + "io/ioutil" "log" "os" + "strings" "git.andreafazzi.eu/andrea/oef/client" "git.andreafazzi.eu/andrea/oef/orm" + "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) ) func alreadySubscribed(participant *orm.Participant, contestId uint) bool { @@ -22,11 +41,15 @@ func main() { username := flag.String("username", "admin", "Username") password := flag.String("password", "admin", "Password") subscribe := flag.Bool("subscribe", true, "Subscribe to the given contest") + region := flag.Bool("region", true, "Region based subscription") unsubscribeAll := flag.Bool("unsubscribe-all", false, "Unsubscribe all participants from all contests") juniorContestId := flag.Int("jid", 0, "Contest ID for Junior category") seniorContestId := flag.Int("sid", 0, "Contest ID for Senior category") + participantsCSV := flag.String("participants-csv", "", "Import participants from CSV file") + // contestId := flag.Int("cid", 0, "Subscribe participants to the given contest ID") + flag.Parse() client, err := client.Dial(flag.Arg(0), *username, *password) @@ -35,11 +58,29 @@ func main() { } participants := make([]*orm.Participant, 0) + + log.Println("Read all participants...") err = client.ReadAll(&participants) if err != nil { panic(err) } + if *participantsCSV != "" { + + 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 { + + } + log.Println(usernames) + } + var updated bool if *unsubscribeAll { @@ -54,27 +95,47 @@ func main() { os.Exit(0) } - if *subscribe { + 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 + } + } + } + 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 regions_ids[participant.School.Region.Name] == nil { + panic(fmt.Errorf("Region not found for participant %v", participant)) } + 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 + } if updated { id, err := client.Update(participant) if err != nil { @@ -84,6 +145,40 @@ func main() { updated = false } } + + } else { + 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 { diff --git a/scripts/subscribe_participants/regions_contests.yaml b/scripts/subscribe_participants/regions_contests.yaml index bfd4f7a6..47637d94 100644 --- a/scripts/subscribe_participants/regions_contests.yaml +++ b/scripts/subscribe_participants/regions_contests.yaml @@ -1,7 +1,19 @@ contests: - - id: 1 - regions: [Basilicata,Calabria,Lombardia,Piemonte,Val d'Aosta,Veneto] - - id: 2 - regions: [Campania,Emilia Romagna,Friuli Venezia Giulia,Liguria,Sardegna,Toscana,Trentino Alto Adige] - - id: 3 - regions: [Abruzzo,Lazio,Marche,Molise,Puglia, Sicilia,Umbria] \ No newline at end of file + - id: 22 + category: Junior + regions: [Basilicata,Calabria,Lombardia,Piemonte,Valle d'Aosta,Veneto] + - id: 23 + category: Senior + regions: [Basilicata,Calabria,Lombardia,Piemonte,Valle d'Aosta,Veneto] + - id: 26 + category: Junior + regions: [Campania,Emilia-Romagna,Friuli-Venezia Giulia,Liguria,Sardegna,Toscana,Trentino-Alto Adige] + - id: 27 + category: Senior + regions: [Campania,Emilia-Romagna,Friuli-Venezia Giulia,Liguria,Sardegna,Toscana,Trentino-Alto Adige] + - id: 28 + category: Junior + regions: [Abruzzo,Lazio,Marche,Molise,Puglia,Sicilia,Umbria] + - id: 25 + category: Senior + regions: [Abruzzo,Lazio,Marche,Molise,Puglia,Sicilia,Umbria] From d395edc5ae3b77861f6f8ec6b391359d80de20be Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Tue, 3 May 2022 14:58:11 +0200 Subject: [PATCH 4/6] [auto] bumped version to 0.9.9-138-g9ab95fd-master --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 5c5d296a..20bfff71 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.9.9-136-g463bc9d-master +0.9.9-138-g9ab95fd-master From a15cb8beb077951de6a4ef0c0c37f0db7e136681 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Tue, 3 May 2022 15:15:45 +0200 Subject: [PATCH 5/6] Working on a new version of the subscribe_participants script --- scripts/subscribe_participants/main.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/subscribe_participants/main.go b/scripts/subscribe_participants/main.go index 67b5e63c..cf8a7184 100644 --- a/scripts/subscribe_participants/main.go +++ b/scripts/subscribe_participants/main.go @@ -57,16 +57,16 @@ func main() { panic(err) } - participants := make([]*orm.Participant, 0) + allParticipants := make([]*orm.Participant, 0) log.Println("Read all participants...") - err = client.ReadAll(&participants) + err = client.ReadAll(&allParticipants) if err != nil { panic(err) } if *participantsCSV != "" { - + participants := make([]*orm.Participant, 0) users := make([]User, 0) data, err := ioutil.ReadFile(*participantsCSV) if err != nil { @@ -76,9 +76,13 @@ func main() { panic(err) } for _, u := range users { - + for _, p := range allParticipants { + if u.Username == p.User.Username { + participants = append(participants, p) + } + } } - log.Println(usernames) + log.Println(participants) } var updated bool From 89d9efb7a593e774c87fc0c07b18060485174f37 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Tue, 3 May 2022 15:15:45 +0200 Subject: [PATCH 6/6] [auto] bumped version to 0.9.9-140-ga15cb8b-master --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 20bfff71..38eae7b0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.9.9-138-g9ab95fd-master +0.9.9-140-ga15cb8b-master