From cb841a8b013414998f3dfbac342f771bd193c3f9 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Wed, 10 Nov 2021 05:45:53 +0100 Subject: [PATCH] Add export_credentials script --- scripts/export_credentials/.gitignore | 5 +++ scripts/export_credentials/main.go | 55 +++++++++++++++++++++++++++ scripts/rank/main.go | 21 +++++++++- 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 scripts/export_credentials/.gitignore create mode 100644 scripts/export_credentials/main.go diff --git a/scripts/export_credentials/.gitignore b/scripts/export_credentials/.gitignore new file mode 100644 index 00000000..41a21e84 --- /dev/null +++ b/scripts/export_credentials/.gitignore @@ -0,0 +1,5 @@ +test_credentials +*.csv +*.xlsx +export_credentials + diff --git a/scripts/export_credentials/main.go b/scripts/export_credentials/main.go new file mode 100644 index 00000000..fb6efbb3 --- /dev/null +++ b/scripts/export_credentials/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "flag" + "log" + "os" + + "git.andreafazzi.eu/andrea/oef/client" + "git.andreafazzi.eu/andrea/oef/orm" + "github.com/gocarina/gocsv" +) + +type Credential struct { + ID uint + Username string + Password string +} + +func main() { + username := flag.String("username", "admin", "Username") + password := flag.String("password", "admin", "Password") + output := flag.String("output", "rank.csv", "Output filename") + + flag.Parse() + + client, err := client.Dial(flag.Arg(0), *username, *password) + if err != nil { + panic(err) + } + + users := make([]*orm.User, 0) + credentials := make([]*Credential, 0) + + log.Println("Get all users...") + + err = client.ReadAll(&users) + if err != nil { + panic(err) + } + + for _, user := range users { + if user.Role == "participant" { + credentials = append(credentials, &Credential{user.ID, user.Username, user.Password}) + } + } + + f, err := os.Create(*output) + if err != nil { + panic(err) + } + defer f.Close() + + gocsv.MarshalFile(credentials, f) + +} diff --git a/scripts/rank/main.go b/scripts/rank/main.go index 2fd11794..8b5d7448 100644 --- a/scripts/rank/main.go +++ b/scripts/rank/main.go @@ -78,6 +78,7 @@ func main() { password := flag.String("password", "admin", "Password") output := flag.String("output", "rank.csv", "Output filename") contestId := flag.Int("id", 0, "Contest ID") + all := flag.Bool("all", false, "Rank all participants") flag.Parse() @@ -119,7 +120,7 @@ func main() { panic(err) } - if r.Score > 0 && r.Duration > 0 { + if r.Score > 0 && r.Duration > 0 && !*all { school, err := findSchool(schools, r.Participant.SchoolID) if err != nil { panic(err) @@ -136,6 +137,24 @@ func main() { Duration: uint(r.Duration / time.Second), }, ) + } else { + school, err := findSchool(schools, r.Participant.SchoolID) + if err != nil { + panic(err) + } + csvResponses = append( + csvResponses, + &Response{ + Firstname: r.Participant.Firstname, + Lastname: r.Participant.Lastname, + FiscalCode: r.Participant.FiscalCode, + School: school.String(), + Region: school.Region.String(), + Score: r.Score, + Duration: uint(r.Duration / time.Second), + }, + ) + } }