Work on news list page

TODO:
  - fix category path printer (only prints last if there is a parent)
  - dates are all NOW
This commit is contained in:
Dan Ballard 2015-08-23 11:04:14 -07:00
parent 72a17784ff
commit 6438250ef7
4 changed files with 54 additions and 5 deletions

View File

@ -9,3 +9,11 @@ body {
.col-form .row .col-xs-2 {
text-align: right;
}
.news-posts h3 {
display: inline;
}
.news-row {
margin-bottom: 1em;
}

View File

@ -337,7 +337,7 @@ func newsFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, se
session.AddFlash("Error loading news", flash_err)
}
ShowTemplate("news", w, map[string]interface{}{"user": user, "flashes": flashes, "news": news, "count": count})
ShowTemplate("news", w, map[string]interface{}{"user": user, "flashes": flashes, "news": news, "count": count, "categories": categories.CategoriesFlat})
}
@ -358,7 +358,7 @@ func init_route_handlers() {
r.HandleFunc("/logout", userHandler(LogoutHandler))
r.HandleFunc("/add", getPostHandler(userHandler(addFormHandler), userHandler(addPostHandler)))
r.HandleFunc("/", userHandler(templateFormHandler))
r.HandleFunc("/", userHandler(newsFormHandler))
r.HandleFunc("/news", userHandler(newsFormHandler))
r.HandleFunc("/export", userHandler(templateFormHandler))
r.HandleFunc("/export-commit", userHandler(exportHandler))

View File

@ -7,6 +7,9 @@ import (
"regexp"
"net/http"
"errors"
"time"
"strings"
"github.com/dballard/transmet/categories"
)
var (
@ -29,6 +32,8 @@ func dict (values ...interface{}) (map[string]interface{}, error) {
return dict, nil
}
// string multiplication
// stringTimes(3, "Foo") => "FooFooFoo"
func stringTimes(times int, str string) string {
result := ""
for i := 0; i < times; i ++ {
@ -37,12 +42,31 @@ func stringTimes(times int, str string) string {
return result
}
// Turns a Time into a formated string
func dateFormat(t time.Time) string {
return t.Format(time.ANSIC)
}
// takes a category_id and returns "Root / Parent / Category"
func fullCategoryPath(categoriesFlat map[int]*categories.Category, category_id int) string {
fmt.Println("fullCategoryPath: ", category_id)
var categoryNames []string = nil
var category *categories.Category = categoriesFlat[category_id]
for ; category.Parent.Valid; category = categoriesFlat[int(category.Parent.Int64)] {
fmt.Println("fullCategoryPath LOOP: ", category.Name)
categoryNames = append(categoryNames, category.Name)
}
return strings.Join(categoryNames, " / ")
}
// Tempalte helper functions
var funcMap = template.FuncMap {
"add": func (x, y int) int { return x + y },
"minus": func (x, y int) int { return x - y },
"dict": dict,
"stringTimes": stringTimes,
"dateFormat": dateFormat,
"fullCategoryPath": fullCategoryPath,
}

View File

@ -1,14 +1,31 @@
{{define "body"}}
<h2 class="news-heading">News</h2>
{{template "flashes" .}}
<div class="news-posts">
{{range $news_post := .news}}
{{template "row-news" $news_post}}
{{template "row-news" dict "post" $news_post "categories" $.categories}}
{{end}}
</div>
{{end}}
<!-- print a news row -->
{{define "row-news"}}
<h2> -- {{.Title}}</h2>
<div class="news-row">
<div class="row">
<div class="col-xs-12">
<h3>{{.post.Title}}</h3>
</div>
<div class="col-xs-12">
{{dateFormat .post.Date}}
</div>
<div class="col-xs-12">
<a href="{{.post.Url}}">{{.post.Url}}</a>
</div>
<div class="col-xs-12">
Category: {{fullCategoryPath .categories .post.Category_id}}
</div>
</div>
<div>{{.post.Notes}}</div>
</div>
{{end}}