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"
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))

View File

@ -14,7 +14,7 @@
{{end}}
</select>
</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>

View File

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