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/admin.go

247 lines
5.6 KiB
Go
Raw Normal View History

2012-08-18 02:06:43 +02:00
package main
import (
"labix.org/v2/mgo/bson"
"log"
2012-08-18 02:06:43 +02:00
"net/http"
2012-10-31 10:24:17 +01:00
"strconv"
2012-12-05 15:23:49 +01:00
"strings"
2012-08-18 02:06:43 +02:00
)
type settingsData struct {
2012-10-29 19:18:57 +01:00
S Status
}
func settingsHandler(w http.ResponseWriter, r *http.Request) {
sess := GetSession(r)
if sess.User == "" {
http.NotFound(w, r)
return
}
if r.Method == "POST" {
current_pass := r.FormValue("currpass")
pass1 := r.FormValue("password1")
pass2 := r.FormValue("password2")
switch {
case !db.UserValid(sess.User, current_pass):
sess.Notify("Password error!", "The current password given don't match with the user password. Try again", "error")
case pass1 != pass2:
sess.Notify("Passwords don't match!", "The new password and the confirmation password don't match. Try again", "error")
default:
db.SetPassword(sess.User, pass1)
sess.Notify("Password updated!", "Your new password is correctly set.", "success")
}
}
var data settingsData
data.S = GetStatus(w, r)
loadTemplate(w, "settings", data)
}
func deleteHandler(w http.ResponseWriter, r *http.Request) {
sess := GetSession(r)
if sess.User == "" {
http.NotFound(w, r)
return
}
2012-08-18 02:06:43 +02:00
2012-09-13 00:05:21 +02:00
var titles []string
var isNew bool
2012-09-14 00:34:13 +02:00
ids := strings.Split(r.URL.Path[len("/delete/"):], "/")
2012-09-13 00:05:21 +02:00
for _, idStr := range ids {
if idStr == "" {
continue
}
id := bson.ObjectIdHex(idStr)
books, _, err := db.GetBooks(bson.M{"_id": id})
if err != nil {
sess.Notify("Book not found!", "The book with id '"+idStr+"' is not there", "error")
continue
2012-09-13 00:05:21 +02:00
}
book := books[0]
2012-10-28 17:27:01 +01:00
DeleteBook(book)
2012-09-13 00:05:21 +02:00
db.RemoveBook(id)
2012-09-14 00:34:13 +02:00
if !book.Active {
2012-09-13 00:05:21 +02:00
isNew = true
}
titles = append(titles, book.Title)
}
if titles != nil {
sess.Notify("Removed books!", "The books "+strings.Join(titles, ", ")+" are completly removed", "success")
}
sess.Save(w, r)
2012-09-13 00:05:21 +02:00
if isNew {
http.Redirect(w, r, "/new/", http.StatusFound)
2012-09-13 00:05:21 +02:00
} else {
http.Redirect(w, r, "/", http.StatusFound)
2012-09-12 21:47:43 +02:00
}
2012-08-18 02:06:43 +02:00
}
2012-08-19 15:58:37 +02:00
func editHandler(w http.ResponseWriter, r *http.Request) {
sess := GetSession(r)
if sess.User == "" {
http.NotFound(w, r)
return
2012-08-19 15:58:37 +02:00
}
id := bson.ObjectIdHex(r.URL.Path[len("/edit/"):])
books, _, err := db.GetBooks(bson.M{"_id": id})
if err != nil {
http.NotFound(w, r)
return
}
var data bookData
data.Book = books[0]
data.S = GetStatus(w, r)
loadTemplate(w, "edit", data)
2012-08-19 15:58:37 +02:00
}
func cleanEmptyStr(s []string) []string {
var res []string
for _, v := range s {
if v != "" {
res = append(res, v)
}
}
return res
}
func saveHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.NotFound(w, r)
return
}
sess := GetSession(r)
if sess.User == "" {
http.NotFound(w, r)
return
}
2012-08-19 15:58:37 +02:00
idStr := r.URL.Path[len("/save/"):]
id := bson.ObjectIdHex(idStr)
title := r.FormValue("title")
publisher := r.FormValue("publisher")
date := r.FormValue("date")
description := r.FormValue("description")
author := cleanEmptyStr(r.Form["author"])
subject := cleanEmptyStr(r.Form["subject"])
lang := cleanEmptyStr(r.Form["lang"])
book := map[string]interface{}{"title": title,
"publisher": publisher,
"date": date,
"description": description,
"author": author,
"subject": subject,
"lang": lang}
book["keywords"] = keywords(book)
err := db.UpdateBook(id, book)
if err != nil {
http.NotFound(w, r)
return
2012-08-19 15:58:37 +02:00
}
sess.Notify("Book Modified!", "", "success")
sess.Save(w, r)
2012-09-12 21:47:43 +02:00
if db.BookActive(id) {
http.Redirect(w, r, "/book/"+idStr, http.StatusFound)
2012-09-12 21:47:43 +02:00
} else {
http.Redirect(w, r, "/new/", http.StatusFound)
2012-09-12 21:47:43 +02:00
}
2012-08-19 15:58:37 +02:00
}
type newBook struct {
2012-09-14 00:34:13 +02:00
TitleFound int
AuthorFound int
2012-09-14 00:34:13 +02:00
B Book
}
2012-08-20 14:25:18 +02:00
type newData struct {
S Status
Found int
Books []newBook
2012-10-31 10:24:17 +01:00
Page int
Next string
Prev string
2012-08-20 14:25:18 +02:00
}
func newHandler(w http.ResponseWriter, r *http.Request) {
sess := GetSession(r)
if sess.User == "" {
http.NotFound(w, r)
return
}
2012-08-25 16:41:54 +02:00
2012-10-31 10:37:21 +01:00
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2012-10-31 10:24:17 +01:00
page := 0
if len(r.Form["p"]) != 0 {
page, err = strconv.Atoi(r.Form["p"][0])
if err != nil {
page = 0
}
}
res, num, _ := db.GetNewBooks(NEW_ITEMS_PAGE, page*NEW_ITEMS_PAGE)
var data newData
data.S = GetStatus(w, r)
data.Found = num
2012-10-31 10:37:21 +01:00
if num-NEW_ITEMS_PAGE*page < NEW_ITEMS_PAGE {
data.Books = make([]newBook, num-NEW_ITEMS_PAGE*page)
} else {
data.Books = make([]newBook, NEW_ITEMS_PAGE)
}
for i, b := range res {
data.Books[i].B = b
2012-09-14 00:34:13 +02:00
_, data.Books[i].TitleFound, _ = db.GetBooks(buildQuery("title:"+b.Title), 1)
_, data.Books[i].AuthorFound, _ = db.GetBooks(buildQuery("author:"+strings.Join(b.Author, " author:")), 1)
2012-08-20 14:25:18 +02:00
}
2012-12-05 15:23:49 +01:00
data.Page = page + 1
2012-10-31 10:24:17 +01:00
if num > (page+1)*NEW_ITEMS_PAGE {
data.Next = "/new/?p=" + strconv.Itoa(page+1)
}
if page > 0 {
data.Prev = "/new/?p=" + strconv.Itoa(page-1)
}
loadTemplate(w, "new", data)
2012-08-20 14:25:18 +02:00
}
func storeHandler(w http.ResponseWriter, r *http.Request) {
sess := GetSession(r)
if sess.User == "" {
http.NotFound(w, r)
return
}
2012-08-20 14:25:18 +02:00
2012-09-13 00:05:21 +02:00
var titles []string
ids := strings.Split(r.URL.Path[len("/store/"):], "/")
for _, idStr := range ids {
if idStr == "" {
continue
}
2012-09-13 00:05:21 +02:00
id := bson.ObjectIdHex(idStr)
books, _, err := db.GetBooks(bson.M{"_id": id})
if err != nil {
sess.Notify("Book not found!", "The book with id '"+idStr+"' is not there", "error")
continue
2012-09-13 00:05:21 +02:00
}
book := books[0]
2012-10-28 17:22:23 +01:00
if err != nil {
sess.Notify("An error ocurred!", err.Error(), "error")
log.Println("Error storing book '", book.Title, "': ", err.Error())
continue
2012-10-28 17:22:23 +01:00
}
2013-04-12 01:05:40 +02:00
db.UpdateBook(id, bson.M{"active": true})
2012-09-13 00:05:21 +02:00
titles = append(titles, book.Title)
}
if titles != nil {
sess.Notify("Store books!", "The books '"+strings.Join(titles, ", ")+"' are stored for public download", "success")
}
sess.Save(w, r)
http.Redirect(w, r, "/new/", http.StatusFound)
2012-08-19 15:58:37 +02:00
}