Preload User when reading all participants
This commit is contained in:
parent
6151284f8e
commit
9ab95fd063
3 changed files with 128 additions and 20 deletions
|
@ -330,6 +330,7 @@ func (model *Participant) ReadAll(db *Database, args map[string]string, w http.R
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := db._db.
|
if err := db._db.
|
||||||
|
Preload("User").
|
||||||
Preload("Category").
|
Preload("Category").
|
||||||
Preload("School").
|
Preload("School").
|
||||||
Preload("School.Region").
|
Preload("School.Region").
|
||||||
|
|
|
@ -2,11 +2,30 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.andreafazzi.eu/andrea/oef/client"
|
"git.andreafazzi.eu/andrea/oef/client"
|
||||||
"git.andreafazzi.eu/andrea/oef/orm"
|
"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 {
|
func alreadySubscribed(participant *orm.Participant, contestId uint) bool {
|
||||||
|
@ -22,11 +41,15 @@ func main() {
|
||||||
username := flag.String("username", "admin", "Username")
|
username := flag.String("username", "admin", "Username")
|
||||||
password := flag.String("password", "admin", "Password")
|
password := flag.String("password", "admin", "Password")
|
||||||
subscribe := flag.Bool("subscribe", true, "Subscribe to the given contest")
|
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")
|
unsubscribeAll := flag.Bool("unsubscribe-all", false, "Unsubscribe all participants from all contests")
|
||||||
|
|
||||||
juniorContestId := flag.Int("jid", 0, "Contest ID for Junior category")
|
juniorContestId := flag.Int("jid", 0, "Contest ID for Junior category")
|
||||||
seniorContestId := flag.Int("sid", 0, "Contest ID for Senior 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()
|
flag.Parse()
|
||||||
|
|
||||||
client, err := client.Dial(flag.Arg(0), *username, *password)
|
client, err := client.Dial(flag.Arg(0), *username, *password)
|
||||||
|
@ -35,11 +58,29 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
participants := make([]*orm.Participant, 0)
|
participants := make([]*orm.Participant, 0)
|
||||||
|
|
||||||
|
log.Println("Read all participants...")
|
||||||
err = client.ReadAll(&participants)
|
err = client.ReadAll(&participants)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
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
|
var updated bool
|
||||||
|
|
||||||
if *unsubscribeAll {
|
if *unsubscribeAll {
|
||||||
|
@ -54,27 +95,47 @@ func main() {
|
||||||
os.Exit(0)
|
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 _, participant := range participants {
|
||||||
for _, contest := range participant.Contests {
|
for _, contest := range participant.Contests {
|
||||||
participant.ContestIDs = append(participant.ContestIDs, contest.ID)
|
participant.ContestIDs = append(participant.ContestIDs, contest.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if *juniorContestId > 0 && participant.Category.Name == "Junior" {
|
if regions_ids[participant.School.Region.Name] == nil {
|
||||||
if !alreadySubscribed(participant, uint(*juniorContestId)) {
|
panic(fmt.Errorf("Region not found for participant %v", participant))
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
if updated {
|
||||||
id, err := client.Update(participant)
|
id, err := client.Update(participant)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -84,6 +145,40 @@ func main() {
|
||||||
updated = false
|
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 {
|
if !updated {
|
||||||
|
|
|
@ -1,7 +1,19 @@
|
||||||
contests:
|
contests:
|
||||||
- id: 1
|
- id: 22
|
||||||
regions: [Basilicata,Calabria,Lombardia,Piemonte,Val d'Aosta,Veneto]
|
category: Junior
|
||||||
- id: 2
|
regions: [Basilicata,Calabria,Lombardia,Piemonte,Valle d'Aosta,Veneto]
|
||||||
regions: [Campania,Emilia Romagna,Friuli Venezia Giulia,Liguria,Sardegna,Toscana,Trentino Alto Adige]
|
- id: 23
|
||||||
- id: 3
|
category: Senior
|
||||||
regions: [Abruzzo,Lazio,Marche,Molise,Puglia, Sicilia,Umbria]
|
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]
|
||||||
|
|
Loading…
Reference in a new issue