Browse Source

Paging function for users and repositories

Unknown 10 years ago
parent
commit
63cc14062a
9 changed files with 71 additions and 7 deletions
  1. 1 1
      gogs.go
  2. 2 2
      models/models.go
  3. 6 0
      models/repo.go
  4. 6 0
      models/user.go
  5. 10 0
      modules/base/tool.go
  6. 37 3
      routers/admin/admin.go
  7. 1 1
      templates/VERSION
  8. 4 0
      templates/admin/repos.tmpl
  9. 4 0
      templates/admin/users.tmpl

+ 1 - 1
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())

+ 2 - 2
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))

+ 6 - 0
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) {

+ 6 - 0
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)

+ 10 - 0
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)

+ 37 - 3
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

+ 1 - 1
templates/VERSION

@@ -1 +1 @@
-0.4.5.0706 Alpha
+0.4.5.0707 Alpha

+ 4 - 0
templates/admin/repos.tmpl

@@ -37,6 +37,10 @@
                         {{end}}
                     </tbody>
                 </table>
+                <ul class="pagination">
+                    {{if .LastPageNum}}<li><a href="/admin/repos?p={{.LastPageNum}}">&laquo; Prev.</a></li>{{end}}
+                    {{if .NextPageNum}}<li><a href="/admin/repos?p={{.NextPageNum}}">&raquo; Next</a></li>{{end}}
+                </ul>
             </div>
         </div>
     </div>

+ 4 - 0
templates/admin/users.tmpl

@@ -38,6 +38,10 @@
                         {{end}}
                     </tbody>
                 </table>
+                <ul class="pagination">
+                    {{if .LastPageNum}}<li><a href="/admin/users?p={{.LastPageNum}}">&laquo; Prev.</a></li>{{end}}
+                    {{if .NextPageNum}}<li><a href="/admin/users?p={{.NextPageNum}}">&raquo; Next</a></li>{{end}}
+                </ul>
             </div>
         </div>
     </div>