Permalink
Browse files

adding csv format interface and handler impl

  • Loading branch information...
nilslice committed May 15, 2017
1 parent 0cf8aa5 commit 8f12938f28c0637a9261c7fcb4b0e311b8ba3b40
Showing with 188 additions and 4 deletions.
  1. +9 −0 management/format/csv.go
  2. +133 −0 system/admin/export.go
  3. +45 −4 system/admin/handlers.go
  4. +1 −0 system/admin/server.go
View
@@ -0,0 +1,9 @@
+// Package format provides interfaces to format content into various kinds of
+// data
+package format
+
+// CSVFormattable is implemented with the method FormatCSV, which must return the ordered
+// slice of JSON struct tag names for the type implmenting it
+type CSVFormattable interface {
+ FormatCSV() []string
+}
View
@@ -0,0 +1,133 @@
+package admin
+
+import (
+ "fmt"
+ "log"
+ "net/http"
+ "path/filepath"
+ "strings"
+ "time"
+
+ "encoding/csv"
+
+ "io/ioutil"
+ "os"
+
+ "github.com/ponzu-cms/ponzu/management/format"
+ "github.com/ponzu-cms/ponzu/system/db"
+ "github.com/ponzu-cms/ponzu/system/item"
+ "github.com/tidwall/gjson"
+)
+
+func exportHandler(res http.ResponseWriter, req *http.Request) {
+ // /admin/contents/export?type=Blogpost&format=csv
+ q := req.URL.Query()
+ t := q.Get("type")
+ f := strings.ToLower(q.Get("format"))
+
+ if t == "" || f == "" {
+ v, err := Error400()
+ if err != nil {
+ res.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+
+ res.WriteHeader(http.StatusBadRequest)
+ _, err = res.Write(v)
+ if err != nil {
+ res.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+
+ }
+
+ pt, ok := item.Types[t]
+ if !ok {
+ res.WriteHeader(http.StatusBadRequest)
+ return
+ }
+
+ switch f {
+ case "csv":
+ csv, ok := pt().(format.CSVFormattable)
+ if !ok {
+ res.WriteHeader(http.StatusBadRequest)
+ return
+ }
+
+ fields := csv.FormatCSV()
+ exportCSV(res, req, pt, fields)
+
+ default:
+ res.WriteHeader(http.StatusBadRequest)
+ return
+ }
+}
+
+func exportCSV(res http.ResponseWriter, req *http.Request, pt func() interface{}, fields []string) {
+ tmpFile, err := ioutil.TempFile(os.TempDir(), "exportcsv-")
+ if err != nil {
+ log.Println("Failed to create tmp file for CSV export:", err)
+ res.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ defer os.Remove(filepath.Join(os.TempDir(), tmpFile.Name()))
+ defer tmpFile.Close()
+
+ csvBuf := csv.NewWriter(tmpFile)
+
+ t := req.URL.Query().Get("type")
+
+ // get content data and loop through creating a csv row per result
+ bb := db.ContentAll(t)
+
+ err = csvBuf.Write(fields)
+ if err != nil {
+ res.WriteHeader(http.StatusInternalServerError)
+ log.Println("Failed to write column headers:", fields)
+ return
+ }
+
+ for row := range bb {
+ // unmarshal data and loop over fields
+ rowBuf := []string{}
+
+ for _, col := range fields {
+ // pull out each field as the column value
+ result := gjson.GetBytes(bb[row], col)
+
+ // append it to the buffer
+ rowBuf = append(rowBuf, result.String())
+ }
+
+ // write row to csv
+ err := csvBuf.Write(rowBuf)
+ if err != nil {
+ res.WriteHeader(http.StatusInternalServerError)
+ log.Println("Failed to write column headers:", fields)
+ return
+ }
+ }
+
+ // write the buffer to a content-disposition response
+ csvB, err := ioutil.ReadAll(tmpFile)
+ if err != nil {
+ log.Println("Failed to read tmp file for CSV export:", err)
+ res.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+
+ ts := time.Now().Unix()
+ disposition := `attachment; filename="export-%s-%d.csv"`
+
+ res.Header().Set("Content-Type", "text/csv")
+ res.Header().Set("Content-Disposition", fmt.Sprintf(disposition, t, ts))
+ res.Header().Set("Content-Length", fmt.Sprintf("%d", len(csvB)))
+
+ _, err = res.Write(csvB)
+ if err != nil {
+ log.Println("Failed to write tmp file to response for CSV export:", err)
+ res.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+}
View
@@ -13,6 +13,7 @@ import (
"time"
"github.com/ponzu-cms/ponzu/management/editor"
+ "github.com/ponzu-cms/ponzu/management/format"
"github.com/ponzu-cms/ponzu/management/manager"
"github.com/ponzu-cms/ponzu/system/addon"
"github.com/ponzu-cms/ponzu/system/admin/config"
@@ -1492,8 +1493,24 @@ func contentsHandler(res http.ResponseWriter, req *http.Request) {
</script>
`
- btn := `<div class="col s3"><a href="/admin/edit?type=` + t + `" class="btn new-post waves-effect waves-light">New ` + t + `</a></div></div>`
- html = html + b.String() + script + btn
+ btn := `<div class="col s3">
+ <a href="/admin/edit?type=` + t + `" class="btn new-post waves-effect waves-light">
+ New ` + t + `
+ </a>
+ </div>`
+ html = html + b.String() + btn
+
+ if _, ok := pt.(format.CSVFormattable); ok {
+ btn = `<div class="col s3">
+ <a href="/admin/edit/export?type=` + t + `&format=csv" class="green darken-4 btn export-post waves-effect waves-light">
+ <i class="material-icons left">system_update_alt</i>
+ .CSV
+ </a>
+ </div>`
+ html = html + b.String() + btn
+ }
+
+ html += `</div>` + script
adminView, err := Admin([]byte(html))
if err != nil {
@@ -2422,7 +2439,15 @@ func searchHandler(res http.ResponseWriter, req *http.Request) {
posts := db.ContentAll(t + specifier)
b := &bytes.Buffer{}
- p := item.Types[t]().(editor.Editable)
+ pt, ok := item.Types[t]
+ if !ok {
+ res.WriteHeader(http.StatusBadRequest)
+ return
+ }
+
+ post := pt()
+
+ p := post.(editor.Editable)
html := `<div class="col s9 card">
<div class="card-content">
@@ -2499,9 +2524,25 @@ func searchHandler(res http.ResponseWriter, req *http.Request) {
return
}
- btn := `<div class="col s3"><a href="/admin/edit?type=` + t + `" class="btn new-post waves-effect waves-light">New ` + t + `</a></div></div>`
+ btn := `<div class="col s3">
+ <a href="/admin/edit?type=` + t + `" class="btn new-post waves-effect waves-light">
+ New ` + t + `
+ </a>
+ </div>`
html = html + b.String() + btn
+ if _, ok := post.(format.CSVFormattable); ok {
+ btn = `<div class="col s3">
+ <a href="/admin/edit/export?type=` + t + `&format=csv" class="green darken-4 btn export-post waves-effect waves-light">
+ <i class="material-icons left">system_update_alt</i>
+ .CSV
+ </a>
+ </div>`
+ html = html + b.String() + btn
+ }
+
+ html += `</div>`
+
adminView, err := Admin([]byte(html))
if err != nil {
log.Println(err)
View
@@ -37,6 +37,7 @@ func Run() {
http.HandleFunc("/admin/contents", user.Auth(contentsHandler))
http.HandleFunc("/admin/contents/search", user.Auth(searchHandler))
+ http.HandleFunc("/admin/contents/export", user.Auth(exportHandler))
http.HandleFunc("/admin/edit", user.Auth(editHandler))
http.HandleFunc("/admin/edit/delete", user.Auth(deleteHandler))

0 comments on commit 8f12938

Please sign in to comment.