Print out the news in the front page

This commit is contained in:
Las Zenow 2013-07-18 11:42:46 +02:00
parent 9a98cfd793
commit 54763b04df
5 changed files with 29 additions and 13 deletions

View file

@ -30,6 +30,7 @@ const (
SEARCH_ITEMS_PAGE = 20 SEARCH_ITEMS_PAGE = 20
NEW_ITEMS_PAGE = 50 NEW_ITEMS_PAGE = 50
NUM_NEWS = 10 NUM_NEWS = 10
DAYS_NEWS_INDEXPAGE = 15
TEMPLATE_PATH = "templates/" TEMPLATE_PATH = "templates/"
CSS_PATH = "css/" CSS_PATH = "css/"

View file

@ -96,8 +96,15 @@ func (d *DB) AddNews(text string) error {
return d.news.Insert(news) return d.news.Insert(news)
} }
func (d *DB) GetNews(num int) (news []News, err error) { func (d *DB) GetNews(num int, days int) (news []News, err error) {
err = d.news.Find(bson.M{}).Sort("-date").Limit(num).All(&news) query := bson.M{}
if days != 0 {
duration := time.Duration(-24*days) * time.Hour
date := time.Now().Add(duration)
query = bson.M{"date": bson.M{"$gt": date}}
}
q := d.news.Find(query).Sort("-date").Limit(num)
err = q.All(&news)
return return
} }

21
news.go
View file

@ -6,10 +6,10 @@ import (
type newsData struct { type newsData struct {
S Status S Status
News []news News []newsEntry
} }
type news struct { type newsEntry struct {
Date string Date string
Text string Text string
} }
@ -18,12 +18,7 @@ func newsHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
var data newsData var data newsData
data.S = GetStatus(w, r) data.S = GetStatus(w, r)
data.S.News = true data.S.News = true
newsEntries, _ := db.GetNews(NUM_NEWS) data.News = getNews(NUM_NEWS, 0)
data.News = make([]news, len(newsEntries))
for i, n := range newsEntries {
data.News[i].Text = n.Text
data.News[i].Date = n.Date.Format("Jan 31, 2006")
}
loadTemplate(w, "news", data) loadTemplate(w, "news", data)
} }
@ -49,3 +44,13 @@ func postNewsHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
db.AddNews(text) db.AddNews(text)
http.Redirect(w, r, "/news/", http.StatusFound) http.Redirect(w, r, "/news/", http.StatusFound)
} }
func getNews(num int, days int) []newsEntry {
dbnews, _ := db.GetNews(num, days)
news := make([]newsEntry, len(dbnews))
for i, n := range dbnews {
news[i].Text = n.Text
news[i].Date = n.Date.Format("Jan 31, 2006")
}
return news
}

View file

@ -1,9 +1,10 @@
{{template "header.html" .S}} {{template "header.html" .S}}
<div class="alert alert-info"> {{range .News}}
<button type="button" class="close" data-dismiss="alert">&times;</button> <div class="offset2 span8 alert alert-info">
<strong>News!</strong> The <a href="/about/">about</a> has being updated with pgp key and bitcoin address for donations. <a href="/news/"><strong>News!</strong></a> {{.Text}}
</div> </div>
{{end}}
<div class="row"> <div class="row">
<div class="span8"> <div class="span8">

View file

@ -121,6 +121,7 @@ type indexData struct {
DownloadedBooks []Book DownloadedBooks []Book
Count int Count int
Tags []string Tags []string
News []newsEntry
} }
func indexHandler(w http.ResponseWriter, r *http.Request, sess *Session) { func indexHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
@ -132,6 +133,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
data.Books, data.Count, _ = db.GetBooks(bson.M{"active": true}, 6) data.Books, data.Count, _ = db.GetBooks(bson.M{"active": true}, 6)
data.VisitedBooks, _ = db.GetVisitedBooks(6) data.VisitedBooks, _ = db.GetVisitedBooks(6)
data.DownloadedBooks, _ = db.GetDownloadedBooks(6) data.DownloadedBooks, _ = db.GetDownloadedBooks(6)
data.News = getNews(1, DAYS_NEWS_INDEXPAGE)
loadTemplate(w, "index", data) loadTemplate(w, "index", data)
} }