From 5f612a36adca598eb76c77e869aa5cd9f3b7d804 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Wed, 17 Jul 2013 01:08:44 +0200 Subject: [PATCH] Simple news implementation Is missing some proper design and to show up the news on the front page. --- config.go | 2 ++ database.go | 19 +++++++++++++++ news.go | 51 ++++++++++++++++++++++++++++++++++++++++ template.go | 3 +++ templates/edit_news.html | 9 +++++++ templates/header.html | 5 +++- templates/news.html | 10 ++++++++ trantor.go | 3 +++ 8 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 news.go create mode 100644 templates/edit_news.html create mode 100644 templates/news.html diff --git a/config.go b/config.go index b8f5f71..21f0612 100644 --- a/config.go +++ b/config.go @@ -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/" diff --git a/database.go b/database.go index ebf6fb0..9f96fd2 100644 --- a/database.go +++ b/database.go @@ -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) } diff --git a/news.go b/news.go new file mode 100644 index 0000000..640c46d --- /dev/null +++ b/news.go @@ -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) +} diff --git a/template.go b/template.go index 6d5da0e..4d1ecaa 100644 --- a/template.go +++ b/template.go @@ -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", diff --git a/templates/edit_news.html b/templates/edit_news.html new file mode 100644 index 0000000..d07c763 --- /dev/null +++ b/templates/edit_news.html @@ -0,0 +1,9 @@ +{{template "header.html" .S}} + +

Add News:

+
+ + +
+ +{{template "footer.html"}} diff --git a/templates/header.html b/templates/header.html index d46e17e..25d97c3 100644 --- a/templates/header.html +++ b/templates/header.html @@ -52,7 +52,8 @@ @@ -65,6 +66,8 @@