Simple news implementation

Is missing some proper design and to show up the news on the front page.
This commit is contained in:
Las Zenow 2013-07-17 01:08:44 +02:00
parent bf0480868d
commit 5f612a36ad
8 changed files with 101 additions and 1 deletions

View file

@ -14,6 +14,7 @@ const (
DAILY_VISITS_COLL = "visits.daily"
MONTHLY_VISITS_COLL = "visits.monthly"
USERS_COLL = "users"
NEWS_COLL = "news"
STATS_COLL = "statistics"
FS_BOOKS = "fs_books"
FS_IMGS = "fs_imgs"
@ -28,6 +29,7 @@ const (
TAGS_DISPLAY = 50
SEARCH_ITEMS_PAGE = 20
NEW_ITEMS_PAGE = 50
NUM_NEWS = 10
TEMPLATE_PATH = "templates/"
CSS_PATH = "css/"

View file

@ -34,10 +34,16 @@ type Book struct {
Keywords []string
}
type News struct {
Date time.Time
Text string
}
type DB struct {
session *mgo.Session
books *mgo.Collection
user *mgo.Collection
news *mgo.Collection
stats *mgo.Collection
mr *MR
}
@ -53,6 +59,7 @@ func initDB() *DB {
database := d.session.DB(DB_NAME)
d.books = database.C(BOOKS_COLL)
d.user = database.C(USERS_COLL)
d.news = database.C(NEWS_COLL)
d.stats = database.C(STATS_COLL)
d.mr = NewMR(database)
return d
@ -82,6 +89,18 @@ func (d *DB) UserValid(user string, pass string) bool {
return n != 0
}
func (d *DB) AddNews(text string) error {
var news News
news.Text = text
news.Date = time.Now()
return d.news.Insert(news)
}
func (d *DB) GetNews(num int) (news []News, err error) {
err = d.news.Find(bson.M{}).Sort("-date").Limit(num).All(&news)
return
}
func (d *DB) InsertStats(stats interface{}) error {
return d.stats.Insert(stats)
}

51
news.go Normal file
View file

@ -0,0 +1,51 @@
package main
import (
"net/http"
)
type newsData struct {
S Status
News []news
}
type news struct {
Date string
Text string
}
func newsHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
var data newsData
data.S = GetStatus(w, r)
data.S.News = true
newsEntries, _ := db.GetNews(NUM_NEWS)
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)
}
func editNewsHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
if sess.User == "" {
notFound(w, r)
return
}
var data statusData
data.S = GetStatus(w, r)
data.S.News = true
loadTemplate(w, "edit_news", data)
}
func postNewsHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
if sess.User == "" {
notFound(w, r)
return
}
text := r.FormValue("text")
db.AddNews(text)
http.Redirect(w, r, "/news/", http.StatusFound)
}

View file

@ -11,6 +11,7 @@ type Status struct {
Notif []Notification
Home bool
About bool
News bool
Upload bool
Stats bool
Help bool
@ -30,6 +31,8 @@ var templates = template.Must(template.ParseFiles(TEMPLATE_PATH+"header.html",
TEMPLATE_PATH+"404.html",
TEMPLATE_PATH+"index.html",
TEMPLATE_PATH+"about.html",
TEMPLATE_PATH+"news.html",
TEMPLATE_PATH+"edit_news.html",
TEMPLATE_PATH+"book.html",
TEMPLATE_PATH+"search.html",
TEMPLATE_PATH+"upload.html",

9
templates/edit_news.html Normal file
View file

@ -0,0 +1,9 @@
{{template "header.html" .S}}
<h4>Add News:</h4>
<form method="POST" action="/news/edit">
<textarea name="text" rows="3"></textarea>
<button type="submit" class="btn">Add</button>
</form>
{{template "footer.html"}}

View file

@ -52,7 +52,8 @@
<ul class="nav">
<li {{if .Home}}class="active"{{end}}><a href="/">Home</a></li>
<li {{if .About}}class="active"{{end}}><a href="/about/">About</a></li>
<li {{if .Upload}}class="active"{{end}}><a href="/upload/">Upload your epub</a></li>
<li {{if .News}}class="active"{{end}}><a href="/news/">News</a></li>
<li {{if .Upload}}class="active"{{end}}><a href="/upload/">Upload</a></li>
<li {{if .Stats}}class="active"{{end}}><a href="/stats/">Statistics</a></li>
</ul>
@ -65,6 +66,8 @@
</a>
<ul class="dropdown-menu">
<li><a href="/new/"><i class="icon-book"></i> New books</a></li>
<li><a href="/news/edit"><i class="icon-certificate"></i> Edit news</a></li>
<li class="divider"></li>
<li><a href="/settings/"><i class="icon-wrench"></i> Settings</a></li>
<li class="divider"></li>
<li><a href="/logout/"><i class="icon-off"></i> Log Out</a></li>

10
templates/news.html Normal file
View file

@ -0,0 +1,10 @@
{{template "header.html" .S}}
<dl class="dl-horizontal">
{{range .News}}
<dt>{{.Date}}</dt>
<dd>{{.Text}}</dd>
{{end}}
</dl>
{{template "footer.html"}}

View file

@ -181,6 +181,9 @@ func setUpRouter() {
r.HandleFunc("/cover/{id:[0-9a-fA-F]+}/{size}/{img:.*}", coverHandler)
r.HandleFunc("/settings/", GatherStats(settingsHandler))
r.HandleFunc("/stats/", GatherStats(statsHandler))
r.HandleFunc("/news/", GatherStats(newsHandler))
r.HandleFunc("/news/edit", GatherStats(editNewsHandler)).Methods("GET")
r.HandleFunc("/news/edit", GatherStats(postNewsHandler)).Methods("POST")
h := http.FileServer(http.Dir(IMG_PATH))
r.Handle("/img/{img}", http.StripPrefix("/img/", h))
h = http.FileServer(http.Dir(CSS_PATH))