diff options
author | Unknown <joe2010xtmf@163.com> | 2014-04-10 14:37:43 -0400 |
---|---|---|
committer | Unknown <joe2010xtmf@163.com> | 2014-04-10 14:37:43 -0400 |
commit | af552596cfd7f6fd05dfc38abaaffad1d7fed654 (patch) | |
tree | cf8efb59ef25077f441f2a3ae8bf2ba40f252649 /modules/middleware/context.go | |
parent | b3f1ae1ba6144b25c937d7b6db085e6b902e74ab (diff) | |
download | gitea-af552596cfd7f6fd05dfc38abaaffad1d7fed654.tar.gz gitea-af552596cfd7f6fd05dfc38abaaffad1d7fed654.zip |
Work on form resubmit
Diffstat (limited to 'modules/middleware/context.go')
-rw-r--r-- | modules/middleware/context.go | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/modules/middleware/context.go b/modules/middleware/context.go index 8129b13b7e..272af33052 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -11,6 +11,7 @@ import ( "fmt" "html/template" "net/http" + "net/url" "strconv" "strings" "time" @@ -34,6 +35,7 @@ type Context struct { p martini.Params Req *http.Request Res http.ResponseWriter + Flash *Flash Session session.SessionStore Cache cache.Cache User *models.User @@ -78,6 +80,7 @@ func (ctx *Context) HasError() bool { if !ok { return false } + ctx.Flash.Error(ctx.Data["ErrorMsg"].(string)) return hasErr.(bool) } @@ -88,8 +91,7 @@ func (ctx *Context) HTML(status int, name string, htmlOpt ...HTMLOptions) { // RenderWithErr used for page has form validation but need to prompt error to users. func (ctx *Context) RenderWithErr(msg, tpl string, form auth.Form) { - ctx.Data["HasError"] = true - ctx.Data["ErrorMsg"] = msg + ctx.Flash.Error(msg) if form != nil { auth.AssignForm(form, ctx.Data) } @@ -239,6 +241,21 @@ func (ctx *Context) CsrfTokenValid() bool { return true } +type Flash struct { + url.Values + ErrorMsg, SuccessMsg string +} + +func (f *Flash) Error(msg string) { + f.Set("error", msg) + f.ErrorMsg = msg +} + +func (f *Flash) Success(msg string) { + f.Set("success", msg) + f.SuccessMsg = msg +} + // InitContext initializes a classic context for a request. func InitContext() martini.Handler { return func(res http.ResponseWriter, r *http.Request, c martini.Context, rd *Render) { @@ -256,9 +273,24 @@ func InitContext() martini.Handler { // start session ctx.Session = base.SessionManager.SessionStart(res, r) + + ctx.Flash = &Flash{} + // Get flash. + values, err := url.ParseQuery(ctx.GetCookie("gogs_flash")) + if err != nil { + log.Error("InitContext.ParseQuery(flash): %v", err) + } else { + ctx.Flash.Values = values + ctx.Data["Flash"] = ctx.Flash + } + rw := res.(martini.ResponseWriter) rw.Before(func(martini.ResponseWriter) { ctx.Session.SessionRelease(res) + + if flash := ctx.Flash.Encode(); len(flash) > 0 { + ctx.SetCookie("gogs_flash", ctx.Flash.Encode(), -1) + } }) // Get user from session if logined. |