From 233b24d561840bbece1ddabca01c9ea95e92a6a1 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Thu, 17 Sep 2015 22:49:54 -0700 Subject: [PATCH] template tweek news; route handler: dont use session for page redirects, bad; further mock edit --- route_handlers.go | 41 +++++++++++++-------------------------- templates/pages/add.html | 2 +- templates/pages/news.html | 18 ++++++++--------- 3 files changed, 23 insertions(+), 38 deletions(-) diff --git a/route_handlers.go b/route_handlers.go index f038a49..fa7f848 100644 --- a/route_handlers.go +++ b/route_handlers.go @@ -16,6 +16,7 @@ import ( "strings" txtTemplate "text/template" "time" + "errors" ) 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) { - # TODO verify ssl session + // TODO verify ssl session session, _ := store.Get(r, "c_user") if session.Values["username"] == nil { return nil, session @@ -131,21 +132,8 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses categories.LoadCategories(db) 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) - 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") reHttp := regexp.MustCompile("^https?://") @@ -153,12 +141,12 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses url = "http://" + url } - title = r.URL.Query().Get("title") + title := r.URL.Query().Get("title") if title == "" && 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}) } @@ -177,14 +165,11 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses news.Category_id = category_id err = (&news).Insert(db) + err = errors.New("test") if err != nil { - session.AddFlash("Error saving news: "+err.Error(), flash_err) - session.Values["title"] = news.Title - session.Values["link"] = news.Url - session.Values["notes"] = news.Notes - session.Values["popup"] = popup - session.Save(r, w) - http.Redirect(w, r, "/add", http.StatusFound) + var flashes = make(map[string]interface{}) + flashes["error"] = []string{ "Error saving news: "+err.Error() } + 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}) } else { // TODO auto close? redirect 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") link := session.Values["link"] delete(session.Values, "link") - description := session.Values["description"] - delete(session.Values, "description") + notes := session.Values["notes"] + delete(session.Values, "notes") category := session.Values["category"] delete(session.Values, "category") 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": 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 } } -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 news.Title = r.FormValue("title") @@ -429,7 +414,7 @@ func init_route_handlers() { r := mux.NewRouter() - # TODO: CSRF + // TODO: CSRF r.HandleFunc("/login", getPostHandler(LoginFormHandler, LoginPostHandler)) r.HandleFunc("/logout", userHandler(LogoutHandler)) diff --git a/templates/pages/add.html b/templates/pages/add.html index 8e8c4c9..0290c4b 100644 --- a/templates/pages/add.html +++ b/templates/pages/add.html @@ -14,7 +14,7 @@ {{end}} -
Notes:
+
Notes:
diff --git a/templates/pages/news.html b/templates/pages/news.html index 4e96e8e..db67378 100644 --- a/templates/pages/news.html +++ b/templates/pages/news.html @@ -13,27 +13,27 @@ {{define "row-news"}}
-
+
{{fullCategoryPath .categories .post.Category_id}}
-
+
{{truncate .post.Title 100}}
-
+
{{dateFormat .post.Date}}
-
 
-
-
 
-
{{truncate .post.Notes 500}}
- +
 
+
{{truncate .post.Notes 500}}
+
{{end}}