Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

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 à :
akuma06 2017-09-03 02:30:30 +02:00 révisé par ewhal
Parent 38d3e45ef4
révision 50a6e844db
6 fichiers modifiés avec 52 ajouts et 41 suppressions

Voir le fichier

@ -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 { func ScopesRequired(scopes ...string) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
mySessionData := oauth2.NewSession("", "") mySessionData := oauth2.NewSession("", "")

Voir le fichier

@ -1,11 +1,14 @@
package torrentController package torrentController
import "github.com/NyaaPantsu/nyaa/controllers/router" import (
"github.com/NyaaPantsu/nyaa/controllers/middlewares"
"github.com/NyaaPantsu/nyaa/controllers/router"
)
func init() { func init() {
router.Get().Any("/download/:hash", DownloadTorrent) router.Get().Any("/download/:hash", DownloadTorrent)
torrentRoutes := router.Get().Group("/torrent") torrentRoutes := router.Get().Group("/torrent", middlewares.LoggedInMiddleware())
{ {
torrentRoutes.GET("/", TorrentEditUserPanel) torrentRoutes.GET("/", TorrentEditUserPanel)
torrentRoutes.POST("/", TorrentPostEditUserPanel) torrentRoutes.POST("/", TorrentPostEditUserPanel)

Voir le fichier

@ -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 // ViewFormTag is a controller displaying a form to add a tag to a torrent
func ViewFormTag(c *gin.Context) { func ViewFormTag(c *gin.Context) {
messages := msg.GetMessages(c)
user := router.GetUser(c) user := router.GetUser(c)
// We add a tag only if user logged
if user.ID > 0 {
messages := msg.GetMessages(c)
id, _ := strconv.ParseInt(c.Query("id"), 10, 32) id, _ := strconv.ParseInt(c.Query("id"), 10, 32)
// Retrieve the torrent // Retrieve the torrent
torrent, err := torrents.FindByID(uint(id)) torrent, err := torrents.FindByID(uint(id))
var tagsForm models.Tags
// If torrent not found, display 404 // If torrent not found, display 404
if err != nil { if err != nil {
c.Status(http.StatusNotFound) c.Status(http.StatusNotFound)
return return
} }
// We add a tag if posted
if user.ID > 0 {
// We load tags for user so we can check if they have them // We load tags for user so we can check if they have them
user.LoadTags(torrent) user.LoadTags(torrent)
tag := postTags(c, torrent, user) if c.Request.Method == "POST" {
tagsForm = postTags(c, torrent, user)
if _, ok := c.GetQuery("json"); ok { if _, ok := c.GetQuery("json"); ok {
apiUtils.ResponseHandler(c, tag) apiUtils.ResponseHandler(c, tagsForm)
return return
} }
if !messages.HasErrors() { if !messages.HasErrors() {
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/view/%d", id)) 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 // AddTag is a controller to add a tag

Voir le fichier

@ -2,7 +2,8 @@
{{if isset(tag) && (accepted || User.ID > 0) }} {{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{{ 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}}"> <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> </span>
{{ if !accepted }} {{ 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> <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>

Voir le fichier

@ -1,25 +1,21 @@
{{ extends "layouts/index_site" }} {{ extends "layouts/index_site" }}
{{ import "layouts/partials/helpers/csrf" }} {{ import "layouts/partials/helpers/csrf" }}
{{ import "layouts/partials/helpers/errors" }} {{ import "layouts/partials/helpers/errors" }}
{{ import "layouts/partials/helpers/tag_form" }}
{{block title()}}{{T("add_tag")}}{{end}} {{block title()}}{{T("add_tag")}}{{end}}
{{block content_body()}} {{block content_body()}}
<div class="box results"> <div class="box results">
<h3 id="torrents">{{ T("add_tag") }}</h3> <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"> <form style="text-align:left;padding-left:10px;padding-right:10px;" enctype="multipart/form-data" role="upload" method="POST">
<div class="upload-tag-form">
{{ yield csrf_field() }} {{ yield csrf_field() }}
<div class="form-group"> {{ range Config.Torrents.Tags.Types }}
<label class="input-label" for="tag">{{ T("tag")}}</label> {{ yield tagForm(tagType=., acceptedTag=Form.Get(.Name)) }}
<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 }} {{ end }}
</select> <div class="form-group">
{{ yield errors(name="Type")}} <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> </div>
<button type="submit" class="form-input up-input btn-green">{{ T("add")}}</button> <button type="submit" class="form-input up-input btn-green">{{ T("add")}}</button>
<br/> <br/>

Voir le fichier

@ -207,7 +207,7 @@ func walkDirTest(dir string, t *testing.T) {
return variables return variables
}, },
"tag.jet.html": func(variables jet.VarMap) jet.VarMap { "tag.jet.html": func(variables jet.VarMap) jet.VarMap {
variables.Set("Form", fakeTag) variables.Set("Form", models.Tags{*fakeTag, *fakeTag, *fakeTag})
return variables return variables
}, },
} }