Refactor new books into moderate submissions

This commit is contained in:
Las Zenow 2019-06-06 11:19:34 +00:00
parent 897071e85d
commit 4efaaadbd2
8 changed files with 162 additions and 153 deletions

View file

@ -4,7 +4,6 @@ import (
log "github.com/cihub/seelog"
"net/http"
"strconv"
"strings"
"github.com/gorilla/mux"
@ -141,115 +140,6 @@ func saveHandler(h handler) {
http.Redirect(h.w, h.r, "/book/"+id, http.StatusFound)
} else {
// XXX: I can't use a referer here :(
http.Redirect(h.w, h.r, "/new/", http.StatusFound)
http.Redirect(h.w, h.r, "/submission/moderate/", http.StatusFound)
}
}
type newBook struct {
TitleFound int
AuthorFound int
Comment string
B database.Book
}
type newData struct {
S Status
Found int
Books []newBook
Page int
Next string
Prev string
Search string
}
func newHandler(h handler) {
if !h.sess.IsModerator() {
notFound(h)
return
}
err := h.r.ParseForm()
if err != nil {
http.Error(h.w, err.Error(), http.StatusInternalServerError)
return
}
req := strings.Join(h.r.Form["q"], " ")
page := 0
if len(h.r.Form["p"]) != 0 {
page, err = strconv.Atoi(h.r.Form["p"][0])
if err != nil {
page = 0
}
}
res, num, _ := h.db.GetNewBooks(req, newItemsPage, page*newItemsPage)
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)
} else {
data.Books = make([]newBook, newItemsPage)
}
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.Authors, " author:"), 1, 0)
data.Books[i].Comment, err = h.db.GetComment(b.ID)
if err != nil {
log.Error("Error getting comment for ", b.Title, " (", b.ID, "): ", err)
}
}
data.Page = page + 1
if num > (page+1)*newItemsPage {
data.Next = "/new/?q=" + req + "&p=" + strconv.Itoa(page+1)
}
if page > 0 {
data.Prev = "/new/?q=" + req + "&p=" + strconv.Itoa(page-1)
}
data.Search = req
h.load("new", data)
}
func storeHandler(h handler) {
if !h.sess.IsModerator() {
notFound(h)
return
}
var titles []string
ids := strings.Split(mux.Vars(h.r)["ids"], "/")
for _, id := range ids {
if id == "" {
continue
}
book, err := h.db.GetBookID(id)
if err != nil {
h.sess.Notify("Book not found!", "The book with id '"+id+"' is not there", "error")
continue
}
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)
if err != nil {
h.sess.Notify("An error ocurred!", err.Error(), "error")
log.Error("Error storing book '", book.Title, "': ", err.Error())
continue
}
err = h.db.UpdateSubmissionByBook(id, "Included in the library", &book)
if err != nil {
log.Error("There was a problem updating the submission: ", err)
}
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)
}

View file

@ -172,7 +172,7 @@ func readHandler(h handler) {
data.Book = book
if !book.Active {
data.Back = "/new/"
data.Back = "/submission/moderate/"
} else {
data.Back = "/book/" + id
}

156
lib/submission.go Normal file
View file

@ -0,0 +1,156 @@
package trantor
import (
"net/http"
"strconv"
"strings"
"github.com/gorilla/mux"
"github.com/prometheus/common/log"
"gitlab.com/trantor/trantor/lib/database"
)
func submissionHandler(h handler) {
var data submissionData
var err error
submissionID := mux.Vars(h.r)["submissionID"]
data.SubmissionID = submissionID
data.Submissions, err = h.db.GetSubmission(submissionID)
if err != nil {
log.Error("Can get submission ", submissionID, ": ", err)
}
data.S = GetStatus(h)
data.S.Title = "Submission -- " + data.S.Title
h.load("submission", data)
}
type submissionData struct {
S Status
SubmissionID string
Submissions []database.Submission
}
func submissionCommentHandler(h handler) {
submissionID := mux.Vars(h.r)["submissionID"]
bookID := mux.Vars(h.r)["id"]
comment := h.r.FormValue("comment")
err := h.db.UpdateSubmissionComment(submissionID, bookID, comment)
if err != nil {
log.Error("Adding comment (submission: ", submissionID, ", book: ", bookID, ") <", comment, ">: ", err)
h.sess.Notify("Error adding a comment!", "Can't add the comment rigt now. Try again later or report it to the site admins", "error")
} else {
h.sess.Notify("Comment added!", "", "success")
}
http.Redirect(h.w, h.r, "/submission/"+submissionID, http.StatusFound)
}
type submissionBook struct {
TitleFound int
AuthorFound int
Comment string
B database.Book
}
type mSubmissionData struct {
S Status
Found int
Books []submissionBook
Page int
Next string
Prev string
Search string
}
func moderateSubmissionsHandler(h handler) {
if !h.sess.IsModerator() {
notFound(h)
return
}
err := h.r.ParseForm()
if err != nil {
http.Error(h.w, err.Error(), http.StatusInternalServerError)
return
}
req := strings.Join(h.r.Form["q"], " ")
page := 0
if len(h.r.Form["p"]) != 0 {
page, err = strconv.Atoi(h.r.Form["p"][0])
if err != nil {
page = 0
}
}
res, num, _ := h.db.GetNewBooks(req, newItemsPage, page*newItemsPage)
var data mSubmissionData
data.S = GetStatus(h)
data.S.Title = "New books -- " + data.S.Title
data.Found = num
if num-newItemsPage*page < newItemsPage {
data.Books = make([]submissionBook, num-newItemsPage*page)
} else {
data.Books = make([]submissionBook, newItemsPage)
}
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.Authors, " author:"), 1, 0)
data.Books[i].Comment, err = h.db.GetComment(b.ID)
if err != nil {
log.Error("Error getting comment for ", b.Title, " (", b.ID, "): ", err)
}
}
data.Page = page + 1
if num > (page+1)*newItemsPage {
data.Next = "/submission/moderate/?q=" + req + "&p=" + strconv.Itoa(page+1)
}
if page > 0 {
data.Prev = "/submission/moderate/?q=" + req + "&p=" + strconv.Itoa(page-1)
}
data.Search = req
h.load("submission_moderate", data)
}
func storeHandler(h handler) {
if !h.sess.IsModerator() {
notFound(h)
return
}
var titles []string
ids := strings.Split(mux.Vars(h.r)["ids"], "/")
for _, id := range ids {
if id == "" {
continue
}
book, err := h.db.GetBookID(id)
if err != nil {
h.sess.Notify("Book not found!", "The book with id '"+id+"' is not there", "error")
continue
}
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)
if err != nil {
h.sess.Notify("An error ocurred!", err.Error(), "error")
log.Error("Error storing book '", book.Title, "': ", err.Error())
continue
}
err = h.db.UpdateSubmissionByBook(id, "Included in the library", &book)
if err != nil {
log.Error("There was a problem updating the submission: ", err)
}
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)
}

View file

@ -203,6 +203,7 @@ func InitRouter(db database.DB, sg *StatsGatherer, assetsPath string) http.Handl
r.HandleFunc("/submission/{submissionID:"+idPattern+"}/edit/{id:"+idPattern+"}", sg.Gather(editHandler))
r.HandleFunc("/submission/{submissionID:"+idPattern+"}/save/{id:"+idPattern+"}", sg.Gather(saveHandler)).Methods("POST")
r.HandleFunc("/submission/{submissionID:"+idPattern+"}/delete/{ids:(?:"+idPattern+"/)+}", sg.Gather(deleteHandler))
r.HandleFunc("/submission/moderate/", sg.Gather(moderateSubmissionsHandler))
r.HandleFunc("/read/{id:"+idPattern+"}", sg.Gather(readStartHandler))
r.HandleFunc("/read/{id:"+idPattern+"}/{file:.*}", sg.Gather(readHandler))
@ -225,7 +226,6 @@ func InitRouter(db database.DB, sg *StatsGatherer, assetsPath string) http.Handl
r.HandleFunc("/list/{listID:"+idPattern+"}", sg.Gather(listEditPostHandler)).Methods("POST")
r.HandleFunc("/list/{listID:"+idPattern+"}/remove/{bookID:"+idPattern+"}", sg.Gather(listRemoveHandler))
r.HandleFunc("/new/", sg.Gather(newHandler))
r.HandleFunc("/save/{id:"+idPattern+"}", sg.Gather(saveHandler)).Methods("POST")
r.HandleFunc("/edit/{id:"+idPattern+"}", sg.Gather(editHandler))
r.HandleFunc("/store/{ids:(?:"+idPattern+"/)+}", sg.Gather(storeHandler))

View file

@ -9,7 +9,6 @@ import (
"encoding/base64"
"mime/multipart"
"github.com/gorilla/mux"
"github.com/meskio/epubgo"
"gitlab.com/trantor/trantor/lib/database"
"gitlab.com/trantor/trantor/lib/parser"
@ -124,42 +123,6 @@ func uploadHandler(h handler) {
h.load("upload", data)
}
func submissionHandler(h handler) {
var data submissionData
var err error
submissionID := mux.Vars(h.r)["submissionID"]
data.SubmissionID = submissionID
data.Submissions, err = h.db.GetSubmission(submissionID)
if err != nil {
log.Error("Can get submission ", submissionID, ": ", err)
}
data.S = GetStatus(h)
data.S.Title = "Submission -- " + data.S.Title
h.load("submission", data)
}
type submissionData struct {
S Status
SubmissionID string
Submissions []database.Submission
}
func submissionCommentHandler(h handler) {
submissionID := mux.Vars(h.r)["submissionID"]
bookID := mux.Vars(h.r)["id"]
comment := h.r.FormValue("comment")
err := h.db.UpdateSubmissionComment(submissionID, bookID, comment)
if err != nil {
log.Error("Adding comment (submission: ", submissionID, ", book: ", bookID, ") <", comment, ">: ", err)
h.sess.Notify("Error adding a comment!", "Can't add the comment rigt now. Try again later or report it to the site admins", "error")
} else {
h.sess.Notify("Comment added!", "", "success")
}
http.Redirect(h.w, h.r, "/submission/"+submissionID, http.StatusFound)
}
func GenID() string {
b := make([]byte, 12)
rand.Read(b)

View file

@ -6,7 +6,7 @@
<div class="span8 offset2">
<ul class="nav nav-tabs nav-stacked">
{{if eq .S.Role "admin" "moderator"}}
<li><a href="/new/"><i class="icon-book"></i> New books</a></li>
<li><a href="/submission/moderate/"><i class="icon-book"></i> Moderate submissions</a></li>
{{if eq .S.Role "admin"}}
<li><a href="/news/edit"><i class="icon-certificate"></i> Edit news</a></li>
{{end}}

View file

@ -69,7 +69,7 @@
<ul class="dropdown-menu">
<li><a href="/dashboard/"><i class="icon-tasks"></i> Dashboard</a></li>
{{if eq .Role "admin" "moderator"}}
<li><a href="/new/"><i class="icon-book"></i> New books</a></li>
<li><a href="/submission/moderate/"><i class="icon-book"></i> Moderate submissions</a></li>
{{if eq .Role "admin"}}
<li><a href="/news/edit"><i class="icon-certificate"></i> Edit news</a></li>
<li><a href="/admin/users/"><i class="icon-user"></i> Users admin</a></li>

View file

@ -1,6 +1,6 @@
{{template "header.html" .S}}
<form class="centered" action="/new/">
<form class="centered" action="/submission/moderate/">
<input type="search" class="search-query span8" name="q" {{if .Search}}value="{{.Search}}"{{else}}placeholder="Search"{{end}} />
</form>