Allow to change default section via ui

This commit is contained in:
Jan-Lukas Else 2022-07-17 08:36:10 +02:00
parent 6cbd8fb2ce
commit 2178b2e223
10 changed files with 112 additions and 42 deletions

View File

@ -19,7 +19,7 @@ RUN go build -ldflags '-w -s' -o GoBlog
FROM build as test FROM build as test
RUN go test -timeout 60s -cover ./... RUN go test -timeout 300s -failfast -cover ./...
FROM alpine:3.16 as base FROM alpine:3.16 as base

View File

@ -384,12 +384,8 @@ func (a *goBlog) initConfig(logging bool) error {
} }
// Check if default blog is set // Check if default blog is set
if a.cfg.DefaultBlog == "" { if a.cfg.DefaultBlog == "" {
if len(a.cfg.Blogs) == 1 { // Set default blog to the only blog that is configured
// Set default blog to the only blog that is configured a.cfg.DefaultBlog = lo.Keys(a.cfg.Blogs)[0]
a.cfg.DefaultBlog = lo.Keys(a.cfg.Blogs)[0]
} else {
return errors.New("no default blog configured")
}
} }
// Check if default blog exists // Check if default blog exists
if a.cfg.Blogs[a.cfg.DefaultBlog] == nil { if a.cfg.Blogs[a.cfg.DefaultBlog] == nil {
@ -423,13 +419,37 @@ func (a *goBlog) initConfig(logging bool) error {
return err return err
} }
// Check config for each blog // Check config for each blog
for _, blog := range a.cfg.Blogs { for blog, bc := range a.cfg.Blogs {
// Check sections and add section if none exists
if len(bc.Sections) == 0 {
bc.Sections = createDefaultSections()
if err = a.saveAllSections(); err != nil {
return err
}
}
// Check default section
if defaultSection, err := a.getSettingValue(settingNameWithBlog(blog, defaultSectionSetting)); err != nil {
// Failed to read value
return err
} else if defaultSection == "" {
// No value defined in database
if _, ok := bc.Sections[bc.DefaultSection]; !ok {
bc.DefaultSection = lo.Keys(bc.Sections)[0]
}
// Save to database
if err = a.saveSettingValue(settingNameWithBlog(blog, defaultSectionSetting), bc.DefaultSection); err != nil {
return err
}
} else {
// Set value from database
bc.DefaultSection = defaultSection
}
// Check if language is set // Check if language is set
if blog.Lang == "" { if bc.Lang == "" {
blog.Lang = "en" bc.Lang = "en"
} }
// Blogroll // Blogroll
if br := blog.Blogroll; br != nil && br.Enabled && br.Opml == "" { if br := bc.Blogroll; br != nil && br.Enabled && br.Opml == "" {
br.Enabled = false br.Enabled = false
} }
} }
@ -484,18 +504,20 @@ func createDefaultBlog() *configBlog {
Lang: "en", Lang: "en",
Title: "My Blog", Title: "My Blog",
Description: "Welcome to my blog.", Description: "Welcome to my blog.",
Sections: map[string]*configSection{
"posts": {
Title: "Posts",
},
},
Taxonomies: []*configTaxonomy{ Taxonomies: []*configTaxonomy{
{ {
Name: "tags", Name: "tags",
Title: "Tags", Title: "Tags",
}, },
}, },
DefaultSection: "posts", }
}
func createDefaultSections() map[string]*configSection {
return map[string]*configSection{
"posts": {
Title: "Posts",
},
} }
} }

2
go.mod
View File

@ -13,7 +13,7 @@ require (
github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b
github.com/carlmjohnson/requests v0.22.3 github.com/carlmjohnson/requests v0.22.3
github.com/cretz/bine v0.2.0 github.com/cretz/bine v0.2.0
github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f github.com/dchest/captcha v1.0.0
github.com/dgraph-io/ristretto v0.1.0 github.com/dgraph-io/ristretto v0.1.0
github.com/disintegration/imaging v1.6.2 github.com/disintegration/imaging v1.6.2
github.com/dmulholl/mp3lib v1.0.0 github.com/dmulholl/mp3lib v1.0.0

4
go.sum
View File

@ -120,8 +120,8 @@ github.com/cretz/bine v0.2.0/go.mod h1:WU4o9QR9wWp8AVKtTM1XD5vUHkEqnf2vVSo6dBqbe
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f h1:q/DpyjJjZs94bziQ7YkBmIlpqbVP7yw179rnzoNVX1M= github.com/dchest/captcha v1.0.0 h1:vw+bm/qMFvTgcjQlYVTuQBJkarm5R0YSsDKhm1HZI2o=
github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f/go.mod h1:QGrK8vMWWHQYQ3QU9bw9Y9OPNfxccGzfb41qjvVeXtY= github.com/dchest/captcha v1.0.0/go.mod h1:7zoElIawLp7GUMLcj54K9kbw+jEyvz2K0FDdRRYhvWo=
github.com/dgraph-io/ristretto v0.1.0 h1:Jv3CGQHp9OjuMBSne1485aDpUkTKEcUqF+jm/LuerPI= github.com/dgraph-io/ristretto v0.1.0 h1:Jv3CGQHp9OjuMBSne1485aDpUkTKEcUqF+jm/LuerPI=
github.com/dgraph-io/ristretto v0.1.0/go.mod h1:fux0lOrBhrVCJd3lcTHsIJhq1T2rokOu6v9Vcb3Q9ug= github.com/dgraph-io/ristretto v0.1.0/go.mod h1:fux0lOrBhrVCJd3lcTHsIJhq1T2rokOu6v9Vcb3Q9ug=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA= github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=

View File

@ -454,5 +454,6 @@ func (a *goBlog) blogSettingsRouter(_ *configBlog) func(r chi.Router) {
r.Post(settingsDeleteSectionPath, a.settingsDeleteSection) r.Post(settingsDeleteSectionPath, a.settingsDeleteSection)
r.Post(settingsCreateSectionPath, a.settingsCreateSection) r.Post(settingsCreateSectionPath, a.settingsCreateSection)
r.Post(settingsUpdateSectionPath, a.settingsUpdateSection) r.Post(settingsUpdateSectionPath, a.settingsUpdateSection)
r.Post(settingsUpdateDefaultSectionPath, a.settingsUpdateDefaultSection)
} }
} }

View File

@ -17,8 +17,9 @@ func (a *goBlog) serveSettings(w http.ResponseWriter, r *http.Request) {
a.render(w, r, a.renderSettings, &renderData{ a.render(w, r, a.renderSettings, &renderData{
Data: &settingsRenderData{ Data: &settingsRenderData{
blog: blog, blog: blog,
sections: sections, sections: sections,
defaultSection: bc.DefaultSection,
}, },
}) })
} }
@ -74,7 +75,7 @@ func (a *goBlog) settingsCreateSection(w http.ResponseWriter, r *http.Request) {
Name: sectionName, Name: sectionName,
Title: sectionTitle, Title: sectionTitle,
} }
err := a.addSection(blog, section) err := a.saveSection(blog, section)
if err != nil { if err != nil {
a.serveError(w, r, "Failed to insert section into database", http.StatusInternalServerError) a.serveError(w, r, "Failed to insert section into database", http.StatusInternalServerError)
return return
@ -112,7 +113,7 @@ func (a *goBlog) settingsUpdateSection(w http.ResponseWriter, r *http.Request) {
PathTemplate: sectionPathTemplate, PathTemplate: sectionPathTemplate,
ShowFull: sectionShowFull, ShowFull: sectionShowFull,
} }
err := a.updateSection(blog, sectionName, section) err := a.saveSection(blog, section)
if err != nil { if err != nil {
a.serveError(w, r, "Failed to update section in database", http.StatusInternalServerError) a.serveError(w, r, "Failed to update section in database", http.StatusInternalServerError)
return return
@ -127,3 +128,24 @@ func (a *goBlog) settingsUpdateSection(w http.ResponseWriter, r *http.Request) {
a.cache.purge() a.cache.purge()
http.Redirect(w, r, bc.getRelativePath(settingsPath), http.StatusFound) http.Redirect(w, r, bc.getRelativePath(settingsPath), http.StatusFound)
} }
const settingsUpdateDefaultSectionPath = "/updatedefaultsection"
func (a *goBlog) settingsUpdateDefaultSection(w http.ResponseWriter, r *http.Request) {
blog, bc := a.getBlog(r)
// Read values
newDefaultSection := r.FormValue("defaultsection")
// Check plausibility
if _, ok := bc.Sections[newDefaultSection]; !ok {
a.serveError(w, r, "Section unknown", http.StatusBadRequest)
return
}
// Update
err := a.saveSettingValue(settingNameWithBlog(blog, defaultSectionSetting), newDefaultSection)
if err != nil {
a.serveError(w, r, "Failed to update default section in database", http.StatusInternalServerError)
return
}
bc.DefaultSection = newDefaultSection
http.Redirect(w, r, bc.getRelativePath(settingsPath), http.StatusFound)
}

View File

@ -3,6 +3,15 @@ package main
import ( import (
"database/sql" "database/sql"
"errors" "errors"
"fmt"
)
func settingNameWithBlog(blog, name string) string {
return fmt.Sprintf("%s---%s", blog, name)
}
const (
defaultSectionSetting = "defaultsection"
) )
func (a *goBlog) getSettingValue(name string) (string, error) { func (a *goBlog) getSettingValue(name string) (string, error) {
@ -63,7 +72,7 @@ func (a *goBlog) saveAllSections() error {
for blog, bc := range a.cfg.Blogs { for blog, bc := range a.cfg.Blogs {
for k, s := range bc.Sections { for k, s := range bc.Sections {
s.Name = k s.Name = k
if err := a.addSection(blog, s); err != nil { if err := a.saveSection(blog, s); err != nil {
return err return err
} }
} }
@ -71,15 +80,22 @@ func (a *goBlog) saveAllSections() error {
return nil return nil
} }
func (a *goBlog) addSection(blog string, section *configSection) error { func (a *goBlog) saveSection(blog string, section *configSection) error {
_, err := a.db.exec( _, err := a.db.exec(
"insert into sections (blog, name, title, description, pathtemplate, showfull) values (@blog, @name, @title, @description, @pathtemplate, @showfull)", `
insert into sections (blog, name, title, description, pathtemplate, showfull) values (@blog, @name, @title, @description, @pathtemplate, @showfull)
on conflict (blog, name) do update set title = @title2, description = @description2, pathtemplate = @pathtemplate2, showfull = @showfull2
`,
sql.Named("blog", blog), sql.Named("blog", blog),
sql.Named("name", section.Name), sql.Named("name", section.Name),
sql.Named("title", section.Title), sql.Named("title", section.Title),
sql.Named("description", section.Description), sql.Named("description", section.Description),
sql.Named("pathtemplate", section.PathTemplate), sql.Named("pathtemplate", section.PathTemplate),
sql.Named("showfull", section.ShowFull), sql.Named("showfull", section.ShowFull),
sql.Named("title2", section.Title),
sql.Named("description2", section.Description),
sql.Named("pathtemplate2", section.PathTemplate),
sql.Named("showfull2", section.ShowFull),
) )
return err return err
} }
@ -88,16 +104,3 @@ func (a *goBlog) deleteSection(blog string, name string) error {
_, err := a.db.exec("delete from sections where blog = @blog and name = @name", sql.Named("blog", blog), sql.Named("name", name)) _, err := a.db.exec("delete from sections where blog = @blog and name = @name", sql.Named("blog", blog), sql.Named("name", name))
return err return err
} }
func (a *goBlog) updateSection(blog string, name string, section *configSection) error {
_, err := a.db.exec(
"update sections set title = @title, description = @description, pathtemplate = @pathtemplate, showfull = @showfull where blog = @blog and name = @name",
sql.Named("title", section.Title),
sql.Named("description", section.Description),
sql.Named("pathtemplate", section.PathTemplate),
sql.Named("showfull", section.ShowFull),
sql.Named("blog", blog),
sql.Named("name", section.Name),
)
return err
}

View File

@ -9,6 +9,7 @@ connectviator: "Über Tor verbinden."
contactagreesend: "Akzeptieren & Senden" contactagreesend: "Akzeptieren & Senden"
contactsend: "Senden" contactsend: "Senden"
create: "Erstellen" create: "Erstellen"
default: "Standard"
delete: "Löschen" delete: "Löschen"
deleteall: "Alle löschen" deleteall: "Alle löschen"
deletedposts: "Gelöschte Posts" deletedposts: "Gelöschte Posts"

View File

@ -12,6 +12,7 @@ connectviator: "Connect via Tor."
contactagreesend: "Accept & Send" contactagreesend: "Accept & Send"
contactsend: "Send" contactsend: "Send"
create: "Create" create: "Create"
default: "Default"
delete: "Delete" delete: "Delete"
deleteall: "Delete all" deleteall: "Delete all"
deletedposts: "Deleted posts" deletedposts: "Deleted posts"

24
ui.go
View File

@ -1486,8 +1486,9 @@ func (a *goBlog) renderEditor(hb *htmlBuilder, rd *renderData) {
} }
type settingsRenderData struct { type settingsRenderData struct {
blog string blog string
sections []*configSection sections []*configSection
defaultSection string
} }
func (a *goBlog) renderSettings(hb *htmlBuilder, rd *renderData) { func (a *goBlog) renderSettings(hb *htmlBuilder, rd *renderData) {
@ -1513,6 +1514,25 @@ func (a *goBlog) renderSettings(hb *htmlBuilder, rd *renderData) {
hb.writeEscaped(a.ts.GetTemplateStringVariant(rd.Blog.Lang, "postsections")) hb.writeEscaped(a.ts.GetTemplateStringVariant(rd.Blog.Lang, "postsections"))
hb.writeElementClose("h2") hb.writeElementClose("h2")
// Update default section
hb.writeElementOpen("h3")
hb.writeEscaped(a.ts.GetTemplateStringVariant(rd.Blog.Lang, "default"))
hb.writeElementClose("h3")
hb.writeElementOpen("form", "class", "fw p", "method", "post")
hb.writeElementOpen("select", "name", "defaultsection")
for _, section := range srd.sections {
hb.writeElementOpen("option", "value", section.Name, lo.If(section.Name == srd.defaultSection, "selected").Else(""), "")
hb.writeEscaped(section.Name)
hb.writeElementClose("option")
}
hb.writeElementClose("select")
hb.writeElementOpen(
"input", "type", "submit", "value", a.ts.GetTemplateStringVariant(rd.Blog.Lang, "update"),
"formaction", rd.Blog.getRelativePath(settingsPath+settingsUpdateDefaultSectionPath),
)
hb.writeElementClose("form")
for _, section := range srd.sections { for _, section := range srd.sections {
hb.writeElementOpen("details") hb.writeElementOpen("details")