Fix adding tag form (nojs) + miscalleneous (#1468)
* Fix search Status Should fix #1428 ?s=2 and others. Reason: the sql query wasn't correct when filtering (status >= ? = ?) * Fix adding tag form (nojs) + miscalleneous * Fix #1423 by checking http method before finding the tags. * Form is now like other tags form * New Middleware LoggedInMiddleware to protect routes to logged user only * Display of Non-Accepted tags in torrent view improved by translating tagtype values * Update tag.jet.html
Cette révision appartient à :
Parent
38d3e45ef4
révision
50a6e844db
6 fichiers modifiés avec 52 ajouts et 41 suppressions
|
@ -46,6 +46,17 @@ func ModMiddleware() gin.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// LoggedInMiddleware make sure that the user is logged in
|
||||
func LoggedInMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
currentUser := router.GetUser(c)
|
||||
if currentUser.ID == 0 {
|
||||
NotFoundHandler(c)
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func ScopesRequired(scopes ...string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
mySessionData := oauth2.NewSession("", "")
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
package torrentController
|
||||
|
||||
import "github.com/NyaaPantsu/nyaa/controllers/router"
|
||||
import (
|
||||
"github.com/NyaaPantsu/nyaa/controllers/middlewares"
|
||||
"github.com/NyaaPantsu/nyaa/controllers/router"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.Get().Any("/download/:hash", DownloadTorrent)
|
||||
|
||||
torrentRoutes := router.Get().Group("/torrent")
|
||||
torrentRoutes := router.Get().Group("/torrent", middlewares.LoggedInMiddleware())
|
||||
{
|
||||
torrentRoutes.GET("/", TorrentEditUserPanel)
|
||||
torrentRoutes.POST("/", TorrentPostEditUserPanel)
|
||||
|
|
|
@ -64,36 +64,36 @@ func postTags(c *gin.Context, torrent *models.Torrent, user *models.User) []mode
|
|||
|
||||
// ViewFormTag is a controller displaying a form to add a tag to a torrent
|
||||
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 add a tag if posted
|
||||
// We add a tag only if user logged
|
||||
if user.ID > 0 {
|
||||
messages := msg.GetMessages(c)
|
||||
id, _ := strconv.ParseInt(c.Query("id"), 10, 32)
|
||||
// Retrieve the torrent
|
||||
torrent, err := torrents.FindByID(uint(id))
|
||||
|
||||
var tagsForm models.Tags
|
||||
// If torrent not found, display 404
|
||||
if err != nil {
|
||||
c.Status(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// We load tags for user so we can check if they have them
|
||||
user.LoadTags(torrent)
|
||||
tag := postTags(c, torrent, user)
|
||||
if _, ok := c.GetQuery("json"); ok {
|
||||
apiUtils.ResponseHandler(c, tag)
|
||||
return
|
||||
if c.Request.Method == "POST" {
|
||||
tagsForm = postTags(c, torrent, user)
|
||||
if _, ok := c.GetQuery("json"); ok {
|
||||
apiUtils.ResponseHandler(c, tagsForm)
|
||||
return
|
||||
}
|
||||
if !messages.HasErrors() {
|
||||
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/view/%d", id))
|
||||
}
|
||||
}
|
||||
if !messages.HasErrors() {
|
||||
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/view/%d", id))
|
||||
}
|
||||
}
|
||||
tagForm := &tagsValidator.CreateForm{}
|
||||
c.Bind(tagForm)
|
||||
|
||||
templates.Form(c, "/site/torrents/tag.jet.html", tagForm)
|
||||
templates.Form(c, "/site/torrents/tag.jet.html", tagsForm)
|
||||
}
|
||||
}
|
||||
|
||||
// AddTag is a controller to add a tag
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
{{if isset(tag) && (accepted || User.ID > 0) }}
|
||||
<span class="tag{{ if !accepted }} pending{{else}} accepted{{end}}" title="Tag: {{ tag.Type }} ({{ if !accepted }}{{ tag.Total }}{{else}}{{ T("accepted") }}{{end}})"{{ if !accepted }} data-weight="{{ tag.Total }}"{{end}}>
|
||||
<span class="tag-text{{ if User.ID > 0 }} votable{{end}}">
|
||||
{{ if tag.Type != Config.Torrents.Tags.Default }}{{ T("tagtype_" + tag.Type) }}: {{ end }}{{ tag.Tag }}
|
||||
{{ if tag.Type != Config.Torrents.Tags.Default }}{{ T("tagtype_" + tag.Type) }}: {{ end }}
|
||||
{{ if len(Config.Torrents.Tags.Types.Get(tag.Type).Defaults) > 0 && Config.Torrents.Tags.Types.Get(tag.Type).Defaults[0] != "db" }}{{ T("tagvalue_"+tag.Tag) }}{{else}}{{ tag.Tag }}{{end}}
|
||||
</span>
|
||||
{{ if !accepted }}
|
||||
<a href="/torrent/tag/{{ if User.Tags.Contains(.) }}remove{{else}}add{{end}}?id={{torrentID}}&tag={{ tag.Tag }}&type={{ tag.Type }}" class="tag-form {{ if User.Tags.Contains(.) }}minus{{else}}plus{{end}}{{if accepted}} accepted{{end}}"></a>
|
||||
|
|
|
@ -1,25 +1,21 @@
|
|||
{{ extends "layouts/index_site" }}
|
||||
{{ import "layouts/partials/helpers/csrf" }}
|
||||
{{ import "layouts/partials/helpers/errors" }}
|
||||
{{ import "layouts/partials/helpers/tag_form" }}
|
||||
{{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" 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[0] }}" {{if Form.Type == type[0] }}selected{{end}}>{{T("tagtype_" + type[0]) }}</option>
|
||||
{{ end }}
|
||||
</select>
|
||||
{{ yield errors(name="Type")}}
|
||||
<div class="upload-tag-form">
|
||||
{{ yield csrf_field() }}
|
||||
{{ range Config.Torrents.Tags.Types }}
|
||||
{{ yield tagForm(tagType=., acceptedTag=Form.Get(.Name)) }}
|
||||
{{ end }}
|
||||
<div class="form-group">
|
||||
<label class="input-label" for="tag_{{Config.Torrents.Tags.Default}}">{{ T("tagtype_tags") }}</label>
|
||||
<input type="text" class="form-input" name="tag_{{Config.Torrents.Tags.Default}}" id="tag_{{Config.Torrents.Tags.Default}}" value="{{ Form.Get(Config.Torrents.Tags.Default).Tag}}" />
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit" class="form-input up-input btn-green">{{ T("add")}}</button>
|
||||
<br/>
|
||||
|
|
|
@ -207,7 +207,7 @@ func walkDirTest(dir string, t *testing.T) {
|
|||
return variables
|
||||
},
|
||||
"tag.jet.html": func(variables jet.VarMap) jet.VarMap {
|
||||
variables.Set("Form", fakeTag)
|
||||
variables.Set("Form", models.Tags{*fakeTag, *fakeTag, *fakeTag})
|
||||
return variables
|
||||
},
|
||||
}
|
||||
|
|
Référencer dans un nouveau ticket