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-12-05 15:23:49 +01:00
|
|
|
"strings"
|
2014-08-30 01:25:16 -05:00
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func deleteHandler(h handler) {
|
2018-04-07 22:58:59 +00:00
|
|
|
ids := strings.Split(mux.Vars(h.r)["ids"], "/")
|
|
|
|
submissionID := mux.Vars(h.r)["submissionID"]
|
|
|
|
if !h.sess.IsModerator() && !h.booksInSubmission(ids, submissionID) {
|
2013-09-23 16:27:31 +02:00
|
|
|
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
|
2014-07-02 20:40:24 -05:00
|
|
|
for _, id := range ids {
|
2014-08-31 17:19:48 -05:00
|
|
|
if id == "" {
|
|
|
|
continue
|
|
|
|
}
|
2016-07-30 07:59:30 -04: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")
|
2012-10-29 19:17:38 +01:00
|
|
|
continue
|
2012-09-13 00:05:21 +02:00
|
|
|
}
|
2017-09-21 12:09:04 +00:00
|
|
|
err = h.db.UpdateSubmissionByBook(id, "Rejected", nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("There was a problem updating the submission: ", err)
|
|
|
|
}
|
2014-08-21 19:24:23 -05:00
|
|
|
h.store.Delete(id)
|
2014-07-02 21:09:41 -05:00
|
|
|
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)
|
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)
|
2018-04-07 22:58:59 +00:00
|
|
|
if submissionID != "" {
|
|
|
|
http.Redirect(h.w, h.r, "/submission/"+submissionID, http.StatusFound)
|
|
|
|
} else if isNew {
|
2017-02-05 01:51:00 +00:00
|
|
|
http.Redirect(h.w, h.r, h.r.Referer(), 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
|
|
|
|
2018-04-07 22:58:59 +00:00
|
|
|
type editData struct {
|
|
|
|
S Status
|
|
|
|
Book database.Book
|
|
|
|
SubmissionID string
|
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func editHandler(h handler) {
|
2014-07-02 20:40:24 -05:00
|
|
|
id := mux.Vars(h.r)["id"]
|
2018-04-07 22:58:59 +00:00
|
|
|
submissionID := mux.Vars(h.r)["submissionID"]
|
|
|
|
if !h.sess.IsModerator() && !h.booksInSubmission([]string{id}, submissionID) {
|
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
|
|
|
}
|
2016-07-30 07:59:30 -04:00
|
|
|
book, err := h.db.GetBookID(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
|
|
|
|
}
|
|
|
|
|
2018-04-07 22:58:59 +00:00
|
|
|
var data editData
|
2014-07-02 20:40:24 -05:00
|
|
|
data.Book = book
|
2013-09-23 16:27:31 +02:00
|
|
|
data.S = GetStatus(h)
|
2018-04-07 22:58:59 +00:00
|
|
|
data.SubmissionID = submissionID
|
2017-02-05 14:14:22 +00:00
|
|
|
author := ""
|
2016-07-30 07:36:58 -04:00
|
|
|
if len(book.Authors) > 0 {
|
|
|
|
author = " by " + book.Authors[0]
|
2017-02-05 14:14:22 +00:00
|
|
|
}
|
|
|
|
data.S.Title = book.Title + author + " -- Edit -- " + data.S.Title
|
2017-06-06 14:18:47 +00:00
|
|
|
h.load("edit", data)
|
2012-08-19 15:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func cleanEmptyStr(s []string) []string {
|
|
|
|
var res []string
|
|
|
|
for _, v := range s {
|
|
|
|
if v != "" {
|
2020-04-11 16:54:03 +00:00
|
|
|
res = append(res, strings.TrimSpace(v))
|
2012-08-19 15:58:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func saveHandler(h handler) {
|
2017-05-30 23:28:42 +00:00
|
|
|
// XXX: check for errors (ISBN, length(lang), ...)
|
2014-07-02 21:09:41 -05:00
|
|
|
id := mux.Vars(h.r)["id"]
|
2018-04-07 22:58:59 +00:00
|
|
|
submissionID := mux.Vars(h.r)["submissionID"]
|
|
|
|
if !h.sess.IsModerator() && !h.booksInSubmission([]string{id}, submissionID) {
|
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
|
|
|
|
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")
|
2017-05-30 23:28:42 +00:00
|
|
|
authors := cleanEmptyStr(h.r.Form["author"])
|
2016-07-30 07:36:58 -04:00
|
|
|
tags := cleanEmptyStr(strings.Split(h.r.FormValue("tags"), ","))
|
2017-05-30 23:28:42 +00:00
|
|
|
isbn := parser.ISBN(h.r.FormValue("isbn"))
|
|
|
|
lang := h.r.FormValue("lang")
|
2012-09-12 00:19:19 +02:00
|
|
|
book := map[string]interface{}{"title": title,
|
|
|
|
"publisher": publisher,
|
|
|
|
"date": date,
|
|
|
|
"description": description,
|
2017-05-30 23:28:42 +00:00
|
|
|
"authors": authors,
|
2016-07-30 07:36:58 -04:00
|
|
|
"tags": tags,
|
2017-01-31 10:45:28 +00:00
|
|
|
"isbn": isbn,
|
2012-09-12 00:19:19 +02:00
|
|
|
"lang": lang}
|
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 {
|
2015-01-22 21:58:27 -06:00
|
|
|
log.Error("Updating book: ", err)
|
2017-05-30 23:28:42 +00:00
|
|
|
h.sess.Notify("Can't modify book!", err.Error(), "error")
|
|
|
|
} else {
|
|
|
|
h.sess.Notify("Book Modified!", "", "success")
|
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.Save(h.w, h.r)
|
2018-04-07 22:58:59 +00:00
|
|
|
if submissionID != "" {
|
|
|
|
http.Redirect(h.w, h.r, "/submission/"+submissionID, http.StatusFound)
|
|
|
|
} else if h.db.IsBookActive(id) {
|
2014-07-02 21:09:41 -05:00
|
|
|
http.Redirect(h.w, h.r, "/book/"+id, http.StatusFound)
|
2012-09-12 21:47:43 +02:00
|
|
|
} else {
|
2017-02-05 01:51:00 +00:00
|
|
|
// XXX: I can't use a referer here :(
|
2019-06-06 11:19:34 +00:00
|
|
|
http.Redirect(h.w, h.r, "/submission/moderate/", http.StatusFound)
|
2012-09-12 21:47:43 +02:00
|
|
|
}
|
2012-08-19 15:58:37 +02:00
|
|
|
}
|