From b9e57e41cee83f257caefec76a276dd143cec153 Mon Sep 17 00:00:00 2001 From: slene Date: Sat, 15 Mar 2014 22:34:33 +0800 Subject: [PATCH] clean code --- modules/middleware/context.go | 9 +-- routers/dashboard.go | 13 ++-- routers/repo/single.go | 35 +++++------ routers/user/setting.go | 115 ++++++++++++++++------------------ routers/user/user.go | 75 +++++++++++----------- 5 files changed, 117 insertions(+), 130 deletions(-) diff --git a/modules/middleware/context.go b/modules/middleware/context.go index dccd3351c7..7a1dba3d65 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -58,10 +58,11 @@ func InitContext() martini.Handler { ctx := &Context{ c: c, // p: p, - Req: r, - Res: res, - Data: data, - Render: rd, + Req: r, + Res: res, + Session: session, + Data: data, + Render: rd, } // Get user from session if logined. diff --git a/routers/dashboard.go b/routers/dashboard.go index de9378b0fa..cab1662fc3 100644 --- a/routers/dashboard.go +++ b/routers/dashboard.go @@ -5,20 +5,15 @@ package routers import ( - "github.com/martini-contrib/render" - "github.com/martini-contrib/sessions" - - "github.com/gogits/gogs/modules/auth" - "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/routers/user" ) -func Home(ctx *middleware.Context, r render.Render, data base.TmplData, session sessions.Session) { - if auth.IsSignedIn(session) { +func Home(ctx *middleware.Context) { + if ctx.IsSigned { user.Dashboard(ctx) return } - data["PageIsHome"] = true - r.HTML(200, "home", data) + ctx.Data["PageIsHome"] = true + ctx.Render.HTML(200, "home", ctx.Data) } diff --git a/routers/repo/single.go b/routers/repo/single.go index 605eb231f3..036bc21830 100644 --- a/routers/repo/single.go +++ b/routers/repo/single.go @@ -1,17 +1,16 @@ package repo import ( - "github.com/codegangsta/martini" - "github.com/martini-contrib/render" "strings" + "github.com/codegangsta/martini" + "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" ) -func Single(params martini.Params, ctx *middleware.Context, r render.Render, data base.TmplData) { - if !data["IsRepositoryValid"].(bool) { +func Single(ctx *middleware.Context, params martini.Params) { + if !ctx.Data["IsRepositoryValid"].(bool) { return } if params["branchname"] == "" { @@ -24,9 +23,9 @@ func Single(params martini.Params, ctx *middleware.Context, r render.Render, dat ctx.Handle(200, "repo.Single", err) return } - data["Username"] = params["username"] - data["Reponame"] = params["reponame"] - data["Branchname"] = params["branchname"] + ctx.Data["Username"] = params["username"] + ctx.Data["Reponame"] = params["reponame"] + ctx.Data["Branchname"] = params["branchname"] var treenames []string Paths := make([]string, 0) @@ -38,21 +37,21 @@ func Single(params martini.Params, ctx *middleware.Context, r render.Render, dat } } - data["Paths"] = Paths - data["Treenames"] = treenames - data["IsRepoToolbarSource"] = true - data["Files"] = files + ctx.Data["Paths"] = Paths + ctx.Data["Treenames"] = treenames + ctx.Data["IsRepoToolbarSource"] = true + ctx.Data["Files"] = files - r.HTML(200, "repo/single", data) + ctx.Render.HTML(200, "repo/single", ctx.Data) } -func Setting(r render.Render, data base.TmplData) { - if !data["IsRepositoryValid"].(bool) { +func Setting(ctx *middleware.Context) { + if !ctx.Data["IsRepositoryValid"].(bool) { return } - data["Title"] = data["Title"].(string) + " - settings" - data["IsRepoToolbarSetting"] = true + ctx.Data["Title"] = ctx.Data["Title"].(string) + " - settings" + ctx.Data["IsRepoToolbarSetting"] = true - r.HTML(200, "repo/setting", data) + ctx.Render.HTML(200, "repo/setting", ctx.Data) } diff --git a/routers/user/setting.go b/routers/user/setting.go index 347aec4e03..f38f2c1429 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -5,12 +5,8 @@ package user import ( - "net/http" "strconv" - "github.com/martini-contrib/render" - "github.com/martini-contrib/sessions" - "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/base" @@ -18,20 +14,20 @@ import ( "github.com/gogits/gogs/modules/middleware" ) -func Setting(form auth.UpdateProfileForm, ctx *middleware.Context, r render.Render, data base.TmplData, req *http.Request, session sessions.Session) { - data["Title"] = "Setting" - data["PageIsUserSetting"] = true +func Setting(ctx *middleware.Context, form auth.UpdateProfileForm) { + ctx.Data["Title"] = "Setting" + ctx.Data["PageIsUserSetting"] = true - user := auth.SignedInUser(session) - data["Owner"] = user + user := ctx.User + ctx.Data["Owner"] = user - if req.Method == "GET" { - r.HTML(200, "user/setting", data) + if ctx.Req.Method == "GET" { + ctx.Render.HTML(200, "user/setting", ctx.Data) return } - if hasErr, ok := data["HasError"]; ok && hasErr.(bool) { - r.HTML(200, "user/setting", data) + if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) { + ctx.Render.HTML(200, "user/setting", ctx.Data) return } @@ -45,20 +41,20 @@ func Setting(form auth.UpdateProfileForm, ctx *middleware.Context, r render.Rend return } - data["IsSuccess"] = true - r.HTML(200, "user/setting", data) + ctx.Data["IsSuccess"] = true + ctx.Render.HTML(200, "user/setting", ctx.Data) } -func SettingPassword(form auth.UpdatePasswdForm, ctx *middleware.Context, r render.Render, data base.TmplData, session sessions.Session, req *http.Request) { - data["Title"] = "Password" - data["PageIsUserSetting"] = true +func SettingPassword(ctx *middleware.Context, form auth.UpdatePasswdForm) { + ctx.Data["Title"] = "Password" + ctx.Data["PageIsUserSetting"] = true - if req.Method == "GET" { - r.HTML(200, "user/password", data) + if ctx.Req.Method == "GET" { + ctx.Render.HTML(200, "user/password", ctx.Data) return } - user := auth.SignedInUser(session) + user := ctx.User newUser := &models.User{Passwd: form.NewPasswd} if err := newUser.EncodePasswd(); err != nil { ctx.Handle(200, "setting.SettingPassword", err) @@ -66,35 +62,34 @@ func SettingPassword(form auth.UpdatePasswdForm, ctx *middleware.Context, r rend } if user.Passwd != newUser.Passwd { - data["HasError"] = true - data["ErrorMsg"] = "Old password is not correct" + ctx.Data["HasError"] = true + ctx.Data["ErrorMsg"] = "Old password is not correct" } else if form.NewPasswd != form.RetypePasswd { - data["HasError"] = true - data["ErrorMsg"] = "New password and re-type password are not same" + ctx.Data["HasError"] = true + ctx.Data["ErrorMsg"] = "New password and re-type password are not same" } else { user.Passwd = newUser.Passwd if err := models.UpdateUser(user); err != nil { ctx.Handle(200, "setting.SettingPassword", err) return } - data["IsSuccess"] = true + ctx.Data["IsSuccess"] = true } - data["Owner"] = user - r.HTML(200, "user/password", data) + ctx.Data["Owner"] = user + ctx.Render.HTML(200, "user/password", ctx.Data) } -func SettingSSHKeys(form auth.AddSSHKeyForm, r render.Render, data base.TmplData, req *http.Request, session sessions.Session) { - data["Title"] = "SSH Keys" +func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) { + ctx.Data["Title"] = "SSH Keys" // Delete SSH key. - if req.Method == "DELETE" || req.FormValue("_method") == "DELETE" { - println(1) - id, err := strconv.ParseInt(req.FormValue("id"), 10, 64) + if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" { + id, err := strconv.ParseInt(ctx.Query("id"), 10, 64) if err != nil { - data["ErrorMsg"] = err + ctx.Data["ErrorMsg"] = err log.Error("ssh.DelPublicKey: %v", err) - r.JSON(200, map[string]interface{}{ + ctx.Render.JSON(200, map[string]interface{}{ "ok": false, "err": err.Error(), }) @@ -102,18 +97,18 @@ func SettingSSHKeys(form auth.AddSSHKeyForm, r render.Render, data base.TmplData } k := &models.PublicKey{ Id: id, - OwnerId: auth.SignedInId(session), + OwnerId: ctx.User.Id, } if err = models.DeletePublicKey(k); err != nil { - data["ErrorMsg"] = err + ctx.Data["ErrorMsg"] = err log.Error("ssh.DelPublicKey: %v", err) - r.JSON(200, map[string]interface{}{ + ctx.Render.JSON(200, map[string]interface{}{ "ok": false, "err": err.Error(), }) } else { - r.JSON(200, map[string]interface{}{ + ctx.Render.JSON(200, map[string]interface{}{ "ok": true, }) } @@ -121,51 +116,51 @@ func SettingSSHKeys(form auth.AddSSHKeyForm, r render.Render, data base.TmplData } // Add new SSH key. - if req.Method == "POST" { - if hasErr, ok := data["HasError"]; ok && hasErr.(bool) { - r.HTML(200, "user/publickey", data) + if ctx.Req.Method == "POST" { + if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) { + ctx.Render.HTML(200, "user/publickey", ctx.Data) return } - k := &models.PublicKey{OwnerId: auth.SignedInId(session), + k := &models.PublicKey{OwnerId: ctx.User.Id, Name: form.KeyName, Content: form.KeyContent, } if err := models.AddPublicKey(k); err != nil { - data["ErrorMsg"] = err + ctx.Data["ErrorMsg"] = err log.Error("ssh.AddPublicKey: %v", err) - r.HTML(200, "base/error", data) + ctx.Render.HTML(200, "base/error", ctx.Data) return } else { - data["AddSSHKeySuccess"] = true + ctx.Data["AddSSHKeySuccess"] = true } } // List existed SSH keys. - keys, err := models.ListPublicKey(auth.SignedInId(session)) + keys, err := models.ListPublicKey(ctx.User.Id) if err != nil { - data["ErrorMsg"] = err + ctx.Data["ErrorMsg"] = err log.Error("ssh.ListPublicKey: %v", err) - r.HTML(200, "base/error", data) + ctx.Render.HTML(200, "base/error", ctx.Data) return } - data["PageIsUserSetting"] = true - data["Keys"] = keys - r.HTML(200, "user/publickey", data) + ctx.Data["PageIsUserSetting"] = true + ctx.Data["Keys"] = keys + ctx.Render.HTML(200, "user/publickey", ctx.Data) } -func SettingNotification(r render.Render, data base.TmplData) { +func SettingNotification(ctx *middleware.Context) { // todo user setting notification - data["Title"] = "Notification" - data["PageIsUserSetting"] = true - r.HTML(200, "user/notification", data) + ctx.Data["Title"] = "Notification" + ctx.Data["PageIsUserSetting"] = true + ctx.Render.HTML(200, "user/notification", ctx.Data) } -func SettingSecurity(r render.Render, data base.TmplData) { +func SettingSecurity(ctx *middleware.Context) { // todo user setting security - data["Title"] = "Security" - data["PageIsUserSetting"] = true - r.HTML(200, "user/security", data) + ctx.Data["Title"] = "Security" + ctx.Data["PageIsUserSetting"] = true + ctx.Render.HTML(200, "user/security", ctx.Data) } diff --git a/routers/user/user.go b/routers/user/user.go index 9dbcb2a190..50b9918308 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -6,7 +6,6 @@ package user import ( "fmt" - "net/http" "github.com/codegangsta/martini" "github.com/martini-contrib/render" @@ -67,7 +66,7 @@ func Profile(ctx *middleware.Context, params martini.Params) { ctx.Render.HTML(200, "user/profile", ctx.Data) } -func SignIn(form auth.LogInForm, ctx *middleware.Context, r render.Render, session sessions.Session) { +func SignIn(ctx *middleware.Context, form auth.LogInForm) { ctx.Data["Title"] = "Log In" if ctx.Req.Method == "GET" { @@ -94,9 +93,9 @@ func SignIn(form auth.LogInForm, ctx *middleware.Context, r render.Render, sessi return } - session.Set("userId", user.Id) - session.Set("userName", user.Name) - r.Redirect("/") + ctx.Session.Set("userId", user.Id) + ctx.Session.Set("userName", user.Name) + ctx.Render.Redirect("/") } func SignOut(r render.Render, session sessions.Session) { @@ -105,25 +104,25 @@ func SignOut(r render.Render, session sessions.Session) { r.Redirect("/") } -func SignUp(form auth.RegisterForm, ctx *middleware.Context, data base.TmplData, req *http.Request, r render.Render) { - data["Title"] = "Sign Up" - data["PageIsSignUp"] = true +func SignUp(ctx *middleware.Context, form auth.RegisterForm) { + ctx.Data["Title"] = "Sign Up" + ctx.Data["PageIsSignUp"] = true - if req.Method == "GET" { - r.HTML(200, "user/signup", data) + if ctx.Req.Method == "GET" { + ctx.Render.HTML(200, "user/signup", ctx.Data) return } if form.Password != form.RetypePasswd { - data["HasError"] = true - data["Err_Password"] = true - data["Err_RetypePasswd"] = true - data["ErrorMsg"] = "Password and re-type password are not same" - auth.AssignForm(form, data) + ctx.Data["HasError"] = true + ctx.Data["Err_Password"] = true + ctx.Data["Err_RetypePasswd"] = true + ctx.Data["ErrorMsg"] = "Password and re-type password are not same" + auth.AssignForm(form, ctx.Data) } - if hasErr, ok := data["HasError"]; ok && hasErr.(bool) { - r.HTML(200, "user/signup", data) + if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) { + ctx.Render.HTML(200, "user/signup", ctx.Data) return } @@ -134,49 +133,47 @@ func SignUp(form auth.RegisterForm, ctx *middleware.Context, data base.TmplData, } if err := models.RegisterUser(u); err != nil { - data["HasError"] = true - auth.AssignForm(form, data) + ctx.Data["HasError"] = true + auth.AssignForm(form, ctx.Data) switch err.Error() { case models.ErrUserAlreadyExist.Error(): - data["Err_Username"] = true - data["ErrorMsg"] = "Username has been already taken" - r.HTML(200, "user/signup", data) + ctx.Data["Err_Username"] = true + ctx.Data["ErrorMsg"] = "Username has been already taken" + ctx.Render.HTML(200, "user/signup", ctx.Data) case models.ErrEmailAlreadyUsed.Error(): - data["Err_Email"] = true - data["ErrorMsg"] = "E-mail address has been already used" - r.HTML(200, "user/signup", data) + ctx.Data["Err_Email"] = true + ctx.Data["ErrorMsg"] = "E-mail address has been already used" + ctx.Render.HTML(200, "user/signup", ctx.Data) default: ctx.Handle(200, "user.SignUp", err) } return } - r.Redirect("/user/login") + ctx.Render.Redirect("/user/login") } -func Delete(data base.TmplData, ctx *middleware.Context, req *http.Request, session sessions.Session, r render.Render) { - data["Title"] = "Delete Account" +func Delete(ctx *middleware.Context) { + ctx.Data["Title"] = "Delete Account" - if req.Method == "GET" { - r.HTML(200, "user/delete", data) + if ctx.Req.Method == "GET" { + ctx.Render.HTML(200, "user/delete", ctx.Data) return } - id := auth.SignedInId(session) - u := &models.User{Id: id} - if err := models.DeleteUser(u); err != nil { - data["HasError"] = true + if err := models.DeleteUser(ctx.User); err != nil { + ctx.Data["HasError"] = true switch err.Error() { case models.ErrUserOwnRepos.Error(): - data["ErrorMsg"] = "Your account still have ownership of repository, you have to delete or transfer them first." + ctx.Data["ErrorMsg"] = "Your account still have ownership of repository, you have to delete or transfer them first." default: ctx.Handle(200, "user.Delete", err) return } } - r.HTML(200, "user/delete", data) + ctx.Render.HTML(200, "user/delete", ctx.Data) } const ( @@ -184,10 +181,10 @@ const (
%s
%s
` ) -func Feeds(form auth.FeedsForm, r render.Render) { +func Feeds(ctx *middleware.Context, form auth.FeedsForm) { actions, err := models.GetFeeds(form.UserId, form.Page*20, false) if err != nil { - r.JSON(500, err) + ctx.Render.JSON(500, err) } feeds := make([]string, len(actions)) @@ -195,5 +192,5 @@ func Feeds(form auth.FeedsForm, r render.Render) { feeds[i] = fmt.Sprintf(feedTpl, base.ActionIcon(actions[i].OpType), base.TimeSince(actions[i].Created), base.ActionDesc(actions[i])) } - r.JSON(200, &feeds) + ctx.Render.JSON(200, &feeds) }