Paging function for users and repositories

This commit is contained in:
Unknown 2014-07-07 04:15:08 -04:00
parent 7ffdabb28f
commit 63cc14062a
9 changed files with 71 additions and 7 deletions

View File

@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.4.5.0706 Alpha" const APP_VER = "0.4.5.0707 Alpha"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

View File

@ -148,9 +148,9 @@ type Statistic struct {
} }
func GetStatistic() (stats Statistic) { 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.PublicKey, _ = x.Count(new(PublicKey))
stats.Counter.Repo, _ = x.Count(new(Repository))
stats.Counter.Watch, _ = x.Count(new(Watch)) stats.Counter.Watch, _ = x.Count(new(Watch))
stats.Counter.Action, _ = x.Count(new(Action)) stats.Counter.Action, _ = x.Count(new(Action))
stats.Counter.Access, _ = x.Count(new(Access)) stats.Counter.Access, _ = x.Count(new(Access))

View File

@ -589,6 +589,12 @@ func CreateRepository(u *User, name, desc, lang, license string, private, mirror
return repo, nil 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. // GetRepositoriesWithUsers returns given number of repository objects with offset.
// It also auto-gets corresponding users. // It also auto-gets corresponding users.
func GetRepositoriesWithUsers(num, offset int) ([]*Repository, error) { func GetRepositoriesWithUsers(num, offset int) ([]*Repository, error) {

View File

@ -212,6 +212,12 @@ func CreateUser(u *User) (*User, error) {
return u, err 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. // GetUsers returns given number of user objects with offset.
func GetUsers(num, offset int) ([]User, error) { func GetUsers(num, offset int) ([]User, error) {
users := make([]User, 0, num) users := make([]User, 0, num)

View File

@ -458,6 +458,16 @@ func (f StrTo) Int64() (int64, error) {
return int64(v), err 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 { func (f StrTo) String() string {
if f.Exist() { if f.Exist() {
return string(f) return string(f)

View File

@ -30,7 +30,9 @@ const (
MONITOR_CRON base.TplName = "admin/monitor/cron" MONITOR_CRON base.TplName = "admin/monitor/cron"
) )
var startTime = time.Now() var (
startTime = time.Now()
)
var sysStatus struct { var sysStatus struct {
Uptime string Uptime string
@ -157,8 +159,24 @@ func Users(ctx *middleware.Context) {
ctx.Data["Title"] = "User Management" ctx.Data["Title"] = "User Management"
ctx.Data["PageIsUsers"] = true 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 var err error
ctx.Data["Users"], err = models.GetUsers(200, 0) ctx.Data["Users"], err = models.GetUsers(pageNum, (p-1)*pageNum)
if err != nil { if err != nil {
ctx.Handle(500, "admin.Users(GetUsers)", err) ctx.Handle(500, "admin.Users(GetUsers)", err)
return return
@ -170,8 +188,24 @@ func Repositories(ctx *middleware.Context) {
ctx.Data["Title"] = "Repository Management" ctx.Data["Title"] = "Repository Management"
ctx.Data["PageIsRepos"] = true 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 var err error
ctx.Data["Repos"], err = models.GetRepositoriesWithUsers(200, 0) ctx.Data["Repos"], err = models.GetRepositoriesWithUsers(pageNum, (p-1)*pageNum)
if err != nil { if err != nil {
ctx.Handle(500, "admin.Repositories", err) ctx.Handle(500, "admin.Repositories", err)
return return

View File

@ -1 +1 @@
0.4.5.0706 Alpha 0.4.5.0707 Alpha

View File

@ -37,6 +37,10 @@
{{end}} {{end}}
</tbody> </tbody>
</table> </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> </div>
</div> </div>

View File

@ -38,6 +38,10 @@
{{end}} {{end}}
</tbody> </tbody>
</table> </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> </div>
</div> </div>