Last active
November 9, 2022 22:17
-
-
Save lucmski/7766c50d39ac305edee8d192f1efb047 to your computer and use it in GitHub Desktop.
for browsing/editing data from https://github.com/mdiplo/Medias_francais [WIP]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package: github.com/lucmski/Medias_francais | |
import: | |
- package: github.com/gosimple/slug | |
- package: github.com/jinzhu/gorm | |
subpackages: | |
- dialects/mysql | |
- package: github.com/k0kubun/pp | |
- package: github.com/mattn/go-sqlite3 | |
- package: github.com/qor/admin | |
- package: github.com/qor/media | |
subpackages: | |
- oss |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/csv" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
"github.com/jinzhu/gorm" | |
_ "github.com/jinzhu/gorm/dialects/mysql" | |
_ "github.com/mattn/go-sqlite3" | |
"github.com/qor/admin" | |
"github.com/qor/media" | |
"github.com/qor/media/oss" | |
// "github.com/qor/filesystem" | |
// "github.com/qor/qor" | |
"github.com/gosimple/slug" | |
"github.com/k0kubun/pp" | |
) | |
// Create a GORM-backend model | |
type Entity struct { | |
gorm.Model | |
Name string | |
TypeLabel string | |
TypeCode string | |
Rank string | |
TypeMedia Media | |
Periodicity string | |
MediaScale string | |
Comments []Comment | |
Slug string | |
Owners []Owner `gorm:"many2many:media_social_owners;"` | |
SocialNetworks []SocialNetwork `gorm:"many2many:media_social_pages;"` | |
Logo oss.OSS | |
} | |
// | |
// Create another GORM-backend model | |
type Owner struct { | |
gorm.Model | |
Owner string | |
Stack string | |
Target string | |
TargetSlug string | |
Sources []Link | |
DatePublication string | |
DateConsultation string | |
Slug string | |
SocialNetworks []SocialNetwork `gorm:"many2many:media_social_owner_pages;"` | |
Logo oss.OSS | |
} | |
// Create a GORM-backend model | |
type SocialPage struct { | |
gorm.Model | |
Href string | |
SocialNetwork SocialNetwork | |
Screenshot oss.OSS | |
} | |
// Create a GORM-backend model | |
type SocialNetwork struct { | |
gorm.Model | |
Name string | |
Username string | |
MainURL string | |
Slug string | |
Screenshot oss.OSS | |
} | |
type Link struct { | |
gorm.Model | |
Href string | |
Visisted bool | |
Screenshot oss.OSS | |
} | |
type Label struct { | |
gorm.Model | |
Value string | |
Slug string | |
Disabled bool | |
Logo oss.OSS | |
} | |
type Code struct { | |
gorm.Model | |
Value string | |
Slug string | |
Disabled bool | |
} | |
type Cycle struct { | |
gorm.Model | |
Value string | |
Slug string | |
Disabled bool | |
Logo oss.OSS | |
} | |
type Media struct { | |
gorm.Model | |
Value string | |
Slug string | |
Disabled bool | |
Logo oss.OSS | |
} | |
type Comment struct { | |
gorm.Model | |
Content string | |
Disabled bool | |
Medias []oss.OSS | |
} | |
var DB *gorm.DB | |
func main() { | |
// DB, _ = gorm.Open("sqlite3", "mdiplo.db") | |
var err error | |
// DB, err = gorm.Open("mysql", fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?parseTime=True&loc=Local", "root", "da33ve79T!", "127.0.0.1", 3306, "medias")) | |
DB, err = gorm.Open("sqlite3", "mdiplo.db") | |
if err != nil { | |
fmt.Println("error", err) | |
os.Exit(1) | |
} | |
media.RegisterCallbacks(DB) | |
DB.AutoMigrate(&Entity{}, &Owner{}, &SocialPage{}, &SocialNetwork{}, &Link{}, &Cycle{}, &Label{}, &Code{}, &Comment{}, &Media{}) | |
// Initalize | |
Admin := admin.New(&admin.AdminConfig{ | |
SiteName: "Le Monde Diplomatique", | |
// Auth: auth.AdminAuth{}, | |
DB: DB, | |
}) | |
// funcmapmaker.AddFuncMapMaker(auth.Auth.Config.Render) | |
// Allow to use Admin to manage User, Product | |
Admin.AddResource(&Entity{}, &admin.Config{Menu: []string{"Entity Management"}}) | |
// entityType = order.Meta(&admin.Meta{Name: "TypeMedia"}) | |
// entityType = order.Meta(&admin.Meta{Name: "TypeMedia"}) | |
// ID Name TypeLabel TypeCode Rank TypeMedia Periodicity MediaScale Comments Slug Owners SocialNetworks Logo | |
// Structure the new form to make it tidy and clean with `Section` | |
/* | |
entity.NewAttrs( | |
&admin.Section{ | |
Title: "Basic Information", | |
Rows: [][]string{ | |
{"Name"}, | |
{"TypeLabel.Value", "Rank", "MediaScale"}, | |
}, | |
}, | |
"SocialNetworks", | |
&admin.Section{ | |
Title: "Organization", | |
Rows: [][]string{ | |
{"TypeMedia", "Periodicity", "MadeCountry"}, | |
}, | |
}, | |
"Comments", | |
"Slug", | |
) | |
*/ | |
Admin.AddResource(&Owner{}, &admin.Config{Menu: []string{"Entity Management"}}) | |
Admin.AddResource(&SocialPage{}, &admin.Config{Menu: []string{"Social Media"}}) | |
Admin.AddResource(&SocialNetwork{}, &admin.Config{Menu: []string{"Links Management"}}) | |
Admin.AddResource(&Link{}, &admin.Config{Menu: []string{"Links Management"}}) | |
Admin.AddResource(&Cycle{}, &admin.Config{Menu: []string{"Media Management"}}) | |
Admin.AddResource(&Label{}, &admin.Config{Menu: []string{"Media Management"}}) | |
Admin.AddResource(&Media{}, &admin.Config{Menu: []string{"Media Management"}}) | |
// Admin.AddResource(&Code{}) | |
Admin.AddResource(&Comment{}, &admin.Config{Menu: []string{"Worflow Management"}}) | |
// initalize an HTTP request multiplexer | |
mux := http.NewServeMux() | |
// Mount admin interface to mux | |
Admin.MountTo("/admin", mux) | |
importMDiplo("relations_medias_francais.tsv") | |
importMDiplo("medias_francais.tsv") | |
fmt.Println("Listening on: 9000") | |
http.ListenAndServe(":9000", mux) | |
/* | |
Application.Use(static.New(&static.Config{ | |
Prefixs: []string{"/system"}, | |
Handler: utils.FileServer(http.Dir(filepath.Join(config.Root, "public"))), | |
})) | |
Application.Use(static.New(&static.Config{ | |
Prefixs: []string{"javascripts", "stylesheets", "images", "dist", "fonts", "vendors", "favicon.ico"}, | |
Handler: bindatafs.AssetFS.FileServer(http.Dir("public"), "javascripts", "stylesheets", "images", "dist", "fonts", "vendors", "favicon.ico"), | |
})) | |
if *compileTemplate { | |
bindatafs.AssetFS.Compile() | |
} else { | |
fmt.Printf("Listening on: %v\n", config.Config.Port) | |
if config.Config.HTTPS { | |
if err := http.ListenAndServeTLS(fmt.Sprintf(":%d", config.Config.Port), "config/local_certs/server.crt", "config/local_certs/server.key", Application.NewServeMux()); err != nil { | |
panic(err) | |
} | |
} else { | |
if err := http.ListenAndServe(fmt.Sprintf(":%d", config.Config.Port), Application.NewServeMux()); err != nil { | |
panic(err) | |
} | |
} | |
} | |
*/ | |
} | |
// https://github.com/qor/qor-example/blob/api/config/db/seeds/seeds.go#L160 | |
// | |
func importMDiplo(filePath string) { | |
csvFile, err := os.Open(filePath) | |
if err != nil { | |
fmt.Println(err) | |
} | |
defer csvFile.Close() | |
reader := csv.NewReader(csvFile) | |
reader.Comma = '\t' // Use tab-delimited instead of comma <---- here! | |
reader.FieldsPerRecord = -1 | |
csvData, err := reader.ReadAll() | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
for _, i := range csvData { | |
switch filePath { | |
case "medias_francais.tsv": | |
media := &Entity{ | |
Name: i[1], | |
TypeLabel: i[2], | |
TypeCode: i[3], | |
Rank: i[4], | |
Periodicity: i[6], | |
MediaScale: i[7], | |
Slug: slug.Make(i[1]), | |
SocialNetworks: make([]SocialNetwork, 0), | |
} | |
if slug.Make(i[2]) != "" { | |
label := &Label{ | |
Value: i[2], | |
Slug: slug.Make(i[2]), | |
} | |
var labelExists Label | |
if DB.Where("slug = ?", slug.Make(i[2])).First(&labelExists).RecordNotFound() { | |
// DB.Create(&label) | |
pp.Println(&label) | |
} | |
label.ID = labelExists.ID | |
label.CreatedAt = labelExists.CreatedAt | |
if err := DB.Save(label).Error; err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
} | |
if slug.Make(i[3]) != "" { | |
code := &Code{ | |
Value: i[3], | |
Slug: slug.Make(i[3]), | |
} | |
var codeExists Code | |
if DB.Where("slug = ?", slug.Make(i[3])).First(&codeExists).RecordNotFound() { | |
// DB.Create(&code) | |
pp.Println(&code) | |
} | |
code.ID = codeExists.ID | |
code.CreatedAt = codeExists.CreatedAt | |
if err := DB.Save(code).Error; err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
} | |
if slug.Make(i[6]) != "" { | |
cycle := &Cycle{ | |
Value: i[6], | |
Slug: slug.Make(i[6]), | |
} | |
var cycleExists Cycle | |
if DB.Where("slug = ?", slug.Make(i[6])).First(&cycleExists).RecordNotFound() { | |
// DB.Create(&cycle) | |
pp.Println(&cycle) | |
} | |
cycle.ID = cycleExists.ID | |
cycle.CreatedAt = cycleExists.CreatedAt | |
if err := DB.Save(cycle).Error; err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
} | |
if slug.Make(i[5]) != "" { | |
m := &Media{ | |
Value: i[5], | |
Slug: slug.Make(i[5]), | |
} | |
var mediaExists Media | |
if DB.Where("slug = ?", slug.Make(i[5])).First(&mediaExists).RecordNotFound() { | |
// DB.Create(&m) | |
pp.Println(&m) | |
} | |
m.ID = mediaExists.ID | |
m.CreatedAt = mediaExists.CreatedAt | |
if err := DB.Save(m).Error; err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
media.TypeMedia = *m | |
} | |
if slug.Make(i[8]) != "" { | |
c := &Comment{ | |
Content: i[8], | |
} | |
if DB.NewRecord(c) { | |
DB.Create(&c) | |
pp.Println(&c) | |
} | |
} | |
if i[8] != "" { | |
media.Comments = []Comment{{Content: i[8]}} | |
} | |
if DB.NewRecord(media) { | |
DB.Create(&media) | |
} | |
pp.Println(&media) | |
case "relations_medias_francais.tsv": | |
if slug.Make(i[1]) != "" { | |
owner := &Owner{ | |
Owner: i[1], | |
Stack: i[2], | |
Target: i[3], | |
DatePublication: i[5], | |
DateConsultation: i[6], | |
Slug: slug.Make(i[1]), | |
SocialNetworks: make([]SocialNetwork, 0), | |
} | |
if i[4] != "" { | |
owner.Sources = []Link{{Href: i[4]}} | |
//media.Comments[0] = i[8] | |
} | |
if DB.NewRecord(owner) { | |
DB.Create(&owner) | |
} | |
pp.Println(&owner) | |
} | |
default: | |
fmt.Println(i) | |
} | |
} | |
} | |
func findEntityByName(slug string) *Owner { | |
o := &Owner{} | |
if err := DB.Where(&Owner{Slug: slug}).First(o).Error; err != nil { | |
log.Printf("can't find collection with name = %q, got err %v\n", slug, err) | |
} | |
return o | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment