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}} -