include nice alert message on success
different template action for partial or complete import success
This commit is contained in:
parent
ee4fe2f4ad
commit
0ca198c715
|
@ -8,6 +8,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/go-multierror"
|
"github.com/hashicorp/go-multierror"
|
||||||
"github.com/writeas/impart"
|
"github.com/writeas/impart"
|
||||||
|
@ -28,6 +29,8 @@ func viewImport(app *App, u *User, w http.ResponseWriter, r *http.Request) error
|
||||||
*UserPage
|
*UserPage
|
||||||
Collections *[]Collection
|
Collections *[]Collection
|
||||||
Flashes []template.HTML
|
Flashes []template.HTML
|
||||||
|
Message string
|
||||||
|
InfoMsg bool
|
||||||
}{
|
}{
|
||||||
UserPage: p,
|
UserPage: p,
|
||||||
Collections: c,
|
Collections: c,
|
||||||
|
@ -36,8 +39,15 @@ func viewImport(app *App, u *User, w http.ResponseWriter, r *http.Request) error
|
||||||
|
|
||||||
flashes, _ := getSessionFlashes(app, w, r, nil)
|
flashes, _ := getSessionFlashes(app, w, r, nil)
|
||||||
for _, flash := range flashes {
|
for _, flash := range flashes {
|
||||||
|
if strings.HasPrefix(flash, "SUCCESS: ") {
|
||||||
|
d.Message = strings.TrimPrefix(flash, "SUCCESS: ")
|
||||||
|
} else if strings.HasPrefix(flash, "INFO: ") {
|
||||||
|
d.Message = strings.TrimPrefix(flash, "INFO: ")
|
||||||
|
d.InfoMsg = true
|
||||||
|
} else {
|
||||||
d.Flashes = append(d.Flashes, template.HTML(flash))
|
d.Flashes = append(d.Flashes, template.HTML(flash))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
showUserPage(w, "import", d)
|
showUserPage(w, "import", d)
|
||||||
return nil
|
return nil
|
||||||
|
@ -48,8 +58,9 @@ func handleImport(app *App, u *User, w http.ResponseWriter, r *http.Request) err
|
||||||
r.ParseMultipartForm(10 << 20)
|
r.ParseMultipartForm(10 << 20)
|
||||||
files := r.MultipartForm.File["files"]
|
files := r.MultipartForm.File["files"]
|
||||||
var fileErrs []error
|
var fileErrs []error
|
||||||
|
filesSubmitted := len(files)
|
||||||
|
var filesImported int
|
||||||
for _, formFile := range files {
|
for _, formFile := range files {
|
||||||
// TODO: count uploaded files that succeed and report back with message
|
|
||||||
file, err := formFile.Open()
|
file, err := formFile.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fileErrs = append(fileErrs, fmt.Errorf("failed to open form file: %s", formFile.Filename))
|
fileErrs = append(fileErrs, fmt.Errorf("failed to open form file: %s", formFile.Filename))
|
||||||
|
@ -125,10 +136,20 @@ func handleImport(app *App, u *User, w http.ResponseWriter, r *http.Request) err
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
filesImported++
|
||||||
}
|
}
|
||||||
if len(fileErrs) != 0 {
|
if len(fileErrs) != 0 {
|
||||||
_ = addSessionFlash(app, w, r, multierror.ListFormatFunc(fileErrs), nil)
|
_ = addSessionFlash(app, w, r, multierror.ListFormatFunc(fileErrs), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if filesImported == filesSubmitted {
|
||||||
|
verb := "posts"
|
||||||
|
if filesSubmitted == 1 {
|
||||||
|
verb = "post"
|
||||||
|
}
|
||||||
|
_ = addSessionFlash(app, w, r, fmt.Sprintf("SUCCESS: Import complete, %d %s imported.", filesImported, verb), nil)
|
||||||
|
} else if filesImported > 0 {
|
||||||
|
_ = addSessionFlash(app, w, r, fmt.Sprintf("INFO: %d of %d posts imported, see details below.", filesImported, filesSubmitted), nil)
|
||||||
|
}
|
||||||
return impart.HTTPError{http.StatusFound, "/me/import"}
|
return impart.HTTPError{http.StatusFound, "/me/import"}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,11 @@
|
||||||
{{template "header" .}}
|
{{template "header" .}}
|
||||||
|
|
||||||
<div class="snug content-container">
|
<div class="snug content-container">
|
||||||
|
{{if .Message}}
|
||||||
|
<div class="alert {{if .InfoMsg}}info{{else}}success{{end}}">
|
||||||
|
<p>{{.Message}}</p>
|
||||||
|
</div>
|
||||||
|
{{end}}
|
||||||
<h2 id="posts-header">Import</h2>
|
<h2 id="posts-header">Import</h2>
|
||||||
<p>Upload text or markdown files to import as posts.</p>
|
<p>Upload text or markdown files to import as posts.</p>
|
||||||
<div class="formContainer">
|
<div class="formContainer">
|
||||||
|
|
Loading…
Reference in New Issue