Code convention

This commit is contained in:
Unknown 2014-05-24 15:28:31 -04:00
parent e33a104448
commit 80055bde86
11 changed files with 63 additions and 90 deletions

View File

@ -80,4 +80,4 @@ The [core team](http://gogs.io/team) of this project. See [contributors page](ht
## License ## License
Gogs is under the MIT License. See the [LICENSE](https://github.com/gogits/gogs/blob/master/LICENSE) file for the full license text. This project is under the MIT License. See the [LICENSE](https://github.com/gogits/gogs/blob/master/LICENSE) file for the full license text.

View File

@ -72,4 +72,4 @@ Gogs 完全使用 Go 语言来实现对 Git 数据的操作,实现 **零** 依
## 授权许可 ## 授权许可
Gogs 采用 MIT 开源授权许可证,完整的授权说明已放置在 [LICENSE](https://github.com/gogits/gogs/blob/master/LICENSE) 文件中。 本项目采用 MIT 开源授权许可证,完整的授权说明已放置在 [LICENSE](https://github.com/gogits/gogs/blob/master/LICENSE) 文件中。

View File

@ -752,14 +752,14 @@ func GetRepositoryById(id int64) (*Repository, error) {
} }
// GetRepositories returns a list of repositories of given user. // GetRepositories returns a list of repositories of given user.
func GetRepositories(user *User, private bool) ([]*Repository, error) { func GetRepositories(uid int64, private bool) ([]*Repository, error) {
repos := make([]*Repository, 0, 10) repos := make([]*Repository, 0, 10)
sess := orm.Desc("updated") sess := orm.Desc("updated")
if !private { if !private {
sess.Where("is_private=?", false) sess.Where("is_private=?", false)
} }
err := sess.Find(&repos, &Repository{OwnerId: user.Id}) err := sess.Find(&repos, &Repository{OwnerId: uid})
return repos, err return repos, err
} }

View File

@ -237,7 +237,7 @@ func ChangeUserName(user *User, newUserName string) (err error) {
} }
} }
repos, err := GetRepositories(user, true) repos, err := GetRepositories(user.Id, true)
if err != nil { if err != nil {
return err return err
} }

View File

@ -24,6 +24,8 @@ func Home(ctx *middleware.Context) {
return return
} }
ctx.Data["PageIsHome"] = true
// Show recent updated repositoires for new visiters. // Show recent updated repositoires for new visiters.
repos, err := models.GetRecentUpdatedRepositories() repos, err := models.GetRecentUpdatedRepositories()
if err != nil { if err != nil {
@ -38,12 +40,11 @@ func Home(ctx *middleware.Context) {
} }
} }
ctx.Data["Repos"] = repos ctx.Data["Repos"] = repos
ctx.Data["PageIsHome"] = true
ctx.HTML(200, "home") ctx.HTML(200, "home")
} }
func NotFound(ctx *middleware.Context) { func NotFound(ctx *middleware.Context) {
ctx.Data["PageIsNotFound"] = true
ctx.Data["Title"] = "Page Not Found" ctx.Data["Title"] = "Page Not Found"
ctx.Data["PageIsNotFound"] = true
ctx.Handle(404, "home.NotFound", nil) ctx.Handle(404, "home.NotFound", nil)
} }

View File

@ -20,26 +20,27 @@ import (
func Dashboard(ctx *middleware.Context) { func Dashboard(ctx *middleware.Context) {
ctx.Data["Title"] = "Dashboard" ctx.Data["Title"] = "Dashboard"
ctx.Data["PageIsUserDashboard"] = true ctx.Data["PageIsUserDashboard"] = true
repos, err := models.GetRepositories(&models.User{Id: ctx.User.Id}, true)
var err error
ctx.Data["MyRepos"], err = models.GetRepositories(ctx.User.Id, true)
if err != nil { if err != nil {
ctx.Handle(500, "home.Dashboard(GetRepositories)", err) ctx.Handle(500, "home.Dashboard(GetRepositories)", err)
return return
} }
ctx.Data["MyRepos"] = repos
collaRepos, err := models.GetCollaborativeRepos(ctx.User.Name) ctx.Data["CollaborativeRepos"], err = models.GetCollaborativeRepos(ctx.User.Name)
if err != nil { if err != nil {
ctx.Handle(500, "home.Dashboard(GetCollaborativeRepos)", err) ctx.Handle(500, "home.Dashboard(GetCollaborativeRepos)", err)
return return
} }
ctx.Data["CollaborativeRepos"] = collaRepos
actions, err := models.GetFeeds(ctx.User.Id, 0, false) actions, err := models.GetFeeds(ctx.User.Id, 0, false)
if err != nil { if err != nil {
ctx.Handle(500, "home.Dashboard", err) ctx.Handle(500, "home.Dashboard(GetFeeds)", err)
return return
} }
// Check access of private repositories.
feeds := make([]*models.Action, 0, len(actions)) feeds := make([]*models.Action, 0, len(actions))
for _, act := range actions { for _, act := range actions {
if act.IsPrivate { if act.IsPrivate {
@ -50,47 +51,42 @@ func Dashboard(ctx *middleware.Context) {
} }
feeds = append(feeds, act) feeds = append(feeds, act)
} }
ctx.Data["Feeds"] = feeds ctx.Data["Feeds"] = feeds
ctx.HTML(200, "user/dashboard") ctx.HTML(200, "user/dashboard")
} }
func Profile(ctx *middleware.Context, params martini.Params) { func Profile(ctx *middleware.Context, params martini.Params) {
ctx.Data["Title"] = "Profile" ctx.Data["Title"] = "Profile"
ctx.Data["PageIsUserProfile"] = true
user, err := models.GetUserByName(params["username"]) user, err := models.GetUserByName(params["username"])
if err != nil { if err != nil {
if err == models.ErrUserNotExist { if err == models.ErrUserNotExist {
ctx.Handle(404, "user.Profile", err) ctx.Handle(404, "user.Profile(GetUserByName)", err)
} else { } else {
ctx.Handle(500, "user.Profile", err) ctx.Handle(500, "user.Profile(GetUserByName)", err)
} }
return return
} }
ctx.Data["Owner"] = user ctx.Data["Owner"] = user
tab := ctx.Query("tab") tab := ctx.Query("tab")
ctx.Data["TabName"] = tab ctx.Data["TabName"] = tab
switch tab { switch tab {
case "activity": case "activity":
feeds, err := models.GetFeeds(user.Id, 0, true) ctx.Data["Feeds"], err = models.GetFeeds(user.Id, 0, true)
if err != nil { if err != nil {
ctx.Handle(500, "user.Profile", err) ctx.Handle(500, "user.Profile(GetFeeds)", err)
return return
} }
ctx.Data["Feeds"] = feeds
default: default:
repos, err := models.GetRepositories(user, ctx.IsSigned && ctx.User.Id == user.Id) ctx.Data["Repos"], err = models.GetRepositories(user.Id, ctx.IsSigned && ctx.User.Id == user.Id)
if err != nil { if err != nil {
ctx.Handle(500, "user.Profile", err) ctx.Handle(500, "user.Profile(GetRepositories)", err)
return return
} }
ctx.Data["Repos"] = repos
} }
ctx.Data["PageIsUserProfile"] = true
ctx.HTML(200, "user/profile") ctx.HTML(200, "user/profile")
} }
@ -98,13 +94,12 @@ func Email2User(ctx *middleware.Context) {
u, err := models.GetUserByEmail(ctx.Query("email")) u, err := models.GetUserByEmail(ctx.Query("email"))
if err != nil { if err != nil {
if err == models.ErrUserNotExist { if err == models.ErrUserNotExist {
ctx.Handle(404, "user.Email2User", err) ctx.Handle(404, "user.Email2User(GetUserByEmail)", err)
} else { } else {
ctx.Handle(500, "user.Email2User(GetUserByEmail)", err) ctx.Handle(500, "user.Email2User(GetUserByEmail)", err)
} }
return return
} }
ctx.Redirect("/user/" + u.Name) ctx.Redirect("/user/" + u.Name)
} }
@ -145,36 +140,32 @@ func Issues(ctx *middleware.Context) {
isShowClosed := ctx.Query("state") == "closed" isShowClosed := ctx.Query("state") == "closed"
var assigneeId, posterId int64
var filterMode int var filterMode int
switch viewType { switch viewType {
case "assigned": case "assigned":
assigneeId = ctx.User.Id
filterMode = models.FM_ASSIGN filterMode = models.FM_ASSIGN
case "created_by": case "created_by":
posterId = ctx.User.Id
filterMode = models.FM_CREATE filterMode = models.FM_CREATE
} }
_, _ = assigneeId, posterId
rid, _ := base.StrTo(ctx.Query("repoid")).Int64() repoId, _ := base.StrTo(ctx.Query("repoid")).Int64()
issueStats := models.GetUserIssueStats(ctx.User.Id, filterMode) issueStats := models.GetUserIssueStats(ctx.User.Id, filterMode)
// Get all repositories. // Get all repositories.
repos, err := models.GetRepositories(ctx.User, true) repos, err := models.GetRepositories(ctx.User.Id, true)
if err != nil { if err != nil {
ctx.Handle(500, "user.Issues(GetRepositories)", err) ctx.Handle(500, "user.Issues(GetRepositories)", err)
return return
} }
rids := make([]int64, 0, len(repos)) repoIds := make([]int64, 0, len(repos))
showRepos := make([]*models.Repository, 0, len(repos)) showRepos := make([]*models.Repository, 0, len(repos))
for _, repo := range repos { for _, repo := range repos {
if repo.NumIssues == 0 { if repo.NumIssues == 0 {
continue continue
} }
rids = append(rids, repo.Id) repoIds = append(repoIds, repo.Id)
repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
issueStats.AllCount += int64(repo.NumOpenIssues) issueStats.AllCount += int64(repo.NumOpenIssues)
@ -195,8 +186,8 @@ func Issues(ctx *middleware.Context) {
} }
} }
if rid > 0 { if repoId > 0 {
rids = []int64{rid} repoIds = []int64{repoId}
} }
page, _ := base.StrTo(ctx.Query("page")).Int() page, _ := base.StrTo(ctx.Query("page")).Int()
@ -207,9 +198,9 @@ func Issues(ctx *middleware.Context) {
case "assigned": case "assigned":
fallthrough fallthrough
case "created_by": case "created_by":
ius, err = models.GetIssueUserPairsByMode(ctx.User.Id, rid, isShowClosed, page, filterMode) ius, err = models.GetIssueUserPairsByMode(ctx.User.Id, repoId, isShowClosed, page, filterMode)
default: default:
ius, err = models.GetIssueUserPairsByRepoIds(rids, isShowClosed, page) ius, err = models.GetIssueUserPairsByRepoIds(repoIds, isShowClosed, page)
} }
if err != nil { if err != nil {
ctx.Handle(500, "user.Issues(GetAllIssueUserPairs)", err) ctx.Handle(500, "user.Issues(GetAllIssueUserPairs)", err)
@ -251,7 +242,7 @@ func Issues(ctx *middleware.Context) {
} }
} }
ctx.Data["RepoId"] = rid ctx.Data["RepoId"] = repoId
ctx.Data["Repos"] = showRepos ctx.Data["Repos"] = showRepos
ctx.Data["Issues"] = issues ctx.Data["Issues"] = issues
ctx.Data["ViewType"] = viewType ctx.Data["ViewType"] = viewType
@ -263,7 +254,7 @@ func Issues(ctx *middleware.Context) {
} else { } else {
ctx.Data["ShowCount"] = issueStats.OpenCount ctx.Data["ShowCount"] = issueStats.OpenCount
} }
ctx.HTML(200, "issue/user") ctx.HTML(200, "user/issue")
} }
func Pulls(ctx *middleware.Context) { func Pulls(ctx *middleware.Context) {

View File

@ -27,16 +27,15 @@ func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
ctx.Data["PageIsUserSetting"] = true ctx.Data["PageIsUserSetting"] = true
ctx.Data["IsUserPageSetting"] = true ctx.Data["IsUserPageSetting"] = true
user := ctx.User
ctx.Data["Owner"] = user
if ctx.HasError() { if ctx.HasError() {
ctx.HTML(200, "user/setting") ctx.HTML(200, "user/setting")
return return
} }
ctx.Data["Owner"] = ctx.User
// Check if user name has been changed. // Check if user name has been changed.
if user.Name != form.UserName { if ctx.User.Name != form.UserName {
isExist, err := models.IsUserExist(form.UserName) isExist, err := models.IsUserExist(form.UserName)
if err != nil { if err != nil {
ctx.Handle(500, "user.Setting(update: check existence)", err) ctx.Handle(500, "user.Setting(update: check existence)", err)
@ -44,27 +43,26 @@ func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
} else if isExist { } else if isExist {
ctx.RenderWithErr("User name has been taken.", "user/setting", &form) ctx.RenderWithErr("User name has been taken.", "user/setting", &form)
return return
} else if err = models.ChangeUserName(user, form.UserName); err != nil { } else if err = models.ChangeUserName(ctx.User, form.UserName); err != nil {
ctx.Handle(500, "user.Setting(change user name)", err) ctx.Handle(500, "user.Setting(change user name)", err)
return return
} }
log.Trace("%s User name changed: %s -> %s", ctx.Req.RequestURI, user.Name, form.UserName) log.Trace("%s User name changed: %s -> %s", ctx.Req.RequestURI, ctx.User.Name, form.UserName)
user.Name = form.UserName ctx.User.Name = form.UserName
} }
user.FullName = form.FullName ctx.User.FullName = form.FullName
user.Email = form.Email ctx.User.Email = form.Email
user.Website = form.Website ctx.User.Website = form.Website
user.Location = form.Location ctx.User.Location = form.Location
user.Avatar = base.EncodeMd5(form.Avatar) ctx.User.Avatar = base.EncodeMd5(form.Avatar)
user.AvatarEmail = form.Avatar ctx.User.AvatarEmail = form.Avatar
if err := models.UpdateUser(user); err != nil { if err := models.UpdateUser(ctx.User); err != nil {
ctx.Handle(500, "setting.Setting", err) ctx.Handle(500, "setting.Setting", err)
return return
} }
log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName) log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
ctx.Flash.Success("Your profile has been successfully updated.") ctx.Flash.Success("Your profile has been successfully updated.")
ctx.Redirect("/user/settings") ctx.Redirect("/user/settings")
} }
@ -86,13 +84,12 @@ func SettingSocial(ctx *middleware.Context) {
return return
} }
socials, err := models.GetOauthByUserId(ctx.User.Id) var err error
ctx.Data["Socials"], err = models.GetOauthByUserId(ctx.User.Id)
if err != nil { if err != nil {
ctx.Handle(500, "user.SettingSocial(GetOauthByUserId)", err) ctx.Handle(500, "user.SettingSocial(GetOauthByUserId)", err)
return return
} }
ctx.Data["Socials"] = socials
ctx.HTML(200, "user/social") ctx.HTML(200, "user/social")
} }
@ -113,28 +110,26 @@ func SettingPasswordPost(ctx *middleware.Context, form auth.UpdatePasswdForm) {
return return
} }
user := ctx.User
tmpUser := &models.User{ tmpUser := &models.User{
Passwd: form.OldPasswd, Passwd: form.OldPasswd,
Salt: user.Salt, Salt: ctx.User.Salt,
} }
tmpUser.EncodePasswd() tmpUser.EncodePasswd()
if user.Passwd != tmpUser.Passwd { if ctx.User.Passwd != tmpUser.Passwd {
ctx.Flash.Error("Old password is not correct.") ctx.Flash.Error("Old password is not correct.")
} else if form.NewPasswd != form.RetypePasswd { } else if form.NewPasswd != form.RetypePasswd {
ctx.Flash.Error("New password and re-type password are not same.") ctx.Flash.Error("New password and re-type password are not same.")
} else { } else {
user.Passwd = form.NewPasswd ctx.User.Passwd = form.NewPasswd
user.Salt = models.GetUserSalt() ctx.User.Salt = models.GetUserSalt()
user.EncodePasswd() ctx.User.EncodePasswd()
if err := models.UpdateUser(user); err != nil { if err := models.UpdateUser(ctx.User); err != nil {
ctx.Handle(200, "setting.SettingPassword", err) ctx.Handle(200, "setting.SettingPassword", err)
return return
} }
log.Trace("%s User password updated: %s", ctx.Req.RequestURI, ctx.User.LowerName) log.Trace("%s User password updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
ctx.Flash.Success("Password is changed successfully. You can now sign in via new password.") ctx.Flash.Success("Password is changed successfully. You can now sign in via new password.")
} }
ctx.Redirect("/user/settings/password") ctx.Redirect("/user/settings/password")
} }
@ -170,13 +165,13 @@ func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
return return
} }
var err error
// List existed SSH keys. // List existed SSH keys.
keys, err := models.ListPublicKey(ctx.User.Id) ctx.Data["Keys"], err = models.ListPublicKey(ctx.User.Id)
if err != nil { if err != nil {
ctx.Handle(500, "ssh.ListPublicKey", err) ctx.Handle(500, "ssh.ListPublicKey", err)
return return
} }
ctx.Data["Keys"] = keys
// Add new SSH key. // Add new SSH key.
if ctx.Req.Method == "POST" { if ctx.Req.Method == "POST" {

View File

@ -25,11 +25,6 @@ func SignIn(ctx *middleware.Context) {
return return
} }
if base.OauthService != nil {
ctx.Data["OauthEnabled"] = true
ctx.Data["OauthService"] = base.OauthService
}
// Check auto-login. // Check auto-login.
userName := ctx.GetCookie(base.CookieUserName) userName := ctx.GetCookie(base.CookieUserName)
if len(userName) == 0 { if len(userName) == 0 {
@ -37,6 +32,11 @@ func SignIn(ctx *middleware.Context) {
return return
} }
if base.OauthService != nil {
ctx.Data["OauthEnabled"] = true
ctx.Data["OauthService"] = base.OauthService
}
isSucceed := false isSucceed := false
defer func() { defer func() {
if !isSucceed { if !isSucceed {
@ -85,24 +85,11 @@ func SignInPost(ctx *middleware.Context, form auth.LogInForm) {
} }
if ctx.HasError() { if ctx.HasError() {
println("shit")
ctx.HTML(200, "user/signin") ctx.HTML(200, "user/signin")
return return
} }
var user *models.User user, err := models.LoginUser(form.UserName, form.Password)
var err error
/*if base.Service.LdapAuth {
user, err = models.LoginUserLdap(form.UserName, form.Password)
if err != nil {
log.Error("Fail to login through LDAP: %v", err)
}
}
// try local if not LDAP or it's failed
if !base.Service.LdapAuth || err != nil {
user, err = models.LoginUserPlain(form.UserName, form.Password)
}*/
user, err = models.LoginUser(form.UserName, form.Password)
if err != nil { if err != nil {
if err == models.ErrUserNotExist { if err == models.ErrUserNotExist {
log.Trace("%s Log in failed: %s", ctx.Req.RequestURI, form.UserName) log.Trace("%s Log in failed: %s", ctx.Req.RequestURI, form.UserName)
@ -192,7 +179,6 @@ func oauthSignUp(ctx *middleware.Context, sid int64) {
ctx.Data["username"] = strings.Replace(ctx.Session.Get("socialName").(string), " ", "", -1) ctx.Data["username"] = strings.Replace(ctx.Session.Get("socialName").(string), " ", "", -1)
ctx.Data["email"] = ctx.Session.Get("socialEmail") ctx.Data["email"] = ctx.Session.Get("socialEmail")
log.Trace("user.oauthSignUp(social ID): %v", ctx.Session.Get("socialId")) log.Trace("user.oauthSignUp(social ID): %v", ctx.Session.Get("socialId"))
ctx.HTML(200, "user/signup") ctx.HTML(200, "user/signup")
} }

View File

@ -2,7 +2,7 @@
{{template "base/navbar" .}} {{template "base/navbar" .}}
<div id="body" class="container" data-page="user"> <div id="body" class="container" data-page="user">
{{template "user/setting_nav" .}} {{template "user/setting_nav" .}}
<div id="repo-setting-container" class="col-md-10"> <div id="user-setting-container" class="col-md-10">
{{template "base/alert" .}} {{template "base/alert" .}}
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"> <div class="panel-heading">

View File

@ -2,7 +2,7 @@
{{template "base/navbar" .}} {{template "base/navbar" .}}
<div id="body" class="container" data-page="user"> <div id="body" class="container" data-page="user">
{{template "user/setting_nav" .}} {{template "user/setting_nav" .}}
<div id="repo-setting-container" class="col-md-10"> <div id="user-setting-container" class="col-md-10">
{{template "base/alert" .}} {{template "base/alert" .}}
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"> <div class="panel-heading">