Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/util/messages/messages.go
akuma06 5991a21818 First batch of changes for the refactor (#1078)
* 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
2017-06-28 21:42:38 +10:00

177 lignes
5,6 Kio
Go

package messages
import (
"fmt"
"github.com/NyaaPantsu/nyaa/util/publicSettings"
"github.com/gin-gonic/gin"
"github.com/gorilla/context"
"github.com/nicksnyder/go-i18n/i18n"
)
// MessagesKey : use for context
const MessagesKey = "nyaapantsu.messages"
// Messages struct
type Messages struct {
Errors map[string][]string
Infos map[string][]string
c *gin.Context
T i18n.TranslateFunc
}
// GetMessages : Initialize or return the messages object from context
func GetMessages(c *gin.Context) *Messages {
if rv := context.Get(c.Request, MessagesKey); rv != nil {
mes := rv.(*Messages)
T, _ := publicSettings.GetTfuncAndLanguageFromRequest(c)
mes.T = T
mes.c = c
return mes
}
context.Set(c.Request, MessagesKey, &Messages{})
T, _ := publicSettings.GetTfuncAndLanguageFromRequest(c)
return &Messages{make(map[string][]string), make(map[string][]string), c, T}
}
// AddError : Add an error in category name with message msg
func (mes *Messages) AddError(name string, msg string) {
if mes.Errors == nil {
mes.Errors = make(map[string][]string)
}
mes.Errors[name] = append(mes.Errors[name], msg)
mes.setMessagesInContext()
}
// AddErrorf : Add an error in category name with message msg formatted with args
func (mes *Messages) AddErrorf(name string, msg string, args ...interface{}) {
mes.AddError(name, fmt.Sprintf(msg, args...))
}
// AddErrorTf : Add an error in category name with translation string id formatted with args
func (mes *Messages) AddErrorTf(name string, id string, args ...interface{}) {
mes.AddErrorf(name, mes.T(id), args...)
}
// AddErrorT : Add an error in category name with translation string id
func (mes *Messages) AddErrorT(name string, id string) {
mes.AddError(name, mes.T(id))
}
// ImportFromError : Add an error in category name with message msg imported from type error
func (mes *Messages) ImportFromError(name string, err error) {
mes.AddError(name, err.Error())
}
// ImportFromErrorT : Add an error in category name with message msg imported from type error
func (mes *Messages) ImportFromErrorT(name string, err error) {
mes.AddError(name, mes.T(err.Error()))
}
// ImportFromErrorTf : Add an error in category name with message msg imported from type error
func (mes *Messages) ImportFromErrorTf(name string, err error, args ...interface{}) {
mes.AddError(name, fmt.Sprintf(mes.T(err.Error()), args...))
}
// ImportFromError : Aliases to import directly an error in "errors" map index
func (mes *Messages) Error(err error) {
mes.ImportFromError("errors", err)
}
// ErrorT : Aliases to import directly an error in "errors" map index and translate the error
func (mes *Messages) ErrorT(err error) {
mes.ImportFromErrorT("errors", err)
}
// ErrorTf : Aliases to import directly an error in "errors" map index and translate the error with args
func (mes *Messages) ErrorTf(err error, args ...interface{}) {
mes.ImportFromErrorTf("errors", err)
}
// AddInfo : Add an info in category name with message msg
func (mes *Messages) AddInfo(name string, msg string) {
if mes.Infos == nil {
mes.Infos = make(map[string][]string)
}
mes.Infos[name] = append(mes.Infos[name], msg)
mes.setMessagesInContext()
}
// AddInfof : Add an info in category name with message msg formatted with args
func (mes *Messages) AddInfof(name string, msg string, args ...interface{}) {
mes.AddInfo(name, fmt.Sprintf(msg, args...))
}
// AddInfoTf : Add an info in category name with translation string id formatted with args
func (mes *Messages) AddInfoTf(name string, id string, args ...interface{}) {
mes.AddInfof(name, mes.T(id), args...)
}
// AddInfoT : Add an info in category name with translation string id
func (mes *Messages) AddInfoT(name string, id string) {
mes.AddInfo(name, mes.T(id))
}
// ClearAllErrors : Erase all errors in messages
func (mes *Messages) ClearAllErrors() {
mes.Errors = nil
mes.setMessagesInContext()
}
// ClearAllInfos : Erase all infos in messages
func (mes *Messages) ClearAllInfos() {
mes.Infos = nil
mes.setMessagesInContext()
}
// ClearErrors : Erase all errors in messages
func (mes *Messages) ClearErrors(name string) {
delete(mes.Errors, name)
mes.setMessagesInContext()
}
// ClearInfos : Erase all infos in messages
func (mes *Messages) ClearInfos(name string) {
delete(mes.Infos, name)
mes.setMessagesInContext()
}
// GetAllErrors : Get all errors
func (mes *Messages) GetAllErrors() map[string][]string {
mes = GetMessages(mes.c) // We need to look if any new errors from other functions has updated context
return mes.Errors
}
// GetErrors : Get all errors in category name
func (mes *Messages) GetErrors(name string) []string {
mes = GetMessages(mes.c) // We need to look if any new errors from other functions has updated context
return mes.Errors[name]
}
// GetAllInfos : Get all infos
func (mes *Messages) GetAllInfos() map[string][]string {
mes = GetMessages(mes.c) // We need to look if any new errors from other functions has updated context
return mes.Infos
}
// GetInfos : Get all infos in category name
func (mes *Messages) GetInfos(name string) []string {
mes = GetMessages(mes.c) // We need to look if any new errors from other functions has updated context
return mes.Infos[name]
}
// HasErrors : Check if there are errors
func (mes *Messages) HasErrors() bool {
mes = GetMessages(mes.c) // We need to look if any new errors from other functions has updated context
return len(mes.Errors) > 0
}
// HasInfos : Check if there are infos
func (mes *Messages) HasInfos() bool {
mes = GetMessages(mes.c) // We need to look if any new errors from other functions has updated context
return len(mes.Infos) > 0
}
func (mes *Messages) setMessagesInContext() {
context.Set(mes.c.Request, MessagesKey, mes)
}