diff --git a/gogs.go b/gogs.go index 552a4db272..1e689e83b5 100644 --- a/gogs.go +++ b/gogs.go @@ -19,7 +19,7 @@ import ( // Test that go1.1 tag above is included in builds. main.go refers to this definition. const go11tag = true -const APP_VER = "0.0.2.0307" +const APP_VER = "0.0.2.0308" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/modules/auth/form.go b/modules/auth/form.go index a90f3d8542..b085527554 100644 --- a/modules/auth/form.go +++ b/modules/auth/form.go @@ -17,6 +17,7 @@ import ( "github.com/gogits/gogs/modules/log" ) +// Web form interface. type Form interface { Name(field string) string } diff --git a/modules/auth/repo.go b/modules/auth/repo.go new file mode 100644 index 0000000000..6656654f56 --- /dev/null +++ b/modules/auth/repo.go @@ -0,0 +1,53 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package auth + +import ( + "net/http" + "reflect" + + "github.com/codegangsta/martini" + + "github.com/gogits/binding" + + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" +) + +type CreateRepoForm struct { + UserId int64 `form:"userId"` + RepoName string `form:"repo" binding:"Required"` + Visibility string `form:"visibility"` + Description string `form:"desc" binding:"MaxSize(100)"` + Language string `form:"language"` + InitReadme string `form:"initReadme"` +} + +func (f *CreateRepoForm) Name(field string) string { + names := map[string]string{ + "RepoName": "Repository name", + "Description": "Description", + } + return names[field] +} + +func (f *CreateRepoForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { + if req.Method == "GET" || errors.Count() == 0 { + return + } + + data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) + data["HasError"] = true + AssignForm(f, data) + + if len(errors.Overall) > 0 { + for _, err := range errors.Overall { + log.Error("CreateRepoForm.Validate: %v", err) + } + return + } + + validate(errors, data, f) +} diff --git a/routers/repo/repo.go b/routers/repo/repo.go index e9ad7d1ab4..834195d83f 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -7,7 +7,6 @@ package repo import ( "fmt" "net/http" - "strconv" "github.com/martini-contrib/render" "github.com/martini-contrib/sessions" @@ -18,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/log" ) -func Create(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { +func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { data["Title"] = "Create repository" if req.Method == "GET" { @@ -26,30 +25,37 @@ func Create(req *http.Request, r render.Render, data base.TmplData, session sess return } + if hasErr, ok := data["HasError"]; ok && hasErr.(bool) { + r.HTML(200, "repo/create", data) + return + } + // TODO: access check - id, err := strconv.ParseInt(req.FormValue("userId"), 10, 64) - if err == nil { - var u *models.User - u, err = models.GetUserById(id) - if u == nil { - err = models.ErrUserNotExist - } - if err == nil { - _, err = models.CreateRepository(u, req.FormValue("name")) - } - if err == nil { - data["RepoName"] = u.Name + "/" + req.FormValue("name") - r.HTML(200, "repo/created", data) + user, err := models.GetUserById(form.UserId) + if err != nil { + if err.Error() == models.ErrUserNotExist.Error() { + data["HasError"] = true + data["ErrorMsg"] = "User does not exist" + auth.AssignForm(form, data) + r.HTML(200, "repo/create", data) return } } - if err != nil { - data["ErrorMsg"] = err - log.Error("repo.Create: %v", err) - r.HTML(200, "base/error", data) + if err == nil { + // TODO: init description and readme + if _, err = models.CreateRepository(user, form.RepoName); err == nil { + data["RepoName"] = user.Name + "/" + form.RepoName + r.HTML(200, "repo/created", data) + fmt.Println("good!!!!") + return + } } + + data["ErrorMsg"] = err + log.Error("repo.Create: %v", err) + r.HTML(200, "base/error", data) } func Delete(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { diff --git a/routers/user/user.go b/routers/user/user.go index a07d79e454..51a84400ab 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -64,8 +64,8 @@ func SignIn(form auth.LogInForm, data base.TmplData, req *http.Request, r render } data["ErrorMsg"] = err - log.Error("user.SignIn: %v", data) - r.HTML(200, "base/error", nil) + log.Error("user.SignIn: %v", err) + r.HTML(200, "base/error", data) return } diff --git a/templates/repo/create.tmpl b/templates/repo/create.tmpl index 4c7f4dd272..e7a3633489 100644 --- a/templates/repo/create.tmpl +++ b/templates/repo/create.tmpl @@ -3,6 +3,7 @@

Create New Repository

+
{{.ErrorMsg}}
@@ -11,10 +12,10 @@
-
+
- + Great repository names are short and memorable.
@@ -23,13 +24,14 @@

Public

+
-
+
- +
@@ -51,7 +53,7 @@
diff --git a/templates/repo/created.tmpl b/templates/repo/created.tmpl index aa47cb3fdc..35803e3938 100644 --- a/templates/repo/created.tmpl +++ b/templates/repo/created.tmpl @@ -1,8 +1,8 @@ {{template "base/head" .}} {{template "base/navbar" .}} -
-
- Created successfully! -
+
+
+ Created successfully! +
{{template "base/footer" .}} \ No newline at end of file diff --git a/web.go b/web.go index 2b6041b123..1b61ca0e1b 100644 --- a/web.go +++ b/web.go @@ -69,7 +69,7 @@ func runWeb(*cli.Context) { m.Any("/user/publickey/add", auth.SignInRequire(true), user.AddPublicKey) m.Any("/user/publickey/list", auth.SignInRequire(true), user.ListPublicKey) - m.Any("/repo/create", auth.SignInRequire(true), repo.Create) + m.Any("/repo/create", auth.SignInRequire(true), binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create) m.Any("/repo/delete", auth.SignInRequire(true), repo.Delete) m.Any("/repo/list", auth.SignInRequire(false), repo.List)