Working on fake data for tests
This commit is contained in:
parent
b9fadab25b
commit
c07bb4ded9
7 changed files with 8593 additions and 8507 deletions
|
@ -15,6 +15,7 @@ import (
|
||||||
"git.andreafazzi.eu/andrea/oef/i18n"
|
"git.andreafazzi.eu/andrea/oef/i18n"
|
||||||
"git.andreafazzi.eu/andrea/oef/orm"
|
"git.andreafazzi.eu/andrea/oef/orm"
|
||||||
"git.andreafazzi.eu/andrea/oef/renderer"
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
"github.com/jinzhu/inflection"
|
"github.com/jinzhu/inflection"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -117,6 +118,28 @@ func (c *Client) ReadAll(model interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) Read(model orm.IDer) (interface{}, error) {
|
||||||
|
var response renderer.JsonResponse
|
||||||
|
|
||||||
|
data, err := c.SendRequest("GET", fmt.Sprintf("/api/%s/%d?format=json", pluralizedModelName(model), model.GetID()), nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(data, &response); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if string(response.Error) != "" {
|
||||||
|
return nil, errors.New(string(response.Error))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(response.Result, &model); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) Create(model interface{}) (uint, error) {
|
func (c *Client) Create(model interface{}) (uint, error) {
|
||||||
var response renderer.JsonResponse
|
var response renderer.JsonResponse
|
||||||
data, err := json.Marshal(model)
|
data, err := json.Marshal(model)
|
||||||
|
@ -203,6 +226,11 @@ func (c *Client) DeleteAllFunc(model interface{}, testFn func(interface{}) bool)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) Exists(model orm.IDer) bool {
|
||||||
|
_, err := c.Read(model)
|
||||||
|
return err != gorm.ErrRecordNotFound
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: refactor this (it's duplicated across the code)
|
// FIXME: refactor this (it's duplicated across the code)
|
||||||
func pluralizedModelName(value interface{}) string {
|
func pluralizedModelName(value interface{}) string {
|
||||||
return inflection.Plural(strings.ToLower(orm.ModelName(value)))
|
return inflection.Plural(strings.ToLower(orm.ModelName(value)))
|
||||||
|
|
31
orm/orm.go
31
orm/orm.go
|
@ -27,28 +27,6 @@ var (
|
||||||
store = sessions.NewCookieStore([]byte(config.Config.Keys.CookieStoreKey))
|
store = sessions.NewCookieStore([]byte(config.Config.Keys.CookieStoreKey))
|
||||||
|
|
||||||
categories = []string{"Junior", "Senior"}
|
categories = []string{"Junior", "Senior"}
|
||||||
regions = []string{
|
|
||||||
"Abruzzo",
|
|
||||||
"Basilicata",
|
|
||||||
"Calabria",
|
|
||||||
"Campania",
|
|
||||||
"Emilia-Romagna",
|
|
||||||
"Friuli-Venezia Giulia",
|
|
||||||
"Lazio",
|
|
||||||
"Liguria",
|
|
||||||
"Lombardia",
|
|
||||||
"Marche",
|
|
||||||
"Molise",
|
|
||||||
"Piemonte",
|
|
||||||
"Puglia",
|
|
||||||
"Sardegna",
|
|
||||||
"Sicilia",
|
|
||||||
"Toscana",
|
|
||||||
"Trentino-Alto Adige",
|
|
||||||
"Umbria",
|
|
||||||
"Valle d'Aosta",
|
|
||||||
"Veneto",
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -79,15 +57,6 @@ func CreateCategories() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateRegions() {
|
|
||||||
for _, name := range regions {
|
|
||||||
var region Region
|
|
||||||
if err := currDB.FirstOrCreate(®ion, Region{Name: name}).Error; err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Use(db *gorm.DB) {
|
func Use(db *gorm.DB) {
|
||||||
currDB = db
|
currDB = db
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,12 +7,68 @@ import (
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
regions = []string{
|
||||||
|
"Abruzzo",
|
||||||
|
"Basilicata",
|
||||||
|
"Calabria",
|
||||||
|
"Campania",
|
||||||
|
"Emilia-Romagna",
|
||||||
|
"Friuli-Venezia Giulia",
|
||||||
|
"Lazio",
|
||||||
|
"Liguria",
|
||||||
|
"Lombardia",
|
||||||
|
"Marche",
|
||||||
|
"Molise",
|
||||||
|
"Piemonte",
|
||||||
|
"Puglia",
|
||||||
|
"Sardegna",
|
||||||
|
"Sicilia",
|
||||||
|
"Toscana",
|
||||||
|
"Trentino-Alto Adige",
|
||||||
|
"Umbria",
|
||||||
|
"Valle d'Aosta",
|
||||||
|
"Veneto",
|
||||||
|
}
|
||||||
|
regionsMap = map[string]uint{
|
||||||
|
"Abruzzo": 1,
|
||||||
|
"Basilicata": 2,
|
||||||
|
"Calabria": 3,
|
||||||
|
"Campania": 4,
|
||||||
|
"Emilia-Romagna": 5,
|
||||||
|
"Friuli-Venezia Giulia": 6,
|
||||||
|
"Lazio": 7,
|
||||||
|
"Liguria": 8,
|
||||||
|
"Lombardia": 9,
|
||||||
|
"Marche": 10,
|
||||||
|
"Molise": 11,
|
||||||
|
"Piemonte": 12,
|
||||||
|
"Puglia": 13,
|
||||||
|
"Sardegna": 14,
|
||||||
|
"Sicilia": 15,
|
||||||
|
"Toscana": 16,
|
||||||
|
"Trentino-Alto Adige": 17,
|
||||||
|
"Umbria": 18,
|
||||||
|
"Valle d'Aosta": 19,
|
||||||
|
"Veneto": 20,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
type Region struct {
|
type Region struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
|
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateRegions() {
|
||||||
|
for _, name := range regions {
|
||||||
|
var region Region
|
||||||
|
if err := currDB.FirstOrCreate(®ion, Region{Name: name}).Error; err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (model *Region) GetID() uint { return model.ID }
|
func (model *Region) GetID() uint { return model.ID }
|
||||||
|
|
||||||
func (model *Region) String() string {
|
func (model *Region) String() string {
|
||||||
|
|
|
@ -12,6 +12,8 @@ import (
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type RegionID uint
|
||||||
|
|
||||||
type School struct {
|
type School struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
|
|
||||||
|
@ -31,7 +33,7 @@ type School struct {
|
||||||
ContestResponsibleFirstname string
|
ContestResponsibleFirstname string
|
||||||
|
|
||||||
UserID uint
|
UserID uint
|
||||||
RegionID uint `schema:"region_id"`
|
RegionID RegionID `schema:"region_id"`
|
||||||
|
|
||||||
Region *Region
|
Region *Region
|
||||||
User *User
|
User *User
|
||||||
|
@ -41,6 +43,11 @@ type School struct {
|
||||||
AllRegions []*Region `gorm:"-"`
|
AllRegions []*Region `gorm:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (id *RegionID) UnmarshalCSV(csv string) error {
|
||||||
|
*id = RegionID(regionsMap[strings.Title(strings.ToLower(csv))])
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (model *School) GetID() uint { return model.ID }
|
func (model *School) GetID() uint { return model.ID }
|
||||||
|
|
||||||
func (model *School) String() string {
|
func (model *School) String() string {
|
||||||
|
@ -190,7 +197,7 @@ func (model *School) Update(args map[string]string, w http.ResponseWriter, r *ht
|
||||||
}
|
}
|
||||||
|
|
||||||
school.SelectedRegion = make(map[uint]string)
|
school.SelectedRegion = make(map[uint]string)
|
||||||
school.SelectedRegion[school.RegionID] = "selected"
|
school.SelectedRegion[uint(school.RegionID)] = "selected"
|
||||||
|
|
||||||
return school, nil
|
return school, nil
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -3,13 +3,16 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"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/config"
|
"git.andreafazzi.eu/andrea/oef/config"
|
||||||
"git.andreafazzi.eu/andrea/oef/orm"
|
"git.andreafazzi.eu/andrea/oef/orm"
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
|
"github.com/gocarina/gocsv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type importer struct {
|
type importer struct {
|
||||||
|
@ -21,7 +24,8 @@ func newImporter(c *client.Client) *importer {
|
||||||
return &importer{
|
return &importer{
|
||||||
c,
|
c,
|
||||||
map[string]func(*client.Client, string) error{
|
map[string]func(*client.Client, string) error{
|
||||||
"contest": importContest,
|
"contests": importContest,
|
||||||
|
"schools": importSchools,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,6 +38,28 @@ func (i *importer) runImport(importer, filename string) error {
|
||||||
return f(i.client, filename)
|
return f(i.client, filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func importSchools(client *client.Client, filename string) error {
|
||||||
|
var schools []*orm.School
|
||||||
|
data, err := ioutil.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := gocsv.Unmarshal(strings.NewReader(string(data)), &schools); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range schools {
|
||||||
|
id, err := client.Create(s)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
log.Println("Create school with ID", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func importContest(client *client.Client, filename string) error {
|
func importContest(client *client.Client, filename string) error {
|
||||||
log.Println("Reading contest data from toml file...")
|
log.Println("Reading contest data from toml file...")
|
||||||
var contest *orm.Contest
|
var contest *orm.Contest
|
||||||
|
|
16942
scripts/import/testdata/schools.csv
vendored
16942
scripts/import/testdata/schools.csv
vendored
File diff suppressed because it is too large
Load diff
|
@ -52,9 +52,9 @@
|
||||||
<dd class="col-sm-9">{{.Data.EmailSentDate|prettyDate}} alle ore {{.Data.EmailSentDate|prettyTime}}</dd>
|
<dd class="col-sm-9">{{.Data.EmailSentDate|prettyDate}} alle ore {{.Data.EmailSentDate|prettyTime}}</dd>
|
||||||
{{end}}
|
{{end}}
|
||||||
<dt class="col-sm-3">Username</dt>
|
<dt class="col-sm-3">Username</dt>
|
||||||
<dd class="col-sm-9">{{.Data.User.Username}}</dd>
|
<dd class="col-sm-9"><span class="text-monospace">{{.Data.User.Username}}</span></dd>
|
||||||
<dt class="col-sm-3">Password</dt>
|
<dt class="col-sm-3">Password</dt>
|
||||||
<dd class="col-sm-9">{{.Data.User.Password}}</dd>
|
<dd class="col-sm-9"><span class="text-monospace">{{.Data.User.Password}}</span></dd>
|
||||||
|
|
||||||
{{if $isAdmin}}
|
{{if $isAdmin}}
|
||||||
{{if $creatorUser:=.Data.CreatedBy}}
|
{{if $creatorUser:=.Data.CreatedBy}}
|
||||||
|
|
Loading…
Reference in a new issue