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

226 lines
5 KiB
Go
Raw Normal View History

2016-05-02 21:36:49 -04:00
package trantor
2012-08-18 02:06:43 +02:00
import (
2014-08-30 13:17:50 -05:00
log "github.com/cihub/seelog"
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"
"github.com/gorilla/mux"
2016-05-02 21:36:49 -04:00
"gitlab.com/trantor/trantor/lib/database"
2017-01-31 10:45:28 +00:00
"gitlab.com/trantor/trantor/lib/parser"
2012-08-18 02:06:43 +02:00
)
const (
newItemsPage = 10
)
func deleteHandler(h handler) {
2016-10-16 20:05:45 -04:00
if !h.sess.IsModerator() {
notFound(h)
return
}
2012-08-18 02:06:43 +02:00
2012-09-13 00:05:21 +02:00
var titles []string
var isNew bool
ids := strings.Split(mux.Vars(h.r)["ids"], "/")
2014-07-02 20:40:24 -05:00
for _, id := range ids {
2014-08-31 17:19:48 -05:00
if id == "" {
continue
}
2014-07-02 20:40:24 -05:00
book, err := h.db.GetBookId(id)
2012-09-13 00:05:21 +02:00
if err != nil {
2014-07-02 20:40:24 -05:00
h.sess.Notify("Book not found!", "The book with id '"+id+"' is not there", "error")
continue
2012-09-13 00:05:21 +02:00
}
2014-08-21 19:24:23 -05:00
h.store.Delete(id)
h.db.DeleteBook(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)
}
if titles != nil {
h.sess.Notify("Removed books!", "The books "+strings.Join(titles, ", ")+" are completly removed", "success")
}
h.sess.Save(h.w, h.r)
2012-09-13 00:05:21 +02:00
if isNew {
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
2012-09-13 00:05:21 +02:00
} else {
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
func editHandler(h handler) {
2014-07-02 20:40:24 -05:00
id := mux.Vars(h.r)["id"]
2016-10-16 20:05:45 -04:00
if !h.sess.IsModerator() {
notFound(h)
return
2012-08-19 15:58:37 +02:00
}
2014-07-02 20:40:24 -05:00
book, err := h.db.GetBookId(id)
if err != nil {
notFound(h)
return
}
var data bookData
2014-07-02 20:40:24 -05:00
data.Book = book
data.S = GetStatus(h)
author := ""
if len(book.Author) > 0 {
author = " by " + book.Author[0]
}
data.S.Title = book.Title + author + " -- Edit -- " + data.S.Title
h.template.load(h, "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(h handler) {
id := mux.Vars(h.r)["id"]
2016-10-16 20:05:45 -04:00
if !h.sess.IsModerator() {
notFound(h)
return
}
2012-08-19 15:58:37 +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(strings.Split(h.r.FormValue("subject"), ","))
2017-01-31 10:45:28 +00:00
isbn := parser.ISBN(h.r.FormValue("isbn")) // XXX: check for errors
lang := cleanEmptyStr(h.r.Form["lang"])
book := map[string]interface{}{"title": title,
"publisher": publisher,
"date": date,
"description": description,
"author": author,
"subject": subject,
2017-01-31 10:45:28 +00:00
"isbn": isbn,
"lang": lang}
err := h.db.UpdateBook(id, book)
if err != nil {
2015-01-22 21:58:27 -06:00
log.Error("Updating book: ", err)
notFound(h)
return
2012-08-19 15:58:37 +02:00
}
h.sess.Notify("Book Modified!", "", "success")
h.sess.Save(h.w, h.r)
2014-08-21 19:24:23 -05:00
if h.db.IsBookActive(id) {
http.Redirect(h.w, h.r, "/book/"+id, http.StatusFound)
2012-09-12 21:47:43 +02:00
} else {
// XXX: I can't use a referer here :(
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
}
type newBook struct {
2012-09-14 00:34:13 +02:00
TitleFound int
AuthorFound int
2014-06-29 19:41:29 -05:00
B database.Book
}
2012-08-20 14:25:18 +02:00
type newData struct {
S Status
Found int
Books []newBook
Page int
Next string
Prev string
Search string
2012-08-20 14:25:18 +02:00
}
func newHandler(h handler) {
2016-10-16 20:05:45 -04:00
if !h.sess.IsModerator() {
notFound(h)
return
}
2012-08-25 16:41:54 +02:00
err := h.r.ParseForm()
2012-10-31 10:37:21 +01:00
if err != nil {
http.Error(h.w, err.Error(), http.StatusInternalServerError)
2012-10-31 10:37:21 +01:00
return
}
req := strings.Join(h.r.Form["q"], " ")
2012-10-31 10:24:17 +01:00
page := 0
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
}
}
res, num, _ := h.db.GetNewBooks(req, newItemsPage, page*newItemsPage)
2012-10-31 10:24:17 +01:00
var data newData
data.S = GetStatus(h)
data.S.Title = "New books -- " + data.S.Title
data.Found = num
if num-newItemsPage*page < newItemsPage {
data.Books = make([]newBook, num-newItemsPage*page)
2012-10-31 10:37:21 +01:00
} else {
data.Books = make([]newBook, newItemsPage)
2012-10-31 10:37:21 +01:00
}
for i, b := range res {
data.Books[i].B = b
_, data.Books[i].TitleFound, _ = h.db.GetBooks("title:"+b.Title, 1, 0)
_, data.Books[i].AuthorFound, _ = h.db.GetBooks("author:"+strings.Join(b.Author, " author:"), 1, 0)
2012-08-20 14:25:18 +02:00
}
2012-12-05 15:23:49 +01:00
data.Page = page + 1
if num > (page+1)*newItemsPage {
2017-02-05 00:59:29 +00:00
data.Next = "/new/?q=" + req + "&p=" + strconv.Itoa(page+1)
2012-10-31 10:24:17 +01:00
}
if page > 0 {
2017-02-05 00:59:29 +00:00
data.Prev = "/new/?q=" + req + "&p=" + strconv.Itoa(page-1)
2012-10-31 10:24:17 +01:00
}
data.Search = req
h.template.load(h, "new", data)
2012-08-20 14:25:18 +02:00
}
func storeHandler(h handler) {
2016-10-16 20:05:45 -04:00
if !h.sess.IsModerator() {
notFound(h)
return
}
2012-08-20 14:25:18 +02:00
2012-09-13 00:05:21 +02:00
var titles []string
ids := strings.Split(mux.Vars(h.r)["ids"], "/")
2014-07-02 20:40:24 -05:00
for _, id := range ids {
2014-08-21 19:24:23 -05:00
if id == "" {
continue
}
2014-07-02 20:40:24 -05:00
book, err := h.db.GetBookId(id)
2012-09-13 00:05:21 +02:00
if err != nil {
2014-07-02 20:40:24 -05:00
h.sess.Notify("Book not found!", "The book with id '"+id+"' is not there", "error")
continue
2012-09-13 00:05:21 +02:00
}
2014-08-21 19:24:23 -05:00
if err != nil {
h.sess.Notify("An error ocurred!", err.Error(), "error")
log.Error("Error getting book for storing '", book.Title, "': ", err.Error())
continue
}
err = h.db.ActiveBook(id)
2012-10-28 17:22:23 +01:00
if err != nil {
h.sess.Notify("An error ocurred!", err.Error(), "error")
log.Error("Error storing book '", book.Title, "': ", err.Error())
continue
2012-10-28 17:22:23 +01:00
}
2012-09-13 00:05:21 +02:00
titles = append(titles, book.Title)
}
if titles != nil {
h.sess.Notify("Store books!", "The books '"+strings.Join(titles, ", ")+"' are stored for public download", "success")
}
h.sess.Save(h.w, h.r)
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
2012-08-19 15:58:37 +02:00
}