5991a21818
* First batch of changes for the refactor Added the support of gin in routes and other services/utils Begining implementation of JetHTML * Remove os folder * Move scrapers to own repo * Second batch of changes All .jet.html are the working templates. You can now test this PR, the index Page and upload works. If you want to complete the other html templates, you're welcome * Move captcha to util * Move uploadService to utils * Use govalidator instead of regex * Third batch of changes All the front end should as previously. I also fixed some minor things unrelated to the refactor (mostly style issues on static pages) Now errors can be accessed by importing the "errors" helpers and using the `yield errors(name="xxx")` command in templates. Same for infos. Templates are now more hierarchized with a base template "base.jet.html" which is extended depending on the context in "index_site" or "index_admin" layouts. Those layouts are extended than in every pages. Other helpers are captcha to render a captcha `yield captcha(captchaid="xxx")` And also csrf, with the command `yield csrf_field()` To translate, you don't have anymore to do `call $.T "xxx"`, you just have to do `T("xxx")`. Pages for the website part are in folders in the folder "templates/site". Pages for the admin part are in "templates/admin". Layouts are separated in "templates/layouts". Helpers and menu are in "templates/layouts/helpers" and "templates/layouts/menu". Error pages should be put in "templates/errors" * Added test on templates When adding a new template, you have to tell to template_test.go, the context of the new template (if it doesn't use the common context) * Panel admin works Now the templating part should work. The PR can now be fully tested. I think we should push the templating PR and do the routes/controllers/removal of services in another branch. So we know that this one is functional * Updated dependencies * Fixed test for modelhelper * Fix testing for commentlist * Fix travis :') * Just renamed router and removed network * Applying same SEO fix * Update form_validator.go * Added back regexp package
185 lignes
5,6 Kio
Go
185 lignes
5,6 Kio
Go
package controllers
|
|
|
|
import (
|
|
"net/http"
|
|
"path"
|
|
|
|
"github.com/NyaaPantsu/nyaa/config"
|
|
"github.com/NyaaPantsu/nyaa/model"
|
|
userForms "github.com/NyaaPantsu/nyaa/service/user/form"
|
|
"github.com/NyaaPantsu/nyaa/util/filelist"
|
|
"github.com/NyaaPantsu/nyaa/util/messages"
|
|
"github.com/NyaaPantsu/nyaa/util/publicSettings"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/justinas/nosurf"
|
|
|
|
"fmt"
|
|
|
|
"github.com/CloudyKit/jet"
|
|
)
|
|
|
|
// TemplateDir : Variable to the template directory
|
|
const TemplateDir = "./templates" // FIXME: Need to be a constant!
|
|
|
|
// ModeratorDir : Variable to the admin template sub directory
|
|
const ModeratorDir = "admin"
|
|
|
|
// SiteDir : Variable pointing to the site page templates
|
|
const SiteDir = "site"
|
|
|
|
// ErrorsDir : Variable pointing to the errors page templates
|
|
const ErrorsDir = "errors"
|
|
|
|
// View : Jet Template Renderer
|
|
var View = jet.NewHTMLSet("./templates")
|
|
var vars = templateFunctions(make(jet.VarMap))
|
|
|
|
func init() {
|
|
if config.Conf.Environment == "DEVELOPMENT" {
|
|
View.SetDevelopmentMode(true)
|
|
fmt.Println("Template Live Update enabled")
|
|
}
|
|
}
|
|
func commonVars(c *gin.Context) jet.VarMap {
|
|
msg := messages.GetMessages(c)
|
|
vars.Set("Navigation", newNavigation())
|
|
vars.Set("Search", newSearchForm(c))
|
|
vars.Set("T", publicSettings.GetTfuncFromRequest(c))
|
|
vars.Set("Theme", publicSettings.GetThemeFromRequest(c))
|
|
vars.Set("Mascot", publicSettings.GetMascotFromRequest(c))
|
|
vars.Set("MascotURL", publicSettings.GetMascotUrlFromRequest(c))
|
|
vars.Set("User", getUser(c))
|
|
vars.Set("URL", c.Request.URL)
|
|
vars.Set("CsrfToken", nosurf.Token(c.Request))
|
|
vars.Set("Config", config.Conf)
|
|
vars.Set("Infos", msg.GetAllInfos())
|
|
vars.Set("Errors", msg.GetAllErrors())
|
|
return vars
|
|
}
|
|
|
|
// newPanelSearchForm : Helper that creates a search form without items/page field
|
|
// these need to be used when the templateVariables don't include `navigation`
|
|
func newPanelSearchForm(c *gin.Context) searchForm {
|
|
form := newSearchForm(c)
|
|
form.ShowItemsPerPage = false
|
|
return form
|
|
}
|
|
|
|
//
|
|
func newPanelCommonVariables(c *gin.Context) jet.VarMap {
|
|
common := commonVars(c)
|
|
common.Set("Search", newPanelSearchForm(c))
|
|
return common
|
|
}
|
|
|
|
func renderTemplate(c *gin.Context, templateName string, vars jet.VarMap) {
|
|
t, err := View.GetTemplate(templateName)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
if err = t.Execute(c.Writer, vars, nil); err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func httpError(c *gin.Context, errorCode int) {
|
|
if errorCode == http.StatusNotFound {
|
|
c.Status(http.StatusNotFound)
|
|
staticTemplate(c, path.Join(ErrorsDir, "404.jet.html"))
|
|
return
|
|
}
|
|
c.AbortWithStatus(errorCode)
|
|
return
|
|
}
|
|
|
|
func staticTemplate(c *gin.Context, templateName string) {
|
|
var vars jet.VarMap
|
|
if isAdminTemplate(templateName) {
|
|
vars = newPanelCommonVariables(c)
|
|
} else {
|
|
vars = commonVars(c)
|
|
}
|
|
renderTemplate(c, templateName, vars)
|
|
}
|
|
|
|
func modelList(c *gin.Context, templateName string, models interface{}, nav navigation, search searchForm) {
|
|
var vars jet.VarMap
|
|
if isAdminTemplate(templateName) {
|
|
vars = newPanelCommonVariables(c)
|
|
} else {
|
|
vars = commonVars(c)
|
|
}
|
|
vars.Set("Models", models)
|
|
vars.Set("Navigation", nav)
|
|
vars.Set("Search", search)
|
|
renderTemplate(c, templateName, vars)
|
|
}
|
|
|
|
func formTemplate(c *gin.Context, templateName string, form interface{}) {
|
|
var vars jet.VarMap
|
|
if isAdminTemplate(templateName) {
|
|
vars = newPanelCommonVariables(c)
|
|
} else {
|
|
vars = commonVars(c)
|
|
}
|
|
vars.Set("Form", form)
|
|
renderTemplate(c, templateName, vars)
|
|
}
|
|
|
|
func torrentTemplate(c *gin.Context, torrent model.TorrentJSON, rootFolder *filelist.FileListFolder, captchaID string) {
|
|
vars := commonVars(c)
|
|
vars.Set("Torrent", torrent)
|
|
vars.Set("RootFolder", rootFolder)
|
|
vars.Set("CaptchaID", captchaID)
|
|
renderTemplate(c, path.Join(SiteDir, "torrents/view.jet.html"), vars)
|
|
}
|
|
|
|
func userProfileEditTemplate(c *gin.Context, userProfile *model.User, userForm userForms.UserForm, languages map[string]string) {
|
|
vars := commonVars(c)
|
|
vars.Set("UserProfile", userProfile)
|
|
vars.Set("UserForm", userForm)
|
|
vars.Set("Languages", languages)
|
|
renderTemplate(c, path.Join(SiteDir, "user/edit.jet.html"), vars)
|
|
}
|
|
|
|
func userProfileTemplate(c *gin.Context, userProfile *model.User) {
|
|
vars := commonVars(c)
|
|
vars.Set("UserProfile", userProfile)
|
|
renderTemplate(c, path.Join(SiteDir, "user/torrents.jet.html"), vars)
|
|
}
|
|
|
|
func userProfileNotificationsTemplate(c *gin.Context, userProfile *model.User) {
|
|
vars := commonVars(c)
|
|
vars.Set("UserProfile", userProfile)
|
|
renderTemplate(c, path.Join(SiteDir, "user/notifications.jet.html"), vars)
|
|
}
|
|
func databaseDumpTemplate(c *gin.Context, listDumps []model.DatabaseDumpJSON, GPGLink string) {
|
|
vars := commonVars(c)
|
|
vars.Set("ListDumps", listDumps)
|
|
vars.Set("GPGLink", GPGLink)
|
|
renderTemplate(c, path.Join(SiteDir, "database/dumps.jet.html"), vars)
|
|
}
|
|
func changeLanguageTemplate(c *gin.Context, language string, languages map[string]string) {
|
|
vars := commonVars(c)
|
|
vars.Set("Language", language)
|
|
vars.Set("Languages", languages)
|
|
renderTemplate(c, path.Join(SiteDir, "user/public/settings.jet.html"), vars)
|
|
}
|
|
|
|
func panelAdminTemplate(c *gin.Context, torrent []model.Torrent, reports []model.TorrentReportJSON, users []model.User, comments []model.Comment) {
|
|
vars := newPanelCommonVariables(c)
|
|
vars.Set("Torrents", torrent)
|
|
vars.Set("TorrentReports", reports)
|
|
vars.Set("Users", users)
|
|
vars.Set("Comments", comments)
|
|
renderTemplate(c, path.Join(ModeratorDir, "index.jet.html"), vars)
|
|
}
|
|
|
|
func isAdminTemplate(templateName string) bool {
|
|
if templateName != "" && len(templateName) > len(ModeratorDir) {
|
|
return templateName[:5] == ModeratorDir
|
|
}
|
|
return false
|
|
}
|