From 7ca830677525e6fc9127660fde32c46bcf00ab59 Mon Sep 17 00:00:00 2001 From: slene Date: Sun, 16 Mar 2014 00:03:23 +0800 Subject: [PATCH 1/6] modify RepoAssignment --- modules/auth/repo.go | 62 ---------------------------- modules/middleware/context.go | 6 +++ modules/middleware/repo.go | 76 +++++++++++++++++++++++++++++++++++ routers/repo/single.go | 13 ++++-- web.go | 8 ++-- 5 files changed, 96 insertions(+), 69 deletions(-) create mode 100644 modules/middleware/repo.go diff --git a/modules/auth/repo.go b/modules/auth/repo.go index 5fe091d62f..ac1b6b699b 100644 --- a/modules/auth/repo.go +++ b/modules/auth/repo.go @@ -12,11 +12,8 @@ import ( "github.com/gogits/binding" - "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" - "github.com/martini-contrib/render" - "github.com/martini-contrib/sessions" ) type CreateRepoForm struct { @@ -61,62 +58,3 @@ type DeleteRepoForm struct { UserName string `form:"userName" binding:"Required"` RepoId int64 `form:"repoId" binding:"Required"` } - -func RepoAssignment(redirect bool) martini.Handler { - return func(params martini.Params, r render.Render, data base.TmplData, session sessions.Session) { - // assign false first - data["IsRepositoryValid"] = false - - var ( - user *models.User - err error - ) - // get repository owner - isOwner := (data["SignedUserName"] == params["username"]) - if !isOwner { - user, err = models.GetUserByName(params["username"]) - if err != nil { - if redirect { - r.Redirect("/") - return - } - //data["ErrorMsg"] = err - //log.Error("repo.Single: %v", err) - //r.HTML(200, "base/error", data) - return - } - } else { - user = SignedInUser(session) - } - if user == nil { - if redirect { - r.Redirect("/") - return - } - //data["ErrorMsg"] = "invliad user account for single repository" - //log.Error("repo.Single: %v", err) - //r.HTML(200, "base/error", data) - return - } - data["IsRepositoryOwner"] = isOwner - - // get repository - repo, err := models.GetRepositoryByName(user, params["reponame"]) - if err != nil { - if redirect { - r.Redirect("/") - return - } - //data["ErrorMsg"] = err - //log.Error("repo.Single: %v", err) - //r.HTML(200, "base/error", data) - return - } - - data["Repository"] = repo - data["Owner"] = user - data["Title"] = user.Name + "/" + repo.Name - data["RepositoryLink"] = data["Title"] - data["IsRepositoryValid"] = true - } -} diff --git a/modules/middleware/context.go b/modules/middleware/context.go index d3fe7bbfbe..42ef1320cb 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -29,6 +29,12 @@ type Context struct { Render render.Render User *models.User IsSigned bool + + Repo struct { + IsValid bool + IsOwner bool + Repository *models.Repository + } } // Query querys form parameter. diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go new file mode 100644 index 0000000000..fcf1f07b98 --- /dev/null +++ b/modules/middleware/repo.go @@ -0,0 +1,76 @@ +// 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 middleware + +import ( + "github.com/codegangsta/martini" + + "github.com/gogits/gogs/models" +) + +func RepoAssignment(redirect bool) martini.Handler { + return func(ctx *Context, params martini.Params) { + // assign false first + ctx.Data["IsRepositoryValid"] = false + + var ( + user *models.User + err error + ) + + // get repository owner + ctx.Repo.IsOwner = ctx.IsSigned && ctx.User.LowerName == params["username"] + ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner + + if !ctx.Repo.IsOwner { + user, err = models.GetUserByName(params["username"]) + if err != nil { + if redirect { + ctx.Render.Redirect("/") + return + } + //data["ErrorMsg"] = err + //log.Error("repo.Single: %v", err) + //r.HTML(200, "base/error", data) + return + } + } else { + user = ctx.User + } + + if user == nil { + if redirect { + ctx.Render.Redirect("/") + return + } + //data["ErrorMsg"] = "invliad user account for single repository" + //log.Error("repo.Single: %v", err) + //r.HTML(200, "base/error", data) + return + } + + // get repository + repo, err := models.GetRepositoryByName(user, params["reponame"]) + if err != nil { + if redirect { + ctx.Render.Redirect("/") + return + } + //data["ErrorMsg"] = err + //log.Error("repo.Single: %v", err) + //r.HTML(200, "base/error", data) + return + } + + ctx.Repo.IsValid = true + ctx.Repo.Repository = repo + + ctx.Data["IsRepositoryValid"] = true + ctx.Data["Repository"] = repo + ctx.Data["Owner"] = user + ctx.Data["Title"] = user.Name + "/" + repo.Name + ctx.Data["RepositoryLink"] = ctx.Data["Title"] + } +} diff --git a/routers/repo/single.go b/routers/repo/single.go index e933951050..bd5cfa1196 100644 --- a/routers/repo/single.go +++ b/routers/repo/single.go @@ -10,12 +10,14 @@ import ( ) func Single(ctx *middleware.Context, params martini.Params) { - if !ctx.Data["IsRepositoryValid"].(bool) { + if !ctx.Repo.IsValid { return } + if params["branchname"] == "" { params["branchname"] = "master" } + treename := params["_1"] files, err := models.GetReposFiles(params["username"], params["reponame"], params["branchname"], treename) @@ -46,11 +48,16 @@ func Single(ctx *middleware.Context, params martini.Params) { } func Setting(ctx *middleware.Context) { - if !ctx.Data["IsRepositoryValid"].(bool) { + if !ctx.Repo.IsValid { return } - ctx.Data["Title"] = ctx.Data["Title"].(string) + " - settings" + var title string + if t, ok := ctx.Data["Title"].(string); ok { + title = t + } + + ctx.Data["Title"] = title + " - settings" ctx.Data["IsRepoToolbarSetting"] = true ctx.Render.HTML(200, "repo/setting", ctx.Data) } diff --git a/web.go b/web.go index ad19a5da01..af430917f5 100644 --- a/web.go +++ b/web.go @@ -68,12 +68,12 @@ func runWeb(*cli.Context) { m.Any("/repo/delete", middleware.SignInRequire(true), binding.Bind(auth.DeleteRepoForm{}), repo.Delete) m.Any("/repo/list", middleware.SignInRequire(false), repo.List) - m.Get("/:username/:reponame/settings", middleware.SignInRequire(false), auth.RepoAssignment(true), repo.Setting) + m.Get("/:username/:reponame/settings", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Setting) m.Get("/:username/:reponame/tree/:branchname/**", - middleware.SignInRequire(false), auth.RepoAssignment(true), repo.Single) + middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single) m.Get("/:username/:reponame/tree/:branchname", - middleware.SignInRequire(false), auth.RepoAssignment(true), repo.Single) - m.Get("/:username/:reponame", middleware.SignInRequire(false), auth.RepoAssignment(true), repo.Single) + middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single) + m.Get("/:username/:reponame", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single) //m.Get("/:username/:reponame", repo.Repo) From dfcae2a97ae57cf8cb9e954b9d69151f5e340745 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 15 Mar 2014 12:13:45 -0400 Subject: [PATCH 2/6] Add repo list data, need front-end fix --- routers/repo/repo.go | 17 ----------------- routers/repo/single.go | 1 - routers/user/user.go | 7 ++++++- templates/user/dashboard.tmpl | 8 ++++---- templates/user/profile.tmpl | 8 +++++++- web.go | 1 - 6 files changed, 17 insertions(+), 25 deletions(-) diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 23f8ea107c..116c199b7d 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -67,20 +67,3 @@ func Delete(ctx *middleware.Context, form auth.DeleteRepoForm) { ctx.Render.Redirect("/", 302) } - -func List(ctx *middleware.Context) { - if ctx.User != nil { - ctx.Render.Redirect("/") - return - } - - ctx.Data["Title"] = "Repositories" - repos, err := models.GetRepositories(ctx.User) - if err != nil { - ctx.Handle(200, "repo.List", err) - return - } - - ctx.Data["Repos"] = repos - ctx.Render.HTML(200, "repo/list", ctx.Data) -} diff --git a/routers/repo/single.go b/routers/repo/single.go index e933951050..d796d9639d 100644 --- a/routers/repo/single.go +++ b/routers/repo/single.go @@ -41,7 +41,6 @@ func Single(ctx *middleware.Context, params martini.Params) { ctx.Data["Treenames"] = treenames ctx.Data["IsRepoToolbarSource"] = true ctx.Data["Files"] = files - ctx.Render.HTML(200, "repo/single", ctx.Data) } diff --git a/routers/user/user.go b/routers/user/user.go index 4ba7da1615..8d8691a39f 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -60,7 +60,12 @@ func Profile(ctx *middleware.Context, params martini.Params) { } ctx.Data["Feeds"] = feeds default: - + repos, err := models.GetRepositories(user) + if err != nil { + ctx.Handle(200, "user.Profile", err) + return + } + ctx.Data["Repos"] = repos } ctx.Render.HTML(200, "user/profile", ctx.Data) diff --git a/templates/user/dashboard.tmpl b/templates/user/dashboard.tmpl index 306c3bc15d..547e8ab556 100644 --- a/templates/user/dashboard.tmpl +++ b/templates/user/dashboard.tmpl @@ -4,9 +4,9 @@ @@ -29,7 +29,7 @@
Your Repositories - New Repo + New Repo
    {{range .MyRepos}} diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index 9444452012..aa15cb88bb 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -44,7 +44,13 @@
{{else}} -
repo
+ {{$owner := .Owner}} +
+ {{range .Repos}} +

{{.LowerName}} Star:{{.NumStars}} Fork:{{.NumForks}}

+

{{.Description}}

+ {{end}} +
{{end}}
diff --git a/web.go b/web.go index ad19a5da01..8d4fd5b5c9 100644 --- a/web.go +++ b/web.go @@ -66,7 +66,6 @@ func runWeb(*cli.Context) { m.Any("/repo/create", middleware.SignInRequire(true), binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create) m.Any("/repo/delete", middleware.SignInRequire(true), binding.Bind(auth.DeleteRepoForm{}), repo.Delete) - m.Any("/repo/list", middleware.SignInRequire(false), repo.List) m.Get("/:username/:reponame/settings", middleware.SignInRequire(false), auth.RepoAssignment(true), repo.Setting) m.Get("/:username/:reponame/tree/:branchname/**", From f18c8fc4d98bfb09719d7be0f246934257725142 Mon Sep 17 00:00:00 2001 From: slene Date: Sun, 16 Mar 2014 00:14:26 +0800 Subject: [PATCH 3/6] add Owner to Context.Repo --- modules/middleware/context.go | 1 + modules/middleware/repo.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/modules/middleware/context.go b/modules/middleware/context.go index 42ef1320cb..d002d3c24e 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -34,6 +34,7 @@ type Context struct { IsValid bool IsOwner bool Repository *models.Repository + Owner *models.User } } diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index fcf1f07b98..948713efe8 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -51,6 +51,8 @@ func RepoAssignment(redirect bool) martini.Handler { return } + ctx.Repo.Owner = user + // get repository repo, err := models.GetRepositoryByName(user, params["reponame"]) if err != nil { From 4224ef142e9b7d7f34ae7f24799d62ea787787c5 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 15 Mar 2014 12:29:49 -0400 Subject: [PATCH 4/6] Add template func FileSize --- modules/base/template.go | 1 + modules/base/tool.go | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/modules/base/template.go b/modules/base/template.go index 620bf4a186..b38ab140c9 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -21,6 +21,7 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ }, "str2html": Str2html, "TimeSince": TimeSince, + "FileSize": FileSize, "Subtract": Subtract, "ActionIcon": ActionIcon, "ActionDesc": ActionDesc, diff --git a/modules/base/tool.go b/modules/base/tool.go index 4802c413ed..8576b941cc 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -8,6 +8,7 @@ import ( "crypto/md5" "encoding/hex" "fmt" + "math" "strings" "time" ) @@ -80,6 +81,51 @@ func TimeSince(then time.Time) string { return then.String() } +const ( + Byte = 1 + KByte = Byte * 1024 + MByte = KByte * 1024 + GByte = MByte * 1024 + TByte = GByte * 1024 + PByte = TByte * 1024 + EByte = PByte * 1024 +) + +var bytesSizeTable = map[string]uint64{ + "b": Byte, + "kb": KByte, + "mb": MByte, + "gb": GByte, + "tb": TByte, + "pb": PByte, + "eb": EByte, +} + +func logn(n, b float64) float64 { + return math.Log(n) / math.Log(b) +} + +func humanateBytes(s uint64, base float64, sizes []string) string { + if s < 10 { + return fmt.Sprintf("%dB", s) + } + e := math.Floor(logn(float64(s), base)) + suffix := sizes[int(e)] + val := float64(s) / math.Pow(base, math.Floor(e)) + f := "%.0f" + if val < 10 { + f = "%.1f" + } + + return fmt.Sprintf(f+"%s", val, suffix) +} + +// FileSize calculates the file size and generate user-friendly string. +func FileSize(s uint64) string { + sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"} + return humanateBytes(uint64(s), 1024, sizes) +} + // Subtract deals with subtraction of all types of number. func Subtract(left interface{}, right interface{}) interface{} { var rleft, rright int64 From 8fb4b3afadaea5a335b0e13a02e44377d8714523 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 15 Mar 2014 12:31:12 -0400 Subject: [PATCH 5/6] Add template func FileSize --- modules/base/tool.go | 2 +- templates/repo/single.tmpl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/base/tool.go b/modules/base/tool.go index 8576b941cc..3f8b8ffa84 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -121,7 +121,7 @@ func humanateBytes(s uint64, base float64, sizes []string) string { } // FileSize calculates the file size and generate user-friendly string. -func FileSize(s uint64) string { +func FileSize(s int64) string { sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"} return humanateBytes(uint64(s), 1024, sizes) } diff --git a/templates/repo/single.tmpl b/templates/repo/single.tmpl index b99950ef51..5774c0051d 100644 --- a/templates/repo/single.tmpl +++ b/templates/repo/single.tmpl @@ -47,7 +47,7 @@ {{if .IsDir}} {{.Name}} {{else}} - {{.Name}} - {{.Size}} + {{.Name}} - {{FileSize .Size}} {{end}} {{.Message}} From 606ad6dcd03185c5755708af500072b2098dca3c Mon Sep 17 00:00:00 2001 From: slene Date: Sun, 16 Mar 2014 00:41:12 +0800 Subject: [PATCH 6/6] update user repos style --- public/css/gogs.css | 32 ++++++++++++++++++++++++++++++++ templates/user/profile.tmpl | 12 ++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/public/css/gogs.css b/public/css/gogs.css index 1c0b2d7be3..6b510364b7 100755 --- a/public/css/gogs.css +++ b/public/css/gogs.css @@ -551,6 +551,38 @@ html, body { border-top: none; } +.repo-list li { + padding: 15px 0; + border-top: 1px solid #ddd; +} + +.repo-list li:first-child { + border-top: none; +} + +.repo-list h4 { + font-weight: bold; + font-size: 24px; +} + +.repo-list .meta { + margin: 15px 0 0; + font-size: 14px; +} + +.repo-list .desc { + font-size: 15px; +} + +.repo-list .meta .fa { + margin: 0 0 0 20px; +} + +.repo-list .meta, +.repo-list .info { + color: #999; +} + #wrapper { min-height: 100%; height: auto !important; diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index aa15cb88bb..84d3b13c92 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -46,10 +46,18 @@ {{else}} {{$owner := .Owner}}
+
    {{range .Repos}} -

    {{.LowerName}} Star:{{.NumStars}} Fork:{{.NumForks}}

    -

    {{.Description}}

    +
  • +
    {{.NumStars}} {{.NumForks}}
    +

    + {{.LowerName}} +

    +

    {{.Description}}

    +
    Last updated {{.Updated|TimeSince}}
    +
  • {{end}} +
{{end}}