This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/news.go

70 lines
1.2 KiB
Go
Raw Normal View History

package main
import (
2014-06-29 19:41:29 -05:00
"git.gitorious.org/trantor/trantor.git/database"
"net/http"
)
type newsData struct {
S Status
2013-07-18 11:42:46 +02:00
News []newsEntry
}
2013-07-18 11:42:46 +02:00
type newsEntry struct {
Date string
Text string
}
func newsHandler(h handler) {
err := h.r.ParseForm()
2013-08-31 02:45:30 +02:00
if err != nil {
http.Error(h.w, err.Error(), http.StatusInternalServerError)
2013-08-31 02:45:30 +02:00
return
}
var data newsData
data.S = GetStatus(h)
data.S.News = true
data.News = getNews(NUM_NEWS, 0, h.db)
2013-08-31 02:45:30 +02:00
format := h.r.Form["fmt"]
2013-08-31 02:45:30 +02:00
if (len(format) > 0) && (format[0] == "rss") {
loadTxtTemplate(h.w, "news_rss.xml", data)
2013-08-31 02:45:30 +02:00
} else {
loadTemplate(h.w, "news", data)
2013-08-31 02:45:30 +02:00
}
}
func editNewsHandler(h handler) {
if !h.sess.IsAdmin() {
notFound(h)
return
}
var data statusData
data.S = GetStatus(h)
data.S.News = true
loadTemplate(h.w, "edit_news", data)
}
func postNewsHandler(h handler) {
if !h.sess.IsAdmin() {
notFound(h)
return
}
text := h.r.FormValue("text")
h.db.AddNews(text)
http.Redirect(h.w, h.r, "/news/", http.StatusFound)
}
2013-07-18 11:42:46 +02:00
2014-06-29 19:41:29 -05:00
func getNews(num int, days int, db *database.DB) []newsEntry {
2013-07-18 11:42:46 +02:00
dbnews, _ := db.GetNews(num, days)
news := make([]newsEntry, len(dbnews))
for i, n := range dbnews {
news[i].Text = n.Text
2013-07-18 11:54:51 +02:00
news[i].Date = n.Date.Format("Jan 2, 2006")
2013-07-18 11:42:46 +02:00
}
return news
}