From a8e3f705fb9419d1fc1e87336ad35198339bef65 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 10 May 2017 07:57:55 +0000 Subject: [PATCH] display reports properly --- model/torrent.go | 14 ++++++++------ router/router.go | 12 ++++++------ router/templateVariables.go | 4 ++++ router/torrentReportHandler.go | 11 +++++------ service/moderation/torrent_report.go | 2 +- templates/torrent_report.html | 2 +- 6 files changed, 25 insertions(+), 20 deletions(-) diff --git a/model/torrent.go b/model/torrent.go index d9ae4f0e..c2817fe1 100644 --- a/model/torrent.go +++ b/model/torrent.go @@ -47,9 +47,11 @@ type Torrent struct { // FIXME can't preload field Torrents for model.TorrentReport type TorrentReport struct { ID uint `gorm:"column:torrent_report_id;primary_key"` - Description string `gorm:"description"` - Torrent Torrent `gorm:"ForeignKey:Id"` - User User `gorm:"ForeignKey:Id"` + Description string `gorm:"column:type"` + TorrentID uint + UserID uint + Torrent Torrent `gorm:"ForeignKey:TorrentID;AssociationForeignKey:ID"` + User User `gorm:"ForeignKey:UserID;AssociationForeignKey:ID"` } /* We need a JSON object instead of a Gorm structure because magnet URLs are @@ -89,14 +91,14 @@ type TorrentJSON struct { type TorrentReportJson struct { ID uint `json:"id"` Description string `json:"description"` - Torrent Torrent `json:"torrent"` - User User `json:"user"` + Torrent TorrentJSON `json:"torrent"` + User string } /* Model Conversion to Json */ func (report *TorrentReport) ToJson() TorrentReportJson { - json := TorrentReportJson{report.ID, report.Description, report.Torrent, report.User} + json := TorrentReportJson{report.ID, report.Description, report.Torrent.ToJSON(), report.User.Username} return json } diff --git a/router/router.go b/router/router.go index 1e0dcceb..de6bfc67 100644 --- a/router/router.go +++ b/router/router.go @@ -49,9 +49,9 @@ func init() { gzipTorrentDeleteModPanel := handlers.CompressHandler(http.HandlerFunc(TorrentDeleteModPanel)) gzipGetTorrentReportHandler := handlers.CompressHandler(http.HandlerFunc(GetTorrentReportHandler)) - gzipTorrentReportCreateHandler := handlers.CompressHandler(http.HandlerFunc(CreateTorrentReportHandler)) - gzipTorrentReportDeleteHandler := handlers.CompressHandler(http.HandlerFunc(DeleteTorrentReportHandler)) - gzipTorrentDeleteHandler := handlers.CompressHandler(http.HandlerFunc(DeleteTorrentHandler)) + //gzipTorrentReportCreateHandler := handlers.CompressHandler(http.HandlerFunc(CreateTorrentReportHandler)) + //gzipTorrentReportDeleteHandler := handlers.CompressHandler(http.HandlerFunc(DeleteTorrentReportHandler)) + //gzipTorrentDeleteHandler := handlers.CompressHandler(http.HandlerFunc(DeleteTorrentHandler)) Router = mux.NewRouter() @@ -96,10 +96,10 @@ func init() { Router.PathPrefix("/captcha").Methods("GET").HandlerFunc(captcha.ServeFiles) - Router.Handle("/report/create", gzipTorrentReportCreateHandler).Name("torrent_report_create").Methods("POST") + //Router.Handle("/report/create", gzipTorrentReportCreateHandler).Name("torrent_report_create").Methods("POST") // TODO Allow only moderators to access /moderation/* - 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("/moderation/report/delete", gzipTorrentReportDeleteHandler).Name("torrent_report_delete").Methods("POST") + //Router.Handle("/moderation/torrent/delete", gzipTorrentDeleteHandler).Name("torrent_delete").Methods("POST") Router.Handle("/moderation/report", gzipGetTorrentReportHandler ).Name("torrent_report").Methods("GET") Router.NotFoundHandler = http.HandlerFunc(NotFoundHandler) diff --git a/router/templateVariables.go b/router/templateVariables.go index aade2f1d..32417d89 100644 --- a/router/templateVariables.go +++ b/router/templateVariables.go @@ -132,6 +132,10 @@ type PanelTorrentEdVbs struct { Torrent model.Torrent } +type ViewTorrentReportsVariables struct { + Torrents []model.TorrentReportJson +} + /* * Variables used by the upper ones */ diff --git a/router/torrentReportHandler.go b/router/torrentReportHandler.go index e4d80823..ff4cf3ef 100644 --- a/router/torrentReportHandler.go +++ b/router/torrentReportHandler.go @@ -5,10 +5,8 @@ import ( "github.com/ewhal/nyaa/model" "github.com/ewhal/nyaa/service/moderation" - "github.com/ewhal/nyaa/service/torrent" - "github.com/ewhal/nyaa/util/modelHelper" ) - +/* func SanitizeTorrentReport(torrentReport *model.TorrentReport) { // TODO unescape html ? return @@ -47,19 +45,20 @@ func DeleteTorrentReportHandler(w http.ResponseWriter, r *http.Request) { return } } - +*/ func GetTorrentReportHandler(w http.ResponseWriter, r *http.Request) { torrentReports, err := moderationService.GetTorrentReports() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - err = torrentReportTemplate.ExecuteTemplate(w, "torrent_report.html", model.TorrentReportsToJSON(torrentReports)) + err = torrentReportTemplate.ExecuteTemplate(w, "torrent_report.html", ViewTorrentReportsVariables{model.TorrentReportsToJSON(torrentReports)}) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } } +/* func DeleteTorrentHandler(w http.ResponseWriter, r *http.Request) { // TODO Figure out how to get torrent report id from form @@ -69,4 +68,4 @@ func DeleteTorrentHandler(w http.ResponseWriter, r *http.Request) { if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } -} +}*/ diff --git a/service/moderation/torrent_report.go b/service/moderation/torrent_report.go index f87358b8..ebce2555 100644 --- a/service/moderation/torrent_report.go +++ b/service/moderation/torrent_report.go @@ -31,7 +31,7 @@ func DeleteTorrentReport(id int) (int, error) { // TODO Use limit, offset func GetTorrentReports() ([]model.TorrentReport, error) { var torrentReports []model.TorrentReport - if db.ORM.Preload("User").Preload("Torrents").Find(&torrentReports).Error != nil { + if db.ORM.Preload("User").Preload("Torrent").Find(&torrentReports).Error != nil { return nil, errors.New("Problem finding all torrent reports.") } return torrentReports, nil diff --git a/templates/torrent_report.html b/templates/torrent_report.html index 6bf070c5..488e6cad 100755 --- a/templates/torrent_report.html +++ b/templates/torrent_report.html @@ -1,6 +1,6 @@ - {{ range .TorrentReport }} + {{ range .Torrents}} {{ .ID }} {{ .Description }} {{ .User }} {{end}}