Work on form resubmit
This commit is contained in:
parent
b3f1ae1ba6
commit
af552596cf
2
gogs.go
2
gogs.go
@ -19,7 +19,7 @@ import (
|
||||
// Test that go1.2 tag above is included in builds. main.go refers to this definition.
|
||||
const go12tag = true
|
||||
|
||||
const APP_VER = "0.2.3.0409 Alpha"
|
||||
const APP_VER = "0.2.3.0410 Alpha"
|
||||
|
||||
func init() {
|
||||
base.AppVer = APP_VER
|
||||
|
@ -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.
|
||||
|
@ -23,6 +23,10 @@ import (
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
)
|
||||
|
||||
type installRouter int
|
||||
|
||||
var InstallRouter installRouter = 1
|
||||
|
||||
// Check run mode(Default of martini is Dev).
|
||||
func checkRunMode() {
|
||||
switch base.Cfg.MustValue("", "RUN_MODE") {
|
||||
@ -54,7 +58,7 @@ func GlobalInit() {
|
||||
checkRunMode()
|
||||
}
|
||||
|
||||
func Install(ctx *middleware.Context, form auth.InstallForm) {
|
||||
func (r installRouter) Get(ctx *middleware.Context, form auth.InstallForm) {
|
||||
if base.InstallLock {
|
||||
ctx.Handle(404, "install.Install", errors.New("Installation is prohibited"))
|
||||
return
|
||||
@ -63,7 +67,6 @@ func Install(ctx *middleware.Context, form auth.InstallForm) {
|
||||
ctx.Data["Title"] = "Install"
|
||||
ctx.Data["PageIsInstall"] = true
|
||||
|
||||
if ctx.Req.Method == "GET" {
|
||||
// Get and assign value to install form.
|
||||
if len(form.Host) == 0 {
|
||||
form.Host = models.DbCfg.Host
|
||||
@ -96,9 +99,17 @@ func Install(ctx *middleware.Context, form auth.InstallForm) {
|
||||
|
||||
auth.AssignForm(form, ctx.Data)
|
||||
ctx.HTML(200, "install")
|
||||
}
|
||||
|
||||
func (r installRouter) Post(ctx *middleware.Context, form auth.InstallForm) {
|
||||
if base.InstallLock {
|
||||
ctx.Handle(404, "install.Install", errors.New("Installation is prohibited"))
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["Title"] = "Install"
|
||||
ctx.Data["PageIsInstall"] = true
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, "install")
|
||||
return
|
||||
@ -197,5 +208,6 @@ func Install(ctx *middleware.Context, form auth.InstallForm) {
|
||||
}
|
||||
|
||||
log.Info("First-time run install finished!")
|
||||
ctx.Flash.Success("Welcome! We're glad that you choose Gogs, have fun and take care.")
|
||||
ctx.Redirect("/user/login")
|
||||
}
|
||||
|
1
templates/base/alert.tmpl
Normal file
1
templates/base/alert.tmpl
Normal file
@ -0,0 +1 @@
|
||||
{{if .Flash.ErrorMsg}}<div class="alert alert-danger form-error">{{.Flash.ErrorMsg}}</div>{{end}}
|
@ -3,7 +3,7 @@
|
||||
<form action="/install" method="post" class="form-horizontal card" id="install-card">
|
||||
{{.CsrfTokenHtml}}
|
||||
<h3>Install Steps For First-time Run</h3>
|
||||
<div class="alert alert-danger form-error{{if .HasError}}{{else}} hidden{{end}}">{{.ErrorMsg}}</div>
|
||||
{{template "base/alert" .}}
|
||||
<p class="help-block text-center">Gogs requires MySQL or PostgreSQL, SQLite3 only available for official binary version</p>
|
||||
<div class="form-group">
|
||||
<label class="col-md-3 control-label">Database Type: </label>
|
||||
|
5
web.go
5
web.go
@ -74,9 +74,12 @@ func runWeb(*cli.Context) {
|
||||
ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView})
|
||||
reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true})
|
||||
|
||||
bindIgnErr := binding.BindIgnErr
|
||||
|
||||
// Routers.
|
||||
m.Get("/", ignSignIn, routers.Home)
|
||||
m.Any("/install", binding.BindIgnErr(auth.InstallForm{}), routers.Install)
|
||||
m.Get("/install", bindIgnErr(auth.InstallForm{}), routers.InstallRouter.Get)
|
||||
m.Post("/install", bindIgnErr(auth.InstallForm{}), routers.InstallRouter.Post)
|
||||
m.Get("/issues", reqSignIn, user.Issues)
|
||||
m.Get("/pulls", reqSignIn, user.Pulls)
|
||||
m.Get("/stars", reqSignIn, user.Stars)
|
||||
|
Loading…
Reference in New Issue
Block a user