From 8d126fbe7a186822168dca5882b800bac23436c7 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Sat, 7 Apr 2018 22:58:59 +0000 Subject: [PATCH] Add the avility to edit metadata and delete books after submission --- lib/admin.go | 28 ++++++++++++++++++------- lib/stats.go | 24 ++++++++++++++++++++++ lib/trantor.go | 7 ++++++- lib/upload.go | 24 ++++++++-------------- templates/edit.html | 8 +++++++- templates/submission.html | 43 +++++++++++++++++++++++---------------- templates/upload.html | 7 ------- 7 files changed, 93 insertions(+), 48 deletions(-) diff --git a/lib/admin.go b/lib/admin.go index c996d3f..1319d8d 100644 --- a/lib/admin.go +++ b/lib/admin.go @@ -17,14 +17,15 @@ const ( ) func deleteHandler(h handler) { - if !h.sess.IsModerator() { + 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 } var titles []string var isNew bool - ids := strings.Split(mux.Vars(h.r)["ids"], "/") for _, id := range ids { if id == "" { continue @@ -50,16 +51,25 @@ func deleteHandler(h handler) { h.sess.Notify("Removed books!", "The books "+strings.Join(titles, ", ")+" are completly removed", "success") } h.sess.Save(h.w, h.r) - if isNew { + 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) } else { http.Redirect(h.w, h.r, "/", http.StatusFound) } } +type editData struct { + S Status + Book database.Book + SubmissionID string +} + func editHandler(h handler) { id := mux.Vars(h.r)["id"] - if !h.sess.IsModerator() { + submissionID := mux.Vars(h.r)["submissionID"] + if !h.sess.IsModerator() && !h.booksInSubmission([]string{id}, submissionID) { notFound(h) return } @@ -69,9 +79,10 @@ func editHandler(h handler) { return } - var data bookData + var data editData data.Book = book data.S = GetStatus(h) + data.SubmissionID = submissionID author := "" if len(book.Authors) > 0 { author = " by " + book.Authors[0] @@ -93,7 +104,8 @@ func cleanEmptyStr(s []string) []string { func saveHandler(h handler) { // XXX: check for errors (ISBN, length(lang), ...) id := mux.Vars(h.r)["id"] - if !h.sess.IsModerator() { + submissionID := mux.Vars(h.r)["submissionID"] + if !h.sess.IsModerator() && !h.booksInSubmission([]string{id}, submissionID) { notFound(h) return } @@ -123,7 +135,9 @@ func saveHandler(h handler) { } h.sess.Save(h.w, h.r) - if h.db.IsBookActive(id) { + 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) } else { // XXX: I can't use a referer here :( diff --git a/lib/stats.go b/lib/stats.go index 4b6d194..8927704 100644 --- a/lib/stats.go +++ b/lib/stats.go @@ -50,6 +50,30 @@ func (h handler) load(tmpl string, data interface{}) { } } +func (h handler) booksInSubmission(bookIDs []string, submissionID string) bool { + submissions, err := h.db.GetSubmission(submissionID) + if err != nil { + return false + } + for _, bookID := range bookIDs { + if bookID == "" { + continue + } + + found := false + for _, s := range submissions { + if s.BookID == bookID && !s.Book.Active { + found = true + break + } + } + if !found { + return false + } + } + return true +} + type StatsGatherer struct { db database.DB store storage.Store diff --git a/lib/trantor.go b/lib/trantor.go index 84bb6f1..480a672 100644 --- a/lib/trantor.go +++ b/lib/trantor.go @@ -167,9 +167,14 @@ func InitRouter(db database.DB, sg *StatsGatherer, assetsPath string) http.Handl r.HandleFunc("/book/{id:"+idPattern+"}", sg.Gather(bookHandler)) r.HandleFunc("/search/", sg.Gather(searchHandler)) + r.HandleFunc("/upload/", sg.Gather(uploadHandler)).Methods("GET") r.HandleFunc("/upload/", sg.Gather(uploadPostHandler)).Methods("POST") - r.HandleFunc("/submission/{id:"+idPattern+"}", sg.Gather(submissionHandler)) + r.HandleFunc("/submission/{submissionID:"+idPattern+"}", sg.Gather(submissionHandler)) + 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("/read/{id:"+idPattern+"}", sg.Gather(readStartHandler)) r.HandleFunc("/read/{id:"+idPattern+"}/{file:.*}", sg.Gather(readHandler)) r.HandleFunc("/content/{id:"+idPattern+"}/{file:.*}", sg.Gather(contentHandler)) diff --git a/lib/upload.go b/lib/upload.go index db6e550..cb940de 100644 --- a/lib/upload.go +++ b/lib/upload.go @@ -1,6 +1,8 @@ package trantor import ( + "net/http" + log "github.com/cihub/seelog" "crypto/rand" @@ -110,32 +112,23 @@ func uploadPostHandler(h handler) { } uploadChannel <- uploadRequest{file, f, id} } - _uploadHandler(h, submissionID) + http.Redirect(h.w, h.r, "/submission/"+submissionID, http.StatusFound) } func uploadHandler(h handler) { - _uploadHandler(h, "") -} - -func _uploadHandler(h handler, submissionID string) { - var data uploadData - data.SubmissionID = submissionID + var data struct{ S Status } data.S = GetStatus(h) data.S.Title = "Upload -- " + data.S.Title data.S.Upload = true h.load("upload", data) } -type uploadData struct { - S Status - SubmissionID string -} - func submissionHandler(h handler) { var data submissionData var err error - submissionID := mux.Vars(h.r)["id"] + 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) @@ -146,8 +139,9 @@ func submissionHandler(h handler) { } type submissionData struct { - S Status - Submissions []database.Submission + S Status + SubmissionID string + Submissions []database.Submission } func genID() string { diff --git a/templates/edit.html b/templates/edit.html index 8ec3d03..90c934a 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -1,5 +1,6 @@ {{template "header.html" .S}} +{{$submissionID := .SubmissionID}} {{with .Book}}
{{if .Cover}} @@ -9,7 +10,12 @@ {{end}}
-
+ +{{else}} + action="/save/{{.ID}}"> +{{end}}
diff --git a/templates/submission.html b/templates/submission.html index 13a7ea3..f358632 100644 --- a/templates/submission.html +++ b/templates/submission.html @@ -1,10 +1,16 @@ {{template "header.html" .S}}
-

The status of your submission:

+

Review your submission:

+
    +
  • Check if the book is already in the library, please remove the books that are already in the library in a better or similar edition/quality. You can click on the title or the authors to search for similar books in the library.
  • +
  • Edit if needed the title, authors, publisher, ...
  • +
+

Thank you for your submission.

{{$role := .S.Role}} +{{$submissionID := .SubmissionID}} {{range .Submissions}}
@@ -19,19 +25,8 @@ {{end}} {{.Status}}

- {{with .Book}} - {{if and .ID (not .Active)}} -
- {{if eq $role "admin" "moderator"}} - - {{end}} -
- {{end}}
+ {{with .Book}} {{if .ID}}
@@ -42,13 +37,27 @@

[{{if .Lang}}{{.Lang}}{{end}}] - {{.Title}} - {{if .Publisher}}{{.Publisher}}{{end}}
- {{range .Authors}}{{.}}, {{end}} + {{.Title}}
+ {{if .Authors}}Authors: {{range .Authors}}{{.}}, {{end}}
{{end}} + {{if .Publisher}}Publisher: {{.Publisher}}
{{end}} + {{if .Tags}}Tags: {{range .Tags}}{{.}}, {{end}}
{{end}} + {{if .Isbn}}ISBN: {{.Isbn}}
{{end}} + {{if .Date}}Date: {{.Date}}
{{end}} + {{.Description}}

-
+ {{if and .ID (not .Active)}} +
+ {{if eq $role "admin" "moderator"}} + Save + {{end}} + Edit + Delete +
+

+ {{end}} + diff --git a/templates/upload.html b/templates/upload.html index ffc885d..e93991c 100644 --- a/templates/upload.html +++ b/templates/upload.html @@ -1,12 +1,5 @@ {{template "header.html" .S}} -{{if .SubmissionID}} -
- - Upload successful! Thank you for your contribution (Review the status of your upload) -
-{{end}} -

Upload your epubs to help the library.