From 63cc14062a891a99a429f2eef0ee90a3f5795f47 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 7 Jul 2014 04:15:08 -0400 Subject: [PATCH] Paging function for users and repositories --- gogs.go | 2 +- models/models.go | 4 ++-- models/repo.go | 6 ++++++ models/user.go | 6 ++++++ modules/base/tool.go | 10 ++++++++++ routers/admin/admin.go | 40 +++++++++++++++++++++++++++++++++++--- templates/VERSION | 2 +- templates/admin/repos.tmpl | 4 ++++ templates/admin/users.tmpl | 4 ++++ 9 files changed, 71 insertions(+), 7 deletions(-) diff --git a/gogs.go b/gogs.go index 28abaa3ee..0014b76f9 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.4.5.0706 Alpha" +const APP_VER = "0.4.5.0707 Alpha" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/models.go b/models/models.go index 070784f13..ded8b0598 100644 --- a/models/models.go +++ b/models/models.go @@ -148,9 +148,9 @@ type Statistic struct { } func GetStatistic() (stats Statistic) { - stats.Counter.User, _ = x.Count(new(User)) + stats.Counter.User = CountUsers() + stats.Counter.Repo = CountRepositories() stats.Counter.PublicKey, _ = x.Count(new(PublicKey)) - stats.Counter.Repo, _ = x.Count(new(Repository)) stats.Counter.Watch, _ = x.Count(new(Watch)) stats.Counter.Action, _ = x.Count(new(Action)) stats.Counter.Access, _ = x.Count(new(Access)) diff --git a/models/repo.go b/models/repo.go index 35e442757..396e536e4 100644 --- a/models/repo.go +++ b/models/repo.go @@ -589,6 +589,12 @@ func CreateRepository(u *User, name, desc, lang, license string, private, mirror return repo, nil } +// CountRepositories returns number of repositories. +func CountRepositories() int64 { + count, _ := x.Count(new(Repository)) + return count +} + // GetRepositoriesWithUsers returns given number of repository objects with offset. // It also auto-gets corresponding users. func GetRepositoriesWithUsers(num, offset int) ([]*Repository, error) { diff --git a/models/user.go b/models/user.go index b98e81bab..47f1d45f0 100644 --- a/models/user.go +++ b/models/user.go @@ -212,6 +212,12 @@ func CreateUser(u *User) (*User, error) { return u, err } +// CountUsers returns number of users. +func CountUsers() int64 { + count, _ := x.Where("type=0").Count(new(User)) + return count +} + // GetUsers returns given number of user objects with offset. func GetUsers(num, offset int) ([]User, error) { users := make([]User, 0, num) diff --git a/modules/base/tool.go b/modules/base/tool.go index 5b066b2b9..9635f13e0 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -458,6 +458,16 @@ func (f StrTo) Int64() (int64, error) { return int64(v), err } +func (f StrTo) MustInt() int { + v, _ := f.Int() + return v +} + +func (f StrTo) MustInt64() int64 { + v, _ := f.Int64() + return v +} + func (f StrTo) String() string { if f.Exist() { return string(f) diff --git a/routers/admin/admin.go b/routers/admin/admin.go index 50a3823a0..2a40792a1 100644 --- a/routers/admin/admin.go +++ b/routers/admin/admin.go @@ -30,7 +30,9 @@ const ( MONITOR_CRON base.TplName = "admin/monitor/cron" ) -var startTime = time.Now() +var ( + startTime = time.Now() +) var sysStatus struct { Uptime string @@ -157,8 +159,24 @@ func Users(ctx *middleware.Context) { ctx.Data["Title"] = "User Management" ctx.Data["PageIsUsers"] = true + p := base.StrTo(ctx.Query("p")).MustInt() + if p < 1 { + p = 1 + } + pageNum := 100 + count := models.CountUsers() + curCount := int64((p-1)*pageNum + pageNum) + if curCount > count { + p = int(count) / pageNum + } else if count > curCount { + ctx.Data["NextPageNum"] = p + 1 + } + if p > 1 { + ctx.Data["LastPageNum"] = p - 1 + } + var err error - ctx.Data["Users"], err = models.GetUsers(200, 0) + ctx.Data["Users"], err = models.GetUsers(pageNum, (p-1)*pageNum) if err != nil { ctx.Handle(500, "admin.Users(GetUsers)", err) return @@ -170,8 +188,24 @@ func Repositories(ctx *middleware.Context) { ctx.Data["Title"] = "Repository Management" ctx.Data["PageIsRepos"] = true + p := base.StrTo(ctx.Query("p")).MustInt() + if p < 1 { + p = 1 + } + pageNum := 2 + count := models.CountRepositories() + curCount := int64((p-1)*pageNum + pageNum) + if curCount > count { + p = int(count) / pageNum + } else if count > curCount { + ctx.Data["NextPageNum"] = p + 1 + } + if p > 1 { + ctx.Data["LastPageNum"] = p - 1 + } + var err error - ctx.Data["Repos"], err = models.GetRepositoriesWithUsers(200, 0) + ctx.Data["Repos"], err = models.GetRepositoriesWithUsers(pageNum, (p-1)*pageNum) if err != nil { ctx.Handle(500, "admin.Repositories", err) return diff --git a/templates/VERSION b/templates/VERSION index 9821ab4ed..636217884 100644 --- a/templates/VERSION +++ b/templates/VERSION @@ -1 +1 @@ -0.4.5.0706 Alpha \ No newline at end of file +0.4.5.0707 Alpha \ No newline at end of file diff --git a/templates/admin/repos.tmpl b/templates/admin/repos.tmpl index f6f3a7be9..373348b9d 100644 --- a/templates/admin/repos.tmpl +++ b/templates/admin/repos.tmpl @@ -37,6 +37,10 @@ {{end}} + diff --git a/templates/admin/users.tmpl b/templates/admin/users.tmpl index 14fddf33c..0efe90929 100644 --- a/templates/admin/users.tmpl +++ b/templates/admin/users.tmpl @@ -38,6 +38,10 @@ {{end}} +