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