template tweek news; route handler: dont use session for page redirects, bad; further mock edit

This commit is contained in:
Dan Ballard 2015-09-17 22:49:54 -07:00
parent f496ca9f80
commit 233b24d561
3 changed files with 23 additions and 38 deletions

View File

@ -16,6 +16,7 @@ import (
"strings" "strings"
txtTemplate "text/template" txtTemplate "text/template"
"time" "time"
"errors"
) )
func GetFlashes(session *sessions.Session) map[string]interface{} { func GetFlashes(session *sessions.Session) map[string]interface{} {
@ -30,7 +31,7 @@ func sessionWipe(session *sessions.Session) {
} }
func initSessionUser(r *http.Request) (*user.User, *sessions.Session) { func initSessionUser(r *http.Request) (*user.User, *sessions.Session) {
# TODO verify ssl session // TODO verify ssl session
session, _ := store.Get(r, "c_user") session, _ := store.Get(r, "c_user")
if session.Values["username"] == nil { if session.Values["username"] == nil {
return nil, session return nil, session
@ -131,21 +132,8 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
categories.LoadCategories(db) categories.LoadCategories(db)
flashes := GetFlashes(session) flashes := GetFlashes(session)
popup := session.Values["popup"]
delete(session.Values, "popup")
title := session.Values["title"]
delete(session.Values, "title")
link := session.Values["link"]
delete(session.Values, "link")
description := session.Values["description"]
delete(session.Values, "description")
session.Save(r, w) session.Save(r, w)
if link != nil {
//TODO category_id
ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree, "link": link, "title": title, "description": description, "popup": popup})
return
}
var url = r.URL.Query().Get("url") var url = r.URL.Query().Get("url")
reHttp := regexp.MustCompile("^https?://") reHttp := regexp.MustCompile("^https?://")
@ -153,12 +141,12 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
url = "http://" + url url = "http://" + url
} }
title = r.URL.Query().Get("title") title := r.URL.Query().Get("title")
if title == "" && url != "" { if title == "" && url != "" {
title = getUrlTitle(url) title = getUrlTitle(url)
} }
popup = r.URL.Query().Get("popup") popup := r.URL.Query().Get("popup")
ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": title, "popup": popup}) ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": title, "popup": popup})
} }
@ -177,14 +165,11 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
news.Category_id = category_id news.Category_id = category_id
err = (&news).Insert(db) err = (&news).Insert(db)
err = errors.New("test")
if err != nil { if err != nil {
session.AddFlash("Error saving news: "+err.Error(), flash_err) var flashes = make(map[string]interface{})
session.Values["title"] = news.Title flashes["error"] = []string{ "Error saving news: "+err.Error() }
session.Values["link"] = news.Url ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": news.Url, "categories": categories.CategoriesTree, "title": news.Title, "popup": popup, "notes": news.Notes, "category_id": news.Category_id})
session.Values["notes"] = news.Notes
session.Values["popup"] = popup
session.Save(r, w)
http.Redirect(w, r, "/add", http.StatusFound)
} else { } else {
// TODO auto close? redirect // TODO auto close? redirect
session.AddFlash("Added news \""+news.Title+"\"", flash_info) session.AddFlash("Added news \""+news.Title+"\"", flash_info)
@ -206,20 +191,20 @@ func editFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, se
delete(session.Values, "title") delete(session.Values, "title")
link := session.Values["link"] link := session.Values["link"]
delete(session.Values, "link") delete(session.Values, "link")
description := session.Values["description"] notes := session.Values["notes"]
delete(session.Values, "description") delete(session.Values, "notes")
category := session.Values["category"] category := session.Values["category"]
delete(session.Values, "category") delete(session.Values, "category")
session.Save(r, w) session.Save(r, w)
if link != nil { if link != nil {
//TODO category_id //TODO category_id
ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree, "link": link, "title": title, "description": description, "popup": false, "category": category}) ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree, "link": link, "title": title, "notes": notes, "popup": false, "category": category})
return return
} }
} }
func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { func editPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
var news news.News var news news.News
news.Title = r.FormValue("title") news.Title = r.FormValue("title")
@ -429,7 +414,7 @@ func init_route_handlers() {
r := mux.NewRouter() r := mux.NewRouter()
# TODO: CSRF // TODO: CSRF
r.HandleFunc("/login", getPostHandler(LoginFormHandler, LoginPostHandler)) r.HandleFunc("/login", getPostHandler(LoginFormHandler, LoginPostHandler))
r.HandleFunc("/logout", userHandler(LogoutHandler)) r.HandleFunc("/logout", userHandler(LogoutHandler))

View File

@ -14,7 +14,7 @@
{{end}} {{end}}
</select> </select>
</div> </div>
<div class="col-xs-2">Notes:</div><div class="col-xs-10"><textarea class="form-control" name="notes" placeholder="Notes" rows="5" cols="80">{{.description}}</textarea></div> <div class="col-xs-2">Notes:</div><div class="col-xs-10"><textarea class="form-control" name="notes" placeholder="Notes" rows="5" cols="80">{{.notes}}</textarea></div>
<div class="col-xs-2"></div><div class="col-xs-10"><input class="add-submit btn btn-lg btn-primary btn-block" type="submit" value="Add Link" /></div> <div class="col-xs-2"></div><div class="col-xs-10"><input class="add-submit btn btn-lg btn-primary btn-block" type="submit" value="Add Link" /></div>
</div> </div>

View File

@ -13,27 +13,27 @@
{{define "row-news"}} {{define "row-news"}}
<div class="news-row"> <div class="news-row">
<div class="row"> <div class="row">
<div class="col-xs-2"> <div class="col-xs-3">
{{fullCategoryPath .categories .post.Category_id}} {{fullCategoryPath .categories .post.Category_id}}
</div> </div>
<div class="col-xs-8"> <div class="col-xs-6">
<strong>{{truncate .post.Title 100}}</strong> <strong>{{truncate .post.Title 100}}</strong>
</div> </div>
<div class="col-xs-2"> <div class="col-xs-3">
{{dateFormat .post.Date}} {{dateFormat .post.Date}}
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="col-xs-2">&nbsp;</div> <div class="col-xs-3">&nbsp;</div>
<div class="col-xs-8"> <div class="col-xs-6">
<a href="{{.post.Url}}">{{truncate .post.Url 100}}</a> <a href="{{.post.Url}}">{{truncate .post.Url 100}}</a>
</div> </div>
<div class="col-xs-2"><a href="/edit/{{.post.Id}}">Edit</a></div> <div class="col-xs-3"><a href="/edit?id={{.post.Id}}">Edit</a></div>
</div> </div>
<div class="row"> <div class="row">
<div class="col-xs-2">&nbsp;</div> <div class="col-xs-3">&nbsp;</div>
<div class="col-xs-8">{{truncate .post.Notes 500}}</div> <div class="col-xs-6">{{truncate .post.Notes 500}}</div>
<div class="col-xs-2"><a class="confirm-delete" href="/delete?id={{.post.Id}}">Delete</a></div> <div class="col-xs-3"><a class="confirm-delete" href="/delete?id={{.post.Id}}">Delete</a></div>
</div> </div>
</div> </div>
{{end}} {{end}}