From 0bf8088457fbd5b30a2b16aeb5e5ee4dff7a43e7 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Wed, 10 May 2017 17:37:49 +0200 Subject: [PATCH] Mostly finished Someone need to do torrent edit page Can't do it :/ --- router/modpanel.go | 38 ++++++++++++++------- router/router.go | 1 + router/template.go | 7 +--- router/templateVariables.go | 17 ++++++++++ router/torrentReportHandler.go | 49 ++++++++++++++++++---------- service/moderation/torrent_report.go | 10 +++--- templates/admin/commentlist.html | 3 +- templates/admin/panelindex.html | 20 ++++++++++-- templates/admin/torrent_report.html | 9 +++++ templates/admin/torrentlist.html | 3 +- templates/admin/userlist.html | 3 +- templates/admin_index.html | 3 +- templates/torrent_report.html | 8 ----- 13 files changed, 118 insertions(+), 53 deletions(-) create mode 100644 templates/admin/torrent_report.html delete mode 100755 templates/torrent_report.html diff --git a/router/modpanel.go b/router/modpanel.go index a2bf5179..061351c4 100644 --- a/router/modpanel.go +++ b/router/modpanel.go @@ -10,8 +10,10 @@ import ( "fmt" "github.com/ewhal/nyaa/service/comment" + "github.com/ewhal/nyaa/service/moderation" "github.com/ewhal/nyaa/service/torrent" "github.com/ewhal/nyaa/service/torrent/form" + "github.com/ewhal/nyaa/util/search" "github.com/ewhal/nyaa/service/user" form "github.com/ewhal/nyaa/service/user/form" "github.com/ewhal/nyaa/service/user/permission" @@ -20,14 +22,21 @@ import ( "github.com/gorilla/mux" ) -var panelIndex, panelTorrentList, panelUserList, panelCommentList, panelTorrentEd *template.Template +var panelIndex, panelTorrentList, panelUserList, panelCommentList, panelTorrentEd, torrentReportTemplate *template.Template func init() { panelTorrentList = template.Must(template.New("torrentlist").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/torrentlist.html"))) + panelTorrentList = template.Must(panelTorrentList.ParseGlob(filepath.Join("templates", "_*.html"))) panelUserList = template.Must(template.New("userlist").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/userlist.html"))) + panelUserList = template.Must(panelUserList.ParseGlob(filepath.Join("templates", "_*.html"))) panelCommentList = template.Must(template.New("commentlist").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/commentlist.html"))) + panelCommentList = template.Must(panelCommentList.ParseGlob(filepath.Join("templates", "_*.html"))) panelIndex = template.Must(template.New("indexPanel").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/panelindex.html"))) - panelTorrentEd = template.Must(template.New("indexPanel").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/paneltorrentedit.html"))) + panelIndex = template.Must(panelIndex.ParseGlob(filepath.Join("templates", "_*.html"))) + panelTorrentEd = template.Must(template.New("torrent_ed").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/paneltorrentedit.html"))) + panelTorrentEd = template.Must(panelTorrentEd.ParseGlob(filepath.Join("templates", "_*.html"))) + torrentReportTemplate = template.Must(template.New("torrent_report").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/torrent_report.html"))) + torrentReportTemplate = template.Must(torrentReportTemplate.ParseGlob(filepath.Join("templates", "_*.html"))) } func IndexModPanel(w http.ResponseWriter, r *http.Request) { @@ -38,8 +47,9 @@ func IndexModPanel(w http.ResponseWriter, r *http.Request) { torrents, _, _ := torrentService.GetAllTorrents(offset, 0) users, _ := userService.RetrieveUsersForAdmin(offset, 0) comments, _ := commentService.GetAllComments(offset, 0, "", "") + torrentReports, _, _ := moderationService.GetTorrentReports(offset,0, "", "") languages.SetTranslationFromRequest(panelIndex, r, "en-us") - htv := PanelIndexVbs{torrents, users, comments, r.URL} + htv := PanelIndexVbs{torrents, torrentReports, users, comments, NewSearchForm(), currentUser, r.URL} _ = panelIndex.ExecuteTemplate(w, "admin_index.html", htv) } else { http.Error(w, "admins only", http.StatusForbidden) @@ -52,10 +62,16 @@ func TorrentsListPanel(w http.ResponseWriter, r *http.Request) { page, _ := strconv.Atoi(vars["page"]) offset := 100 - torrents, nbTorrents, _ := torrentService.GetAllTorrents(offset, page * offset) + searchParam, torrents, _, err := search.SearchByQuery(r, page) + searchForm := SearchForm{ + SearchParam: searchParam, + Category: searchParam.Category.String(), + HideAdvancedSearch: false, + } + languages.SetTranslationFromRequest(panelTorrentList, r, "en-us") - htv := PanelTorrentListVbs{torrents, Navigation{nbTorrents, offset, page, "mod_tlist_page"}, r.URL} - err := panelTorrentList.ExecuteTemplate(w, "admin_index.html", htv) + htv := PanelTorrentListVbs{torrents, searchForm, Navigation{int(searchParam.Max), offset, page, "mod_tlist_page"}, currentUser, r.URL} + err = panelTorrentList.ExecuteTemplate(w, "admin_index.html", htv) fmt.Println(err) } else { @@ -71,7 +87,7 @@ func UsersListPanel(w http.ResponseWriter, r *http.Request) { users, nbUsers := userService.RetrieveUsersForAdmin(offset, page*offset) languages.SetTranslationFromRequest(panelUserList, r, "en-us") - htv := PanelUserListVbs{users, Navigation{nbUsers, offset, page, "mod_ulist_page"}, r.URL} + htv := PanelUserListVbs{users, NewSearchForm(), Navigation{nbUsers, offset, page, "mod_ulist_page"}, currentUser, r.URL} err := panelUserList.ExecuteTemplate(w, "admin_index.html", htv) fmt.Println(err) } else { @@ -93,7 +109,7 @@ func CommentsListPanel(w http.ResponseWriter, r *http.Request) { } comments, nbComments := commentService.GetAllComments(offset, page * offset, conditions, values...) languages.SetTranslationFromRequest(panelCommentList, r, "en-us") - htv := PanelCommentListVbs{comments, Navigation{nbComments, offset, page, "mod_clist_page"}, r.URL} + htv := PanelCommentListVbs{comments, NewSearchForm(), Navigation{nbComments, offset, page, "mod_clist_page"}, currentUser, r.URL} err := panelCommentList.ExecuteTemplate(w, "admin_index.html", htv) fmt.Println(err) } else { @@ -107,7 +123,7 @@ func TorrentEditModPanel(w http.ResponseWriter, r *http.Request) { id := r.URL.Query().Get("id") torrent, _ := torrentService.GetTorrentById(id) languages.SetTranslationFromRequest(panelTorrentEd, r, "en-us") - htv := PanelTorrentEdVbs{torrent} + htv := PanelTorrentEdVbs{torrent, NewSearchForm(), currentUser} err := panelTorrentEd.ExecuteTemplate(w, "admin_index.html", htv) fmt.Println(err) } else { @@ -138,7 +154,7 @@ func TorrentPostEditModPanel(w http.ResponseWriter, r *http.Request) { } } languages.SetTranslationFromRequest(panelTorrentEd, r, "en-us") - htv := PanelTorrentEdVbs{torrent} + htv := PanelTorrentEdVbs{torrent, NewSearchForm(), currentUser} _ = panelTorrentEd.ExecuteTemplate(w, "admin_index.html", htv) } else { http.Error(w, "admins only", http.StatusForbidden) @@ -169,4 +185,4 @@ func TorrentDeleteModPanel(w http.ResponseWriter, r *http.Request) { } else { http.Error(w, "admins only", http.StatusForbidden) } -} +} \ No newline at end of file diff --git a/router/router.go b/router/router.go index 16ce6e3c..3f694bee 100644 --- a/router/router.go +++ b/router/router.go @@ -106,6 +106,7 @@ func init() { //Router.Handle("/moderation/report/delete", gzipTorrentReportDeleteHandler).Name("torrent_report_delete").Methods("POST") //Router.Handle("/moderation/torrent/delete", gzipTorrentDeleteHandler).Name("torrent_delete").Methods("POST") Router.Handle("/mod/reports", gzipGetTorrentReportHandler).Name("torrent_report").Methods("GET") + Router.Handle("/mod/reports/{page}", gzipGetTorrentReportHandler).Name("mod_trlist").Methods("GET") Router.NotFoundHandler = http.HandlerFunc(NotFoundHandler) } diff --git a/router/template.go b/router/template.go index 60940c48..e206ffac 100644 --- a/router/template.go +++ b/router/template.go @@ -7,7 +7,7 @@ import ( var TemplateDir = "templates" -var torrentReportTemplate, homeTemplate, searchTemplate, faqTemplate, uploadTemplate, viewTemplate, viewRegisterTemplate, viewLoginTemplate, viewRegisterSuccessTemplate, viewVerifySuccessTemplate, viewProfileTemplate, viewProfileEditTemplate, viewUserDeleteTemplate, notFoundTemplate *template.Template +var homeTemplate, searchTemplate, faqTemplate, uploadTemplate, viewTemplate, viewRegisterTemplate, viewLoginTemplate, viewRegisterSuccessTemplate, viewVerifySuccessTemplate, viewProfileTemplate, viewProfileEditTemplate, viewUserDeleteTemplate, notFoundTemplate *template.Template type templateLoader struct { templ **template.Template @@ -18,11 +18,6 @@ type templateLoader struct { // ReloadTemplates reloads templates on runtime func ReloadTemplates() { templs := []templateLoader{ - templateLoader{ - templ: &torrentReportTemplate, - name: "torrent_report", - file: "torrent_report.html", - }, templateLoader{ templ: &homeTemplate, name: "home", diff --git a/router/templateVariables.go b/router/templateVariables.go index 9f197be4..2b74303b 100644 --- a/router/templateVariables.go +++ b/router/templateVariables.go @@ -113,34 +113,51 @@ type UploadTemplateVariables struct { Route *mux.Route } +/* MODERATION Variables */ + type PanelIndexVbs struct { Torrents []model.Torrent + TorrentReports []model.TorrentReport Users []model.User Comments []model.Comment + Search SearchForm + User *model.User URL *url.URL // For parsing Url in templates } type PanelTorrentListVbs struct { Torrents []model.Torrent + Search SearchForm Navigation Navigation + User *model.User URL *url.URL // For parsing Url in templates } type PanelUserListVbs struct { Users []model.User + Search SearchForm Navigation Navigation + User *model.User URL *url.URL // For parsing Url in templates } type PanelCommentListVbs struct { Comments []model.Comment + Search SearchForm Navigation Navigation + User *model.User URL *url.URL // For parsing Url in templates } type PanelTorrentEdVbs struct { Torrent model.Torrent + Search SearchForm + User *model.User } type ViewTorrentReportsVariables struct { Torrents []model.TorrentReportJson + Search SearchForm + Navigation Navigation + User *model.User + URL *url.URL // For parsing Url in templates } /* diff --git a/router/torrentReportHandler.go b/router/torrentReportHandler.go index 74907c0d..18398c96 100644 --- a/router/torrentReportHandler.go +++ b/router/torrentReportHandler.go @@ -2,10 +2,12 @@ package router import ( "net/http" + "strconv" "github.com/ewhal/nyaa/model" "github.com/ewhal/nyaa/service/moderation" "github.com/ewhal/nyaa/service/user/permission" + "github.com/gorilla/mux" ) /* func SanitizeTorrentReport(torrentReport *model.TorrentReport) { @@ -47,24 +49,7 @@ func DeleteTorrentReportHandler(w http.ResponseWriter, r *http.Request) { } } */ -func GetTorrentReportHandler(w http.ResponseWriter, r *http.Request) { - currentUser := GetUser(r) - if userPermission.HasAdmin(currentUser) { - torrentReports, err := moderationService.GetTorrentReports() - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - err = torrentReportTemplate.ExecuteTemplate(w, "torrent_report.html", ViewTorrentReportsVariables{model.TorrentReportsToJSON(torrentReports)}) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - http.Error(w, "admins only", http.StatusForbidden) - } -} /* func DeleteTorrentHandler(w http.ResponseWriter, r *http.Request) { @@ -76,3 +61,33 @@ func DeleteTorrentHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) } }*/ + + +func GetTorrentReportHandler(w http.ResponseWriter, r *http.Request) { + currentUser := GetUser(r) + if userPermission.HasAdmin(currentUser) { + vars := mux.Vars(r) + page, _ := strconv.Atoi(vars["page"]) + offset := 100 + userid := r.URL.Query().Get("userid") + var conditions string + var values []interface{} + if (userid != "") { + conditions = "user_id = ?" + values = append(values, userid) + } + + torrentReports, nbReports, err := moderationService.GetTorrentReports(offset, page * offset, conditions, values...) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + err = torrentReportTemplate.ExecuteTemplate(w, "admin_index.html", ViewTorrentReportsVariables{model.TorrentReportsToJSON(torrentReports), NewSearchForm(), Navigation{nbReports, offset, page, "mod_trlist_page"}, currentUser, r.URL}) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } else { + http.Error(w, "admins only", http.StatusForbidden) + } +} \ No newline at end of file diff --git a/service/moderation/torrent_report.go b/service/moderation/torrent_report.go index ebce2555..9dfd6303 100644 --- a/service/moderation/torrent_report.go +++ b/service/moderation/torrent_report.go @@ -22,17 +22,19 @@ func DeleteTorrentReport(id int) (int, error) { return http.StatusNotFound, errors.New("Trying to delete a torrent report that does not exists.") } if db.ORM.Delete(&torrentReport).Error != nil { - return http.StatusInternalServerError, errors.New("User is not deleted.") + return http.StatusInternalServerError, errors.New("TorrentReport is not deleted.") } return http.StatusOK, nil } // TODO Add WhereParams to filter the torrent reports (ie: searching description) // TODO Use limit, offset -func GetTorrentReports() ([]model.TorrentReport, error) { +func GetTorrentReports(limit int, offset int, conditions string, values ...interface{}) ([]model.TorrentReport, int, error) { var torrentReports []model.TorrentReport + var nbReports int + db.ORM.Model(&torrentReports).Where(conditions, values...).Count(&nbReports) if db.ORM.Preload("User").Preload("Torrent").Find(&torrentReports).Error != nil { - return nil, errors.New("Problem finding all torrent reports.") + return torrentReports, nbReports, errors.New("Problem finding all torrent reports.") } - return torrentReports, nil + return torrentReports, nbReports, nil } diff --git a/templates/admin/commentlist.html b/templates/admin/commentlist.html index 9f5e1e2e..39cbca0a 100644 --- a/templates/admin/commentlist.html +++ b/templates/admin/commentlist.html @@ -1,9 +1,10 @@ +{{define "title"}}Comments List{{end}} {{define "content"}} {{ range .Comments}} - + {{end}}
{{ .Content }}{{ .User.Username }}Delete
{{ T "delete" }}