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

146 lines
3.5 KiB
Go
Raw Permalink 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-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) {
ids := strings.Split(mux.Vars(h.r)["ids"], "/")
submissionID := mux.Vars(h.r)["submissionID"]
if !h.sess.IsModerator() && !h.booksInSubmission(ids, submissionID) {
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
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")
continue
2012-09-13 00:05:21 +02: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)
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)
if submissionID != "" {
http.Redirect(h.w, h.r, "/submission/"+submissionID, http.StatusFound)
} else 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
type editData struct {
S Status
Book database.Book
SubmissionID string
}
func editHandler(h handler) {
2014-07-02 20:40:24 -05:00
id := mux.Vars(h.r)["id"]
submissionID := mux.Vars(h.r)["submissionID"]
if !h.sess.IsModerator() && !h.booksInSubmission([]string{id}, submissionID) {
notFound(h)
return
2012-08-19 15:58:37 +02:00
}
2016-07-30 07:59:30 -04:00
book, err := h.db.GetBookID(id)
if err != nil {
notFound(h)
return
}
var data editData
2014-07-02 20:40:24 -05:00
data.Book = book
data.S = GetStatus(h)
data.SubmissionID = submissionID
author := ""
if len(book.Authors) > 0 {
author = " by " + book.Authors[0]
}
data.S.Title = book.Title + author + " -- Edit -- " + data.S.Title
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
}
func saveHandler(h handler) {
// XXX: check for errors (ISBN, length(lang), ...)
id := mux.Vars(h.r)["id"]
submissionID := mux.Vars(h.r)["submissionID"]
if !h.sess.IsModerator() && !h.booksInSubmission([]string{id}, submissionID) {
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")
authors := cleanEmptyStr(h.r.Form["author"])
tags := cleanEmptyStr(strings.Split(h.r.FormValue("tags"), ","))
isbn := parser.ISBN(h.r.FormValue("isbn"))
lang := h.r.FormValue("lang")
book := map[string]interface{}{"title": title,
"publisher": publisher,
"date": date,
"description": description,
"authors": authors,
"tags": tags,
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)
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
}
h.sess.Save(h.w, h.r)
if submissionID != "" {
http.Redirect(h.w, h.r, "/submission/"+submissionID, http.StatusFound)
} else 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, "/submission/moderate/", http.StatusFound)
2012-09-12 21:47:43 +02:00
}
2012-08-19 15:58:37 +02:00
}