Added html views + fix template test + View & JSON Controller
Cette révision appartient à :
Parent
e5c115b400
révision
8dddc5240c
8 fichiers modifiés avec 145 ajouts et 12 suppressions
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -10,12 +10,12 @@
|
|||
{{ yield csrf_field() }}
|
||||
<div class="form-group">
|
||||
<label class="input-label" for="name">{{ T("name")}}</label>
|
||||
<input type="text" name="name" class="form-input up-input" placeholder="{{ T("file_name")}}" value="{{Form.Name}}" required/>
|
||||
<input type="text" name="name" id="name" class="form-input up-input" placeholder="{{ T("file_name")}}" value="{{Form.Name}}" required/>
|
||||
{{ yield errors(name="name")}}
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="input-label" for="c">{{ T("category")}}</label>
|
||||
<select name="c" class="form-input up-input">
|
||||
<select name="c" id="c" class="form-input up-input">
|
||||
<option value="">{{ T("select_a_torrent_category")}}</option>
|
||||
{{ range _, cat := GetCategories(false, true) }}
|
||||
<option value="{{ cat.ID }}" {{if Form.Category == cat.ID }}selected{{end}}>{{T(cat.Name) }}</option>
|
||||
|
@ -25,7 +25,7 @@
|
|||
</div>
|
||||
<div class="form-group">
|
||||
<label class="input-label" for="language">{{ T("torrent_language") }}</label>
|
||||
<div name="language" class="form-input language">
|
||||
<div name="language" id="language" class="form-input language">
|
||||
{{ yield flagList(languages=GetTorrentLanguages(), selected=Form.Languages) }}
|
||||
</div>
|
||||
{{ yield errors(name="language")}}
|
||||
|
|
29
templates/site/torrents/tag.jet.html
Fichier normal
29
templates/site/torrents/tag.jet.html
Fichier normal
|
@ -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()}}
|
||||
<div class="box results">
|
||||
<h3 id="torrents">{{ T("add_tag") }}</h3>
|
||||
<form style="text-align:left;padding-left:10px;padding-right:10px;" enctype="multipart/form-data" role="upload" method="POST">
|
||||
{{ yield csrf_field() }}
|
||||
<div class="form-group">
|
||||
<label class="input-label" for="tag">{{ T("tag")}}</label>
|
||||
<input type="text" id="tag" name="tag" class="form-input up-input" placeholder="{{ T("file_name")}}" value="{{Form.Tag}}" required/>
|
||||
{{ yield errors(name="Tag")}}
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="input-label" for="type">{{ T("tagtype")}}</label>
|
||||
<select name="type" id="type" class="form-input up-input">
|
||||
{{ range _, type := Config.Torrents.Tags.Types }}
|
||||
<option value="{{ type }}" {{if Form.Type == type }}selected{{end}}>{{T("tagtype_" + type) }}</option>
|
||||
{{ end }}
|
||||
</select>
|
||||
{{ yield errors(name="Type")}}
|
||||
</div>
|
||||
<button type="submit" class="form-input up-input btn-green">{{ T("add")}}</button>
|
||||
<br/>
|
||||
<br/>
|
||||
</form>
|
||||
</div>
|
||||
{{end}}
|
|
@ -45,11 +45,11 @@ func TestTemplates(t *testing.T) {
|
|||
type ContextTest map[string]func(jet.VarMap) jet.VarMap
|
||||
|
||||
func walkDirTest(dir string, t *testing.T) {
|
||||
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{}}
|
||||
fu := "http://nyaa.cat"
|
||||
em := "cop@cat.fe"
|
||||
|
||||
fakeTag := &models.Tag{1, 1, "12345", "anidbid", 1, false}
|
||||
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}
|
||||
|
@ -65,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")
|
||||
|
@ -160,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", "")
|
||||
|
@ -192,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)
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
]
|
||||
|
|
Référencer dans un nouveau ticket