From 03ded2e027f3fd86ac89179b96234f9a890563b3 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Thu, 6 Jul 2017 22:19:44 +0200 Subject: [PATCH] Changing catSelect Behavior Return now Categories object instead of map[string]string. Remove some debug log --- controllers/rss_handler.go | 22 ++++++---------- controllers/template_functions.go | 25 +++++++------------ templates/admin/paneltorrentedit.jet.html | 4 +-- .../layouts/partials/helpers/search.jet.html | 4 +-- templates/site/torrents/edit.jet.html | 4 +-- templates/site/torrents/listing.jet.html | 4 +-- templates/site/torrents/upload.jet.html | 4 +-- utils/categories/categories.go | 8 +++--- utils/categories/categories_test.go | 8 +++--- utils/upload/upload.go | 7 +++--- 10 files changed, 39 insertions(+), 51 deletions(-) diff --git a/controllers/rss_handler.go b/controllers/rss_handler.go index c73fe107..e3534308 100644 --- a/controllers/rss_handler.go +++ b/controllers/rss_handler.go @@ -8,8 +8,6 @@ import ( "strings" "time" - "sort" - "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/models" "github.com/NyaaPantsu/nyaa/models/users" @@ -177,30 +175,26 @@ func RSSTorznabHandler(c *gin.Context) { } if t == "caps" { T := publicSettings.GetTfuncFromRequest(c) - cat := categories.GetSelect(true, true) + cats := categories.GetSelect(true, true) var categories []*nyaafeeds.RssCategoryTorznab categories = append(categories, &nyaafeeds.RssCategoryTorznab{ ID: "5070", Name: "Anime", Description: "Anime", }) - var keys []string - for name := range cat { - keys = append(keys, name) - } - sort.Strings(keys) + last := 0 - for _, key := range keys { - if len(cat[key]) <= 2 { + for _, v := range cats { + if len(v.ID) <= 2 { categories = append(categories, &nyaafeeds.RssCategoryTorznab{ - ID: nyaafeeds.ConvertFromCat(cat[key]), - Name: string(T(key)), + ID: nyaafeeds.ConvertFromCat(v.ID), + Name: string(T(v.Name)), }) last++ } else { categories[last].Subcat = append(categories[last].Subcat, &nyaafeeds.RssSubCat{ - ID: nyaafeeds.ConvertFromCat(cat[key]), - Name: string(T(key)), + ID: nyaafeeds.ConvertFromCat(v.ID), + Name: string(T(v.Name)), }) } } diff --git a/controllers/template_functions.go b/controllers/template_functions.go index 043155a8..3cbb2210 100644 --- a/controllers/template_functions.go +++ b/controllers/template_functions.go @@ -4,7 +4,6 @@ import ( "html/template" "math" "net/url" - "sort" "strconv" "time" @@ -161,29 +160,23 @@ func templateFunctions(vars jet.VarMap) jet.VarMap { return t.Format(time.RFC3339) }) vars.Set("GetHostname", format.GetHostname) - vars.Set("GetCategories", func(keepParent bool, keepChild bool) map[string]string { + vars.Set("GetCategories", func(keepParent bool, keepChild bool) categories.Categories { return categories.GetSelect(keepParent, keepChild) }) - vars.Set("GetCategory", func(category string, keepParent bool) (categoryRet map[string]string) { - cat := categories.GetSelect(true, true) - var keys []string - for name := range cat { - keys = append(keys, name) - } - - sort.Strings(keys) + vars.Set("GetCategory", func(category string, keepParent bool) (categoryRet categories.Categories) { + cats := categories.GetSelect(true, true) found := false - categoryRet = make(map[string]string) - for _, key := range keys { - if cat[key] == category+"_" { + categoryRet = make(categories.Categories, len(cats)) + for _, v := range cats { + if v.ID == category+"_" { found = true if keepParent { - categoryRet[key] = cat[key] + categoryRet = append(categoryRet, v) } - } else if len(cat[key]) <= 2 && len(categoryRet) > 0 { + } else if len(v.ID) <= 2 && len(categoryRet) > 0 { break } else if found { - categoryRet[key] = cat[key] + categoryRet = append(categoryRet, v) } } return diff --git a/templates/admin/paneltorrentedit.jet.html b/templates/admin/paneltorrentedit.jet.html index 1e121ad0..a7f13ad7 100644 --- a/templates/admin/paneltorrentedit.jet.html +++ b/templates/admin/paneltorrentedit.jet.html @@ -15,8 +15,8 @@ diff --git a/templates/layouts/partials/helpers/search.jet.html b/templates/layouts/partials/helpers/search.jet.html index 6af08115..44c20b8e 100644 --- a/templates/layouts/partials/helpers/search.jet.html +++ b/templates/layouts/partials/helpers/search.jet.html @@ -1,8 +1,8 @@ {{block search_common()}} diff --git a/templates/site/torrents/edit.jet.html b/templates/site/torrents/edit.jet.html index 599e6f7b..0d0d6d70 100644 --- a/templates/site/torrents/edit.jet.html +++ b/templates/site/torrents/edit.jet.html @@ -19,8 +19,8 @@ {{ yield errors(name="c")}} diff --git a/templates/site/torrents/listing.jet.html b/templates/site/torrents/listing.jet.html index d018f900..2fba163b 100644 --- a/templates/site/torrents/listing.jet.html +++ b/templates/site/torrents/listing.jet.html @@ -105,8 +105,8 @@ diff --git a/templates/site/torrents/upload.jet.html b/templates/site/torrents/upload.jet.html index e258a027..6e07f2ac 100644 --- a/templates/site/torrents/upload.jet.html +++ b/templates/site/torrents/upload.jet.html @@ -26,8 +26,8 @@

{{ T("category")}}

{{ yield errors(name="c")}} diff --git a/utils/categories/categories.go b/utils/categories/categories.go index c821176a..239e1bb8 100644 --- a/utils/categories/categories.go +++ b/utils/categories/categories.go @@ -79,11 +79,13 @@ func Exists(category string) bool { } // GetSelect : Format categories in map ordered alphabetically -func GetSelect(keepParent bool, keepChild bool) map[string]string { - catSelect := make(map[string]string, len(categories)) +func GetSelect(keepParent bool, keepChild bool) Categories { + catSelect := make(Categories, len(categories)) + k := 0 for _, v := range categories { if (keepParent && keepChild) || (len(v.ID) > 2 && !keepParent) || (len(v.ID) <= 2 && !keepChild) { - catSelect[v.Name] = v.ID + catSelect[k] = v + k++ } } return catSelect diff --git a/utils/categories/categories_test.go b/utils/categories/categories_test.go index 2f9a4aad..e777cfea 100644 --- a/utils/categories/categories_test.go +++ b/utils/categories/categories_test.go @@ -38,9 +38,9 @@ func TestCategoryExists(t *testing.T) { func TestGetCategoriesSelect(t *testing.T) { cats := GetSelect(true, false) for _, value := range cats { - split := strings.Split(value, "_") + split := strings.Split(value.ID, "_") if len(split) != 2 { - t.Errorf("The category %s doesn't have only one underscore", value) + t.Errorf("The category %s doesn't have only one underscore", value.Name) } if split[1] != "" { t.Errorf("The function doesn't filter out child categories, expected '', got %s", split[1]) @@ -48,9 +48,9 @@ func TestGetCategoriesSelect(t *testing.T) { } cats = GetSelect(false, true) for _, value := range cats { - split := strings.Split(value, "_") + split := strings.Split(value.ID, "_") if len(split) != 2 { - t.Errorf("The category %s doesn't have only one underscore", value) + t.Errorf("The category %s doesn't have only one underscore", value.Name) } if split[1] == "" { t.Error("The function doesn't filter out parent categories, expected a string, got nothing") diff --git a/utils/upload/upload.go b/utils/upload/upload.go index e2ff18e2..3c4fdb76 100644 --- a/utils/upload/upload.go +++ b/utils/upload/upload.go @@ -92,11 +92,10 @@ func ExtractBasicValue(c *gin.Context, r *torrentValidator.TorrentRequest) error r.WebsiteLink = strings.TrimSpace(r.WebsiteLink) r.Magnet = strings.TrimSpace(r.Magnet) - fmt.Println("Languages:") - fmt.Println(r.Languages) - r.Languages = c.PostFormArray("languages") + if len(r.Languages) == 0 { // Shouldn't have to do that since c.Bind actually bind arrays, but better off adding it in case gin doesn't do his work + r.Languages = c.PostFormArray("languages") + } // then actually check that we have everything we need - fmt.Println(r.Languages) err := r.ValidateDescription() if err != nil {