From 51d7fe2ef7a58945cf8d79c96cf72d1db96ceb64 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Fri, 28 Jul 2017 23:02:37 +0200 Subject: [PATCH] Added html views + fix template test + View & JSON Controller --- config/default_config.yml | 5 +++ config/structs.go | 24 ++++++++--- controllers/torrent/router.go | 2 + controllers/torrent/tag.go | 57 +++++++++++++++++++++++++++ templates/site/torrents/edit.jet.html | 6 +-- templates/site/torrents/tag.jet.html | 29 ++++++++++++++ templates/template_test.go | 12 ++++-- translations/en-us.all.json | 24 +++++++++++ 8 files changed, 147 insertions(+), 12 deletions(-) create mode 100644 templates/site/torrents/tag.jet.html diff --git a/config/default_config.yml b/config/default_config.yml index 919e222b..7c833de1 100644 --- a/config/default_config.yml +++ b/config/default_config.yml @@ -129,6 +129,11 @@ torrents: tags: # Torrent Tag Max weight for automatic system approval max_weight: 100.00 +# Array of tag types allowed for now + types: + - anidbid + - vndbid + - quality users: default_notifications_settings: {"new_torrent": true, "new_torrent_email": false, "new_comment": true, "new_comment_email": false, "new_responses": false, "new_responses_email": false, "new_follower": false, "new_follower_email": false, "followed": false, "followed_email": false} navigation: diff --git a/config/structs.go b/config/structs.go index 1ca64932..c21c76dd 100644 --- a/config/structs.go +++ b/config/structs.go @@ -47,7 +47,8 @@ type Config struct { } type Tags struct { - MaxWeight float64 `yaml:"max_weight,omitempty"` + MaxWeight float64 `yaml:"max_weight,omitempty"` + Types ArrayString `yaml:"types,omitempty"` } // WebAddressConfig : Config struct for web addresses @@ -102,8 +103,8 @@ type ScraperConfig struct { // TrackersConfig ; Config struct for Trackers type TrackersConfig struct { - Default []string `yaml:"default,flow,omitempty"` - NeededTrackers []int `yaml:"needed,flow,omitempty"` + Default ArrayString `yaml:"default,flow,omitempty"` + NeededTrackers []int `yaml:"needed,flow,omitempty"` } // TorrentsConfig : Config struct for Torrents @@ -111,9 +112,9 @@ type TorrentsConfig struct { Status []bool `yaml:"status,omitempty,omitempty"` SukebeiCategories map[string]string `yaml:"sukebei_categories,omitempty"` CleanCategories map[string]string `yaml:"clean_categories,omitempty"` - EnglishOnlyCategories []string `yaml:"english_only_categories,omitempty"` - NonEnglishOnlyCategories []string `yaml:"non_english_only_categories,omitempty"` - AdditionalLanguages []string `yaml:"additional_languages,omitempty"` + EnglishOnlyCategories ArrayString `yaml:"english_only_categories,omitempty"` + NonEnglishOnlyCategories ArrayString `yaml:"non_english_only_categories,omitempty"` + AdditionalLanguages ArrayString `yaml:"additional_languages,omitempty"` FileStorage string `yaml:"filestorage,omitempty"` StorageLink string `yaml:"storage_link,omitempty"` CacheLink string `yaml:"cache_link,omitempty"` @@ -198,3 +199,14 @@ type SearchConfig struct { ElasticsearchIndex string `yaml:"es_index,omitempty"` ElasticsearchType string `yaml:"es_type,omitempty"` } + +type ArrayString []string + +func (ar ArrayString) Contains(str string) bool { + for _, s := range ar { + if s == str { + return true + } + } + return false +} diff --git a/controllers/torrent/router.go b/controllers/torrent/router.go index 126654b8..3cb584ef 100644 --- a/controllers/torrent/router.go +++ b/controllers/torrent/router.go @@ -9,6 +9,8 @@ func init() { { torrentRoutes.GET("/", TorrentEditUserPanel) torrentRoutes.POST("/", TorrentPostEditUserPanel) + torrentRoutes.GET("/tag", ViewFormTag) + torrentRoutes.POST("/tag", ViewFormTag) torrentRoutes.GET("/delete", TorrentDeleteUserPanel) } torrentViewRoutes := router.Get().Group("/view") diff --git a/controllers/torrent/tag.go b/controllers/torrent/tag.go index f1a87f6f..7ee60fd3 100644 --- a/controllers/torrent/tag.go +++ b/controllers/torrent/tag.go @@ -1,8 +1,17 @@ package torrentController import ( + "errors" + + "fmt" + "net/http" + "strconv" + + "github.com/NyaaPantsu/nyaa/controllers/router" "github.com/NyaaPantsu/nyaa/models" "github.com/NyaaPantsu/nyaa/models/tags" + "github.com/NyaaPantsu/nyaa/models/torrents" + "github.com/NyaaPantsu/nyaa/templates" msg "github.com/NyaaPantsu/nyaa/utils/messages" "github.com/NyaaPantsu/nyaa/utils/validator" "github.com/NyaaPantsu/nyaa/utils/validator/tags" @@ -16,6 +25,12 @@ func postTag(c *gin.Context, torrent *models.Torrent, user *models.User) { c.Bind(tagForm) validator.ValidateForm(tagForm, messages) + // We check that the tag type sent is one enabled in config.yml + if !tagsValidator.CheckTagType(tagForm.Type) { + messages.ErrorT(errors.New("wrong_tag_type")) + return + } + for _, tag := range user.Tags { if tag.Tag == tagForm.Tag { return // already a tag by the user, don't add one more @@ -25,3 +40,45 @@ func postTag(c *gin.Context, torrent *models.Torrent, user *models.User) { tags.Create(tagForm.Tag, tagForm.Type, torrent, user) // Add a tag to the db tags.Filter(tagForm.Tag, tagForm.Type, torrent.ID) // Check if we have a tag reaching the maximum weight, if yes, deletes every tag and add only the one accepted } + +func ViewFormTag(c *gin.Context) { + messages := msg.GetMessages(c) + user := router.GetUser(c) + id, _ := strconv.ParseInt(c.Query("id"), 10, 32) + // Retrieve the torrent + torrent, err := torrents.FindByID(uint(id)) + + // If torrent not found, display 404 + if err != nil { + c.Status(http.StatusNotFound) + return + } + + // We load tags for user and torrents + user.LoadTags(torrent) + torrent.LoadTags() + + // We add a tag if posted + if c.PostForm("tag") != "" && user.ID > 0 { + postTag(c, torrent, user) + if !messages.HasErrors() { + if _, ok := c.GetQuery("json"); ok { + c.JSON(http.StatusOK, struct { + Ok bool + }{true}) + return + } + c.Redirect(http.StatusSeeOther, fmt.Sprintf("/view/%d", id)) + } + if _, ok := c.GetQuery("json"); ok { + c.JSON(http.StatusOK, struct { + Ok bool + }{false}) + return + } + } + tagForm := &tagsValidator.CreateForm{} + c.Bind(tagForm) + + templates.Form(c, "/site/torrents/tag.jet.html", tagForm) +} diff --git a/templates/site/torrents/edit.jet.html b/templates/site/torrents/edit.jet.html index 943cfe0b..820d81e4 100644 --- a/templates/site/torrents/edit.jet.html +++ b/templates/site/torrents/edit.jet.html @@ -10,12 +10,12 @@ {{ yield csrf_field() }}
- + {{ yield errors(name="name")}}
- {{ range _, cat := GetCategories(false, true) }} @@ -25,7 +25,7 @@
-
+
{{ yield flagList(languages=GetTorrentLanguages(), selected=Form.Languages) }}
{{ yield errors(name="language")}} diff --git a/templates/site/torrents/tag.jet.html b/templates/site/torrents/tag.jet.html new file mode 100644 index 00000000..01623443 --- /dev/null +++ b/templates/site/torrents/tag.jet.html @@ -0,0 +1,29 @@ +{{ extends "layouts/index_site" }} +{{ import "layouts/partials/helpers/csrf" }} +{{ import "layouts/partials/helpers/errors" }} +{{block title()}}{{T("add_tag")}}{{end}} +{{block content_body()}} +
+

{{ T("add_tag") }}

+
+ {{ yield csrf_field() }} +
+ + + {{ yield errors(name="Tag")}} +
+
+ + + {{ yield errors(name="Type")}} +
+ +
+
+
+
+{{end}} \ No newline at end of file diff --git a/templates/template_test.go b/templates/template_test.go index 455bf9f5..0ec3bad7 100644 --- a/templates/template_test.go +++ b/templates/template_test.go @@ -47,12 +47,14 @@ type ContextTest map[string]func(jet.VarMap) jet.VarMap func walkDirTest(dir string, t *testing.T) { fu := "http://nyaa.cat" em := "cop@cat.fe" - fakeUser := &models.User{1, "test", "test", "test", 1, time.Now(), time.Now(), "test", time.Now(), "en", "test", "test", "test", "test", []models.User{}, []models.User{}, "test", []models.Torrent{}, []models.Notification{}, 1, models.UserSettings{}} + + fakeTag := &models.Tag{1, 1, "12345", "anidbid", 1, false, 0} + fakeUser := &models.User{1, "test", "test", "test", 1, time.Now(), time.Now(), "test", time.Now(), "en", "test", "test", "test", "test", 0, []models.User{}, []models.User{}, "test", []models.Torrent{}, []models.Notification{}, 1, models.UserSettings{}, []models.Tag{*fakeTag}} fakeComment := &models.Comment{1, 1, 1, "test", time.Now(), time.Now(), nil, &models.Torrent{}, fakeUser} fakeScrapeData := &models.Scrape{1, 0, 0, 10, time.Now()} fakeFile := &models.File{1, 1, "l12:somefile.mp4e", 3} fakeLanguages := []string{"fr", "en"} - fakeTorrent := &models.Torrent{1, "test", "test", 3, 12, 1, false, time.Now(), 1, 0, 3, "test", "test", "test", "test", "test", nil, fakeUser, "test", []models.OldComment{}, []models.Comment{*fakeComment, *fakeComment}, fakeScrapeData, []models.File{*fakeFile}, fakeLanguages} + fakeTorrent := &models.Torrent{1, "test", "test", 3, 12, 1, false, time.Now(), 1, 0, 3, "test", "test", "test", "test", "test", nil, fakeUser, "test", []models.OldComment{}, []models.Comment{*fakeComment, *fakeComment}, []models.Tag{*fakeTag, *fakeTag}, fakeScrapeData, []models.File{*fakeFile}, fakeLanguages} fakeActivity := &models.Activity{1, "t", "e", "s", 1, fakeUser} fakeDB := &models.DatabaseDump{time.Now(), 3, "test", "test"} fakeLanguage := &publicSettings.Language{"English", "en", "en-us"} @@ -63,6 +65,7 @@ func walkDirTest(dir string, t *testing.T) { fakeOauthForm := apiValidator.CreateForm{"", "f", []string{fu}, []string{}, []string{}, "", "fedr", fu, fu, fu, fu, []string{em}, ""} fakeOauthModel := fakeOauthForm.Bind(&models.OauthClient{}) fakeClient := client.Client{"", "", "", []string{""}, []string{""}, []string{""}, "", "", "", "", "", "", []string{""}, false} + contextvariables := ContextTest{ "dumps.jet.html": func(variables jet.VarMap) jet.VarMap { variables.Set("GPGLink", "test") @@ -158,7 +161,6 @@ func walkDirTest(dir string, t *testing.T) { return variables }, "callback.jet.html": func(variables jet.VarMap) jet.VarMap { - variables.Set("Callback", true) variables.Set("AccessToken", "") variables.Set("RefreshToken", "") @@ -190,6 +192,10 @@ func walkDirTest(dir string, t *testing.T) { variables.Set("Form", fakeOauthForm) return variables }, + "tag.jet.html": func(variables jet.VarMap) jet.VarMap { + variables.Set("Form", fakeTag) + return variables + }, } fmt.Printf("\nTesting Folder: %s\n", dir) diff --git a/translations/en-us.all.json b/translations/en-us.all.json index 02f1d9d2..e56cbb11 100644 --- a/translations/en-us.all.json +++ b/translations/en-us.all.json @@ -1926,5 +1926,29 @@ { "id": "torrent_age", "translation": "{1} days {2} hours ago" + }, + { + "id": "wrong_tag_type", + "translation": "The tag type selected doesn't exist" + }, + { + "id": "add_tag", + "translation": "Add a Tag" + }, + { + "id": "tagtype", + "translation": "Tag Type" + }, + { + "id": "tagtype_anidbid", + "translation": "Anidb ID" + }, + { + "id": "tagtype_vndbid", + "translation": "VNdb ID" + }, + { + "id": "tagtype_quality", + "translation": "Quality tag" } ]