clean code

This commit is contained in:
slene 2014-03-15 22:34:33 +08:00
parent ca86433402
commit b9e57e41ce
5 changed files with 117 additions and 130 deletions

View File

@ -60,6 +60,7 @@ func InitContext() martini.Handler {
// p: p, // p: p,
Req: r, Req: r,
Res: res, Res: res,
Session: session,
Data: data, Data: data,
Render: rd, Render: rd,
} }

View File

@ -5,20 +5,15 @@
package routers package routers
import ( 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/modules/middleware"
"github.com/gogits/gogs/routers/user" "github.com/gogits/gogs/routers/user"
) )
func Home(ctx *middleware.Context, r render.Render, data base.TmplData, session sessions.Session) { func Home(ctx *middleware.Context) {
if auth.IsSignedIn(session) { if ctx.IsSigned {
user.Dashboard(ctx) user.Dashboard(ctx)
return return
} }
data["PageIsHome"] = true ctx.Data["PageIsHome"] = true
r.HTML(200, "home", data) ctx.Render.HTML(200, "home", ctx.Data)
} }

View File

@ -1,17 +1,16 @@
package repo package repo
import ( import (
"github.com/codegangsta/martini"
"github.com/martini-contrib/render"
"strings" "strings"
"github.com/codegangsta/martini"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/middleware"
) )
func Single(params martini.Params, ctx *middleware.Context, r render.Render, data base.TmplData) { func Single(ctx *middleware.Context, params martini.Params) {
if !data["IsRepositoryValid"].(bool) { if !ctx.Data["IsRepositoryValid"].(bool) {
return return
} }
if params["branchname"] == "" { 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) ctx.Handle(200, "repo.Single", err)
return return
} }
data["Username"] = params["username"] ctx.Data["Username"] = params["username"]
data["Reponame"] = params["reponame"] ctx.Data["Reponame"] = params["reponame"]
data["Branchname"] = params["branchname"] ctx.Data["Branchname"] = params["branchname"]
var treenames []string var treenames []string
Paths := make([]string, 0) Paths := make([]string, 0)
@ -38,21 +37,21 @@ func Single(params martini.Params, ctx *middleware.Context, r render.Render, dat
} }
} }
data["Paths"] = Paths ctx.Data["Paths"] = Paths
data["Treenames"] = treenames ctx.Data["Treenames"] = treenames
data["IsRepoToolbarSource"] = true ctx.Data["IsRepoToolbarSource"] = true
data["Files"] = files 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) { func Setting(ctx *middleware.Context) {
if !data["IsRepositoryValid"].(bool) { if !ctx.Data["IsRepositoryValid"].(bool) {
return return
} }
data["Title"] = data["Title"].(string) + " - settings" ctx.Data["Title"] = ctx.Data["Title"].(string) + " - settings"
data["IsRepoToolbarSetting"] = true ctx.Data["IsRepoToolbarSetting"] = true
r.HTML(200, "repo/setting", data) ctx.Render.HTML(200, "repo/setting", ctx.Data)
} }

View File

@ -5,12 +5,8 @@
package user package user
import ( import (
"net/http"
"strconv" "strconv"
"github.com/martini-contrib/render"
"github.com/martini-contrib/sessions"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/base"
@ -18,20 +14,20 @@ import (
"github.com/gogits/gogs/modules/middleware" "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) { func Setting(ctx *middleware.Context, form auth.UpdateProfileForm) {
data["Title"] = "Setting" ctx.Data["Title"] = "Setting"
data["PageIsUserSetting"] = true ctx.Data["PageIsUserSetting"] = true
user := auth.SignedInUser(session) user := ctx.User
data["Owner"] = user ctx.Data["Owner"] = user
if req.Method == "GET" { if ctx.Req.Method == "GET" {
r.HTML(200, "user/setting", data) ctx.Render.HTML(200, "user/setting", ctx.Data)
return return
} }
if hasErr, ok := data["HasError"]; ok && hasErr.(bool) { if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
r.HTML(200, "user/setting", data) ctx.Render.HTML(200, "user/setting", ctx.Data)
return return
} }
@ -45,20 +41,20 @@ func Setting(form auth.UpdateProfileForm, ctx *middleware.Context, r render.Rend
return return
} }
data["IsSuccess"] = true ctx.Data["IsSuccess"] = true
r.HTML(200, "user/setting", data) 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) { func SettingPassword(ctx *middleware.Context, form auth.UpdatePasswdForm) {
data["Title"] = "Password" ctx.Data["Title"] = "Password"
data["PageIsUserSetting"] = true ctx.Data["PageIsUserSetting"] = true
if req.Method == "GET" { if ctx.Req.Method == "GET" {
r.HTML(200, "user/password", data) ctx.Render.HTML(200, "user/password", ctx.Data)
return return
} }
user := auth.SignedInUser(session) user := ctx.User
newUser := &models.User{Passwd: form.NewPasswd} newUser := &models.User{Passwd: form.NewPasswd}
if err := newUser.EncodePasswd(); err != nil { if err := newUser.EncodePasswd(); err != nil {
ctx.Handle(200, "setting.SettingPassword", err) 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 { if user.Passwd != newUser.Passwd {
data["HasError"] = true ctx.Data["HasError"] = true
data["ErrorMsg"] = "Old password is not correct" ctx.Data["ErrorMsg"] = "Old password is not correct"
} else if form.NewPasswd != form.RetypePasswd { } else if form.NewPasswd != form.RetypePasswd {
data["HasError"] = true ctx.Data["HasError"] = true
data["ErrorMsg"] = "New password and re-type password are not same" ctx.Data["ErrorMsg"] = "New password and re-type password are not same"
} else { } else {
user.Passwd = newUser.Passwd user.Passwd = newUser.Passwd
if err := models.UpdateUser(user); err != nil { if err := models.UpdateUser(user); err != nil {
ctx.Handle(200, "setting.SettingPassword", err) ctx.Handle(200, "setting.SettingPassword", err)
return return
} }
data["IsSuccess"] = true ctx.Data["IsSuccess"] = true
} }
data["Owner"] = user ctx.Data["Owner"] = user
r.HTML(200, "user/password", data) 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) { func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
data["Title"] = "SSH Keys" ctx.Data["Title"] = "SSH Keys"
// Delete SSH key. // Delete SSH key.
if req.Method == "DELETE" || req.FormValue("_method") == "DELETE" { if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" {
println(1) id, err := strconv.ParseInt(ctx.Query("id"), 10, 64)
id, err := strconv.ParseInt(req.FormValue("id"), 10, 64)
if err != nil { if err != nil {
data["ErrorMsg"] = err ctx.Data["ErrorMsg"] = err
log.Error("ssh.DelPublicKey: %v", err) log.Error("ssh.DelPublicKey: %v", err)
r.JSON(200, map[string]interface{}{ ctx.Render.JSON(200, map[string]interface{}{
"ok": false, "ok": false,
"err": err.Error(), "err": err.Error(),
}) })
@ -102,18 +97,18 @@ func SettingSSHKeys(form auth.AddSSHKeyForm, r render.Render, data base.TmplData
} }
k := &models.PublicKey{ k := &models.PublicKey{
Id: id, Id: id,
OwnerId: auth.SignedInId(session), OwnerId: ctx.User.Id,
} }
if err = models.DeletePublicKey(k); err != nil { if err = models.DeletePublicKey(k); err != nil {
data["ErrorMsg"] = err ctx.Data["ErrorMsg"] = err
log.Error("ssh.DelPublicKey: %v", err) log.Error("ssh.DelPublicKey: %v", err)
r.JSON(200, map[string]interface{}{ ctx.Render.JSON(200, map[string]interface{}{
"ok": false, "ok": false,
"err": err.Error(), "err": err.Error(),
}) })
} else { } else {
r.JSON(200, map[string]interface{}{ ctx.Render.JSON(200, map[string]interface{}{
"ok": true, "ok": true,
}) })
} }
@ -121,51 +116,51 @@ func SettingSSHKeys(form auth.AddSSHKeyForm, r render.Render, data base.TmplData
} }
// Add new SSH key. // Add new SSH key.
if req.Method == "POST" { if ctx.Req.Method == "POST" {
if hasErr, ok := data["HasError"]; ok && hasErr.(bool) { if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
r.HTML(200, "user/publickey", data) ctx.Render.HTML(200, "user/publickey", ctx.Data)
return return
} }
k := &models.PublicKey{OwnerId: auth.SignedInId(session), k := &models.PublicKey{OwnerId: ctx.User.Id,
Name: form.KeyName, Name: form.KeyName,
Content: form.KeyContent, Content: form.KeyContent,
} }
if err := models.AddPublicKey(k); err != nil { if err := models.AddPublicKey(k); err != nil {
data["ErrorMsg"] = err ctx.Data["ErrorMsg"] = err
log.Error("ssh.AddPublicKey: %v", err) log.Error("ssh.AddPublicKey: %v", err)
r.HTML(200, "base/error", data) ctx.Render.HTML(200, "base/error", ctx.Data)
return return
} else { } else {
data["AddSSHKeySuccess"] = true ctx.Data["AddSSHKeySuccess"] = true
} }
} }
// List existed SSH keys. // List existed SSH keys.
keys, err := models.ListPublicKey(auth.SignedInId(session)) keys, err := models.ListPublicKey(ctx.User.Id)
if err != nil { if err != nil {
data["ErrorMsg"] = err ctx.Data["ErrorMsg"] = err
log.Error("ssh.ListPublicKey: %v", err) log.Error("ssh.ListPublicKey: %v", err)
r.HTML(200, "base/error", data) ctx.Render.HTML(200, "base/error", ctx.Data)
return return
} }
data["PageIsUserSetting"] = true ctx.Data["PageIsUserSetting"] = true
data["Keys"] = keys ctx.Data["Keys"] = keys
r.HTML(200, "user/publickey", data) 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 // todo user setting notification
data["Title"] = "Notification" ctx.Data["Title"] = "Notification"
data["PageIsUserSetting"] = true ctx.Data["PageIsUserSetting"] = true
r.HTML(200, "user/notification", data) 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 // todo user setting security
data["Title"] = "Security" ctx.Data["Title"] = "Security"
data["PageIsUserSetting"] = true ctx.Data["PageIsUserSetting"] = true
r.HTML(200, "user/security", data) ctx.Render.HTML(200, "user/security", ctx.Data)
} }

View File

@ -6,7 +6,6 @@ package user
import ( import (
"fmt" "fmt"
"net/http"
"github.com/codegangsta/martini" "github.com/codegangsta/martini"
"github.com/martini-contrib/render" "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) 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" ctx.Data["Title"] = "Log In"
if ctx.Req.Method == "GET" { if ctx.Req.Method == "GET" {
@ -94,9 +93,9 @@ func SignIn(form auth.LogInForm, ctx *middleware.Context, r render.Render, sessi
return return
} }
session.Set("userId", user.Id) ctx.Session.Set("userId", user.Id)
session.Set("userName", user.Name) ctx.Session.Set("userName", user.Name)
r.Redirect("/") ctx.Render.Redirect("/")
} }
func SignOut(r render.Render, session sessions.Session) { func SignOut(r render.Render, session sessions.Session) {
@ -105,25 +104,25 @@ func SignOut(r render.Render, session sessions.Session) {
r.Redirect("/") r.Redirect("/")
} }
func SignUp(form auth.RegisterForm, ctx *middleware.Context, data base.TmplData, req *http.Request, r render.Render) { func SignUp(ctx *middleware.Context, form auth.RegisterForm) {
data["Title"] = "Sign Up" ctx.Data["Title"] = "Sign Up"
data["PageIsSignUp"] = true ctx.Data["PageIsSignUp"] = true
if req.Method == "GET" { if ctx.Req.Method == "GET" {
r.HTML(200, "user/signup", data) ctx.Render.HTML(200, "user/signup", ctx.Data)
return return
} }
if form.Password != form.RetypePasswd { if form.Password != form.RetypePasswd {
data["HasError"] = true ctx.Data["HasError"] = true
data["Err_Password"] = true ctx.Data["Err_Password"] = true
data["Err_RetypePasswd"] = true ctx.Data["Err_RetypePasswd"] = true
data["ErrorMsg"] = "Password and re-type password are not same" ctx.Data["ErrorMsg"] = "Password and re-type password are not same"
auth.AssignForm(form, data) auth.AssignForm(form, ctx.Data)
} }
if hasErr, ok := data["HasError"]; ok && hasErr.(bool) { if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
r.HTML(200, "user/signup", data) ctx.Render.HTML(200, "user/signup", ctx.Data)
return return
} }
@ -134,49 +133,47 @@ func SignUp(form auth.RegisterForm, ctx *middleware.Context, data base.TmplData,
} }
if err := models.RegisterUser(u); err != nil { if err := models.RegisterUser(u); err != nil {
data["HasError"] = true ctx.Data["HasError"] = true
auth.AssignForm(form, data) auth.AssignForm(form, ctx.Data)
switch err.Error() { switch err.Error() {
case models.ErrUserAlreadyExist.Error(): case models.ErrUserAlreadyExist.Error():
data["Err_Username"] = true ctx.Data["Err_Username"] = true
data["ErrorMsg"] = "Username has been already taken" ctx.Data["ErrorMsg"] = "Username has been already taken"
r.HTML(200, "user/signup", data) ctx.Render.HTML(200, "user/signup", ctx.Data)
case models.ErrEmailAlreadyUsed.Error(): case models.ErrEmailAlreadyUsed.Error():
data["Err_Email"] = true ctx.Data["Err_Email"] = true
data["ErrorMsg"] = "E-mail address has been already used" ctx.Data["ErrorMsg"] = "E-mail address has been already used"
r.HTML(200, "user/signup", data) ctx.Render.HTML(200, "user/signup", ctx.Data)
default: default:
ctx.Handle(200, "user.SignUp", err) ctx.Handle(200, "user.SignUp", err)
} }
return 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) { func Delete(ctx *middleware.Context) {
data["Title"] = "Delete Account" ctx.Data["Title"] = "Delete Account"
if req.Method == "GET" { if ctx.Req.Method == "GET" {
r.HTML(200, "user/delete", data) ctx.Render.HTML(200, "user/delete", ctx.Data)
return return
} }
id := auth.SignedInId(session) if err := models.DeleteUser(ctx.User); err != nil {
u := &models.User{Id: id} ctx.Data["HasError"] = true
if err := models.DeleteUser(u); err != nil {
data["HasError"] = true
switch err.Error() { switch err.Error() {
case models.ErrUserOwnRepos.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: default:
ctx.Handle(200, "user.Delete", err) ctx.Handle(200, "user.Delete", err)
return return
} }
} }
r.HTML(200, "user/delete", data) ctx.Render.HTML(200, "user/delete", ctx.Data)
} }
const ( const (
@ -184,10 +181,10 @@ const (
<div class="info"><span class="meta">%s</span><br>%s</div>` <div class="info"><span class="meta">%s</span><br>%s</div>`
) )
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) actions, err := models.GetFeeds(form.UserId, form.Page*20, false)
if err != nil { if err != nil {
r.JSON(500, err) ctx.Render.JSON(500, err)
} }
feeds := make([]string, len(actions)) 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), feeds[i] = fmt.Sprintf(feedTpl, base.ActionIcon(actions[i].OpType),
base.TimeSince(actions[i].Created), base.ActionDesc(actions[i])) base.TimeSince(actions[i].Created), base.ActionDesc(actions[i]))
} }
r.JSON(200, &feeds) ctx.Render.JSON(200, &feeds)
} }