Skip to content

Refactor pagination #33037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions models/user/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ type SearchUserOptions struct {
IsTwoFactorEnabled optional.Option[bool]
IsProhibitLogin optional.Option[bool]
IncludeReserved bool

ExtraParamStrings map[string]string
}

func (opts *SearchUserOptions) toSearchQueryBase(ctx context.Context) *xorm.Session {
Expand Down
2 changes: 1 addition & 1 deletion routers/web/admin/emails.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func Emails(ctx *context.Context) {
ctx.Data["Emails"] = emails

pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

ctx.HTML(http.StatusOK, tplEmails)
Expand Down
4 changes: 1 addition & 3 deletions routers/web/admin/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ func Packages(ctx *context.Context) {
ctx.Data["TotalUnreferencedBlobSize"] = totalUnreferencedBlobSize

pager := context.NewPagination(int(total), setting.UI.PackagesPagingNum, page, 5)
pager.AddParamString("q", query)
pager.AddParamString("type", packageType)
pager.AddParamString("sort", sort)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

ctx.HTML(http.StatusOK, tplPackagesList)
Expand Down
7 changes: 2 additions & 5 deletions routers/web/admin/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package admin

import (
"fmt"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -84,8 +83,7 @@ func UnadoptedRepos(ctx *context.Context) {

if !doSearch {
pager := context.NewPagination(0, opts.PageSize, opts.Page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamString("search", fmt.Sprint(doSearch))
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplUnadoptedRepos)
return
Expand All @@ -99,8 +97,7 @@ func UnadoptedRepos(ctx *context.Context) {
}
ctx.Data["Dirs"] = repoNames
pager := context.NewPagination(count, opts.PageSize, opts.Page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamString("search", fmt.Sprint(doSearch))
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplUnadoptedRepos)
}
Expand Down
5 changes: 0 additions & 5 deletions routers/web/admin/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,12 @@ func Users(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("admin.users")
ctx.Data["PageIsAdminUsers"] = true

extraParamStrings := map[string]string{}
statusFilterKeys := []string{"is_active", "is_admin", "is_restricted", "is_2fa_enabled", "is_prohibit_login"}
statusFilterMap := map[string]string{}
for _, filterKey := range statusFilterKeys {
paramKey := "status_filter[" + filterKey + "]"
paramVal := ctx.FormString(paramKey)
statusFilterMap[filterKey] = paramVal
if paramVal != "" {
extraParamStrings[paramKey] = paramVal
}
}

sortType := ctx.FormString("sort")
Expand All @@ -82,7 +78,6 @@ func Users(ctx *context.Context) {
IsTwoFactorEnabled: util.OptionalBoolParse(statusFilterMap["is_2fa_enabled"]),
IsProhibitLogin: util.OptionalBoolParse(statusFilterMap["is_prohibit_login"]),
IncludeReserved: true, // administrator needs to list all accounts include reserved, bot, remote ones
ExtraParamStrings: extraParamStrings,
}, tplUsers)
}

Expand Down
3 changes: 1 addition & 2 deletions routers/web/explore/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ func Code(ctx *context.Context) {
ctx.Data["SearchResultLanguages"] = searchResultLanguages

pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamString("l", language)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

ctx.HTML(http.StatusOK, tplExploreCode)
Expand Down
21 changes: 1 addition & 20 deletions routers/web/explore/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package explore

import (
"fmt"
"net/http"

"code.gitea.io/gitea/models/db"
Expand Down Expand Up @@ -139,25 +138,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled

pager := context.NewPagination(int(count), opts.PageSize, page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamString("topic", fmt.Sprint(topicOnly))
pager.AddParamString("language", language)
pager.AddParamString(relevantReposOnlyParam, fmt.Sprint(opts.OnlyShowRelevant))
if archived.Has() {
pager.AddParamString("archived", fmt.Sprint(archived.Value()))
}
if fork.Has() {
pager.AddParamString("fork", fmt.Sprint(fork.Value()))
}
if mirror.Has() {
pager.AddParamString("mirror", fmt.Sprint(mirror.Value()))
}
if template.Has() {
pager.AddParamString("template", fmt.Sprint(template.Value()))
}
if private.Has() {
pager.AddParamString("private", fmt.Sprint(private.Value()))
}
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

ctx.HTML(http.StatusOK, opts.TplName)
Expand Down
5 changes: 1 addition & 4 deletions routers/web/explore/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled

pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
pager.SetDefaultParams(ctx)
for paramKey, paramVal := range opts.ExtraParamStrings {
pager.AddParamString(paramKey, paramVal)
}
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

ctx.HTML(http.StatusOK, tplName)
Expand Down
19 changes: 1 addition & 18 deletions routers/web/org/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package org

import (
"fmt"
"net/http"
"path"
"strings"
Expand Down Expand Up @@ -146,23 +145,7 @@ func home(ctx *context.Context, viewRepositories bool) {
ctx.Data["Total"] = count

pager := context.NewPagination(int(count), setting.UI.User.RepoPagingNum, page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamString("language", language)
if archived.Has() {
pager.AddParamString("archived", fmt.Sprint(archived.Value()))
}
if fork.Has() {
pager.AddParamString("fork", fmt.Sprint(fork.Value()))
}
if mirror.Has() {
pager.AddParamString("mirror", fmt.Sprint(mirror.Value()))
}
if template.Has() {
pager.AddParamString("template", fmt.Sprint(template.Value()))
}
if private.Has() {
pager.AddParamString("private", fmt.Sprint(private.Value()))
}
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

ctx.HTML(http.StatusOK, tplOrgHome)
Expand Down
2 changes: 1 addition & 1 deletion routers/web/org/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func Projects(ctx *context.Context) {
}

pager := context.NewPagination(int(total), setting.UI.IssuePagingNum, page, numPages)
pager.AddParamString("state", fmt.Sprint(ctx.Data["State"]))
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

ctx.Data["CanWriteProjects"] = canWriteProjects(ctx)
Expand Down
6 changes: 1 addition & 5 deletions routers/web/repo/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package actions
import (
"bytes"
stdCtx "context"
"fmt"
"net/http"
"slices"
"strings"
Expand Down Expand Up @@ -262,10 +261,7 @@ func List(ctx *context.Context) {
ctx.Data["StatusInfoList"] = actions_model.GetStatusInfoList(ctx)

pager := context.NewPagination(int(total), opts.PageSize, opts.Page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamString("workflow", workflowID)
pager.AddParamString("actor", fmt.Sprint(actorID))
pager.AddParamString("status", fmt.Sprint(status))
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager
ctx.Data["HasWorkflowsOrRuns"] = len(workflows) > 0 || len(runs) > 0

Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func Branches(ctx *context.Context) {
ctx.Data["CommitStatuses"] = commitStatuses
ctx.Data["DefaultBranchBranch"] = defaultBranch
pager := context.NewPagination(int(branchesCount), pageSize, page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplBranch)
}
Expand Down
14 changes: 3 additions & 11 deletions routers/web/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func Commits(ctx *context.Context) {
ctx.Data["CommitCount"] = commitsCount

pager := context.NewPagination(int(commitsCount), pageSize, page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplCommits)
}
Expand Down Expand Up @@ -139,7 +139,6 @@ func Graph(ctx *context.Context) {
if err != nil {
log.Warn("GetCommitGraphsCount error for generate graph exclude prs: %t branches: %s in %-v, Will Ignore branches and try again. Underlying Error: %v", hidePRRefs, branches, ctx.Repo.Repository, err)
realBranches = []string{}
branches = []string{}
graphCommitsCount, err = ctx.Repo.GetCommitGraphsCount(ctx, hidePRRefs, realBranches, files)
if err != nil {
ctx.ServerError("GetCommitGraphsCount", err)
Expand Down Expand Up @@ -175,14 +174,7 @@ func Graph(ctx *context.Context) {
ctx.Data["CommitCount"] = commitsCount

paginator := context.NewPagination(int(graphCommitsCount), setting.UI.GraphMaxCommitNum, page, 5)
paginator.AddParamString("mode", mode)
paginator.AddParamString("hide-pr-refs", fmt.Sprint(hidePRRefs))
for _, branch := range branches {
paginator.AddParamString("branch", branch)
}
for _, file := range files {
paginator.AddParamString("file", file)
}
paginator.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = paginator
if ctx.FormBool("div-only") {
ctx.HTML(http.StatusOK, tplGraphDiv)
Expand Down Expand Up @@ -262,7 +254,7 @@ func FileHistory(ctx *context.Context) {
ctx.Data["CommitCount"] = commitsCount

pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplCommits)
}
Expand Down
4 changes: 1 addition & 3 deletions routers/web/repo/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package repo

import (
"fmt"
"net/http"
"net/url"

Expand Down Expand Up @@ -93,8 +92,7 @@ func Milestones(ctx *context.Context) {
ctx.Data["IsShowClosed"] = isShowClosed

pager := context.NewPagination(int(total), setting.UI.IssuePagingNum, page, 5)
pager.AddParamString("state", fmt.Sprint(ctx.Data["State"]))
pager.AddParamString("q", keyword)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

ctx.HTML(http.StatusOK, tplMilestone)
Expand Down
3 changes: 1 addition & 2 deletions routers/web/repo/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func Packages(ctx *context.Context) {
ctx.Data["RepositoryAccessMap"] = map[int64]bool{ctx.Repo.Repository.ID: true} // There is only the current repository

pager := context.NewPagination(int(total), setting.UI.PackagesPagingNum, page, 5)
pager.AddParamString("q", query)
pager.AddParamString("type", packageType)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

ctx.HTML(http.StatusOK, tplPackagesList)
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Projects(ctx *context.Context) {
}

pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, numPages)
pager.AddParamString("state", fmt.Sprint(ctx.Data["State"]))
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects)
Expand Down
4 changes: 2 additions & 2 deletions routers/web/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func Releases(ctx *context.Context) {

numReleases := ctx.Data["NumReleases"].(int64)
pager := context.NewPagination(int(numReleases), listOptions.PageSize, listOptions.Page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplReleasesList)
}
Expand Down Expand Up @@ -240,7 +240,7 @@ func TagsList(ctx *context.Context) {
ctx.Data["TagCount"] = count

pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager
ctx.Data["PageIsViewCode"] = !ctx.Repo.Repository.UnitEnabled(ctx, unit.TypeReleases)
ctx.HTML(http.StatusOK, tplTagsList)
Expand Down
3 changes: 1 addition & 2 deletions routers/web/repo/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ func Search(ctx *context.Context) {
ctx.Data["SearchResultLanguages"] = searchResultLanguages

pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamString("l", language)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

ctx.HTML(http.StatusOK, tplSearch)
Expand Down
3 changes: 1 addition & 2 deletions routers/web/repo/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
ctx.Data["Commits"] = git_model.ConvertFromGitCommit(ctx, commitsHistory, ctx.Repo.Repository)

pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamString("action", "_revision")
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

return wikiRepo, entry
Expand Down
3 changes: 1 addition & 2 deletions routers/web/user/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ func CodeSearch(ctx *context.Context) {
ctx.Data["SearchResultLanguages"] = searchResultLanguages

pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamString("l", language)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

ctx.HTML(http.StatusOK, tplUserCode)
Expand Down
7 changes: 2 additions & 5 deletions routers/web/user/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func Dashboard(ctx *context.Context) {
ctx.Data["Feeds"] = feeds

pager := context.NewPagination(int(count), setting.UI.FeedPagingNum, page, 5)
pager.AddParamString("date", date)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

ctx.HTML(http.StatusOK, tplDashboard)
Expand Down Expand Up @@ -330,10 +330,7 @@ func Milestones(ctx *context.Context) {
ctx.Data["IsShowClosed"] = isShowClosed

pager := context.NewPagination(pagerCount, setting.UI.IssuePagingNum, page, 5)
pager.AddParamString("q", keyword)
pager.AddParamString("repos", reposQuery)
pager.AddParamString("sort", sortType)
pager.AddParamString("state", fmt.Sprint(ctx.Data["State"]))
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager

ctx.HTML(http.StatusOK, tplMilestones)
Expand Down
Loading
Loading