github.com/pbberlin/tools@v0.0.0-20160910141205-7aa5421c2169/appengine/guestbook/views.go (about)

     1  // Package contains view to dsu.ancestored_gb_entries.
     2  package guestbook
     3  
     4  import (
     5  	"net/http"
     6  
     7  	gbp "github.com/pbberlin/tools/dsu/ancestored_gb_entries"
     8  	sc "github.com/pbberlin/tools/dsu/distributed_unancestored"
     9  	"github.com/pbberlin/tools/net/http/tplx"
    10  	"google.golang.org/appengine" // guest book persistence
    11  )
    12  
    13  const c_view_gbe = `
    14  	{{range .}}
    15  		{{$a := .Date}}
    16  		{{$b := .Date  | df }}
    17  		{{$c := df .Date}}
    18  			<p>
    19  		{{with .Author}}
    20  			<b>{{.}}</b> wrote on {{$c}}<br>
    21  		{{else}}
    22  			An anonymous person wrote:   <br>
    23  		{{end}}
    24  			<span style='display:block; 
    25  			 white-space: pre;
    26  			 max-width:300px;font-size:12px;' >{{.Content}}</span>
    27  		</p>
    28  	{{end}}
    29  `
    30  
    31  const c_new_gbe = `
    32  	Try number {{.}}
    33  	<form action="/guest-save" method="post">
    34  		<div><textarea name="content" rows="3" cols="60"></textarea></div>
    35  		<div><input type="submit" value="Save Entry"></div>
    36  	</form>
    37  `
    38  
    39  func guestEntry(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
    40  
    41  	c := appengine.NewContext(r)
    42  	path := m["dir"].(string) + m["base"].(string)
    43  
    44  	cntr, _ := sc.Count(c, path)
    45  
    46  	tplAdder, tplExec := tplx.FuncTplBuilder(w, r)
    47  	tplAdder("n_html_title", "New guest book entry", nil)
    48  	tplAdder("n_cont_0", c_new_gbe, cntr)
    49  	tplExec(w, r)
    50  
    51  }
    52  
    53  func guestSave(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
    54  
    55  	contnt := r.FormValue("content")
    56  	gbp.SaveEntry(w, r, map[string]interface{}{"content": contnt})
    57  	http.Redirect(w, r, "/guest-view", http.StatusFound)
    58  
    59  }
    60  
    61  func guestView(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
    62  
    63  	c := appengine.NewContext(r)
    64  	path := m["dir"].(string) + m["base"].(string)
    65  
    66  	cntr, _ := sc.Count(c, path)
    67  
    68  	gbEntries, report := gbp.ListEntries(w, r)
    69  
    70  	tplAdder, tplExec := tplx.FuncTplBuilder(w, r)
    71  	tplAdder("n_html_title", "List of guest book entries", nil)
    72  	tplAdder("n_cont_0", c_view_gbe, gbEntries)
    73  	tplAdder("n_cont_1", "<pre>{{.}}</pre>", report)
    74  	tplAdder("n_cont_2", "Visitors: {{.}}", cntr)
    75  	tplExec(w, r)
    76  
    77  }