#13 finish user and repository search

Both are possible on explore and admin panel
This commit is contained in:
Unknwon 2016-03-11 15:33:12 -05:00
parent df2bdf7ea3
commit 2bf8494332
31 changed files with 636 additions and 463 deletions

View file

@ -5,12 +5,11 @@
package admin
import (
"github.com/Unknwon/paginater"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/modules/setting"
"github.com/gogits/gogs/routers"
)
const (
@ -22,22 +21,6 @@ func Organizations(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminOrganizations"] = true
total := models.CountOrganizations()
page := ctx.QueryInt("page")
if page <= 1 {
page = 1
}
ctx.Data["Page"] = paginater.New(int(total), setting.AdminOrgPagingNum, page, 5)
orgs, err := models.Organizations(page, setting.AdminOrgPagingNum)
if err != nil {
ctx.Handle(500, "Organizations", err)
return
}
ctx.Data["Orgs"] = orgs
ctx.Data["Total"] = total
ctx.HTML(200, ORGS)
routers.RenderUserSearch(ctx, models.USER_TYPE_ORGANIZATION, models.CountOrganizations, models.Organizations,
setting.AdminOrgPagingNum, "id ASC", ORGS)
}

View file

@ -5,13 +5,12 @@
package admin
import (
"github.com/Unknwon/paginater"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/setting"
"github.com/gogits/gogs/routers"
)
const (
@ -23,22 +22,8 @@ func Repos(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminRepositories"] = true
total := models.CountRepositories()
page := ctx.QueryInt("page")
if page <= 1 {
page = 1
}
ctx.Data["Page"] = paginater.New(int(total), setting.AdminRepoPagingNum, page, 5)
repos, err := models.RepositoriesWithUsers(page, setting.AdminRepoPagingNum)
if err != nil {
ctx.Handle(500, "RepositoriesWithUsers", err)
return
}
ctx.Data["Repos"] = repos
ctx.Data["Total"] = total
ctx.HTML(200, REPOS)
routers.RenderRepoSearch(ctx, models.CountRepositories, models.Repositories,
setting.AdminRepoPagingNum, "id ASC", REPOS)
}
func DeleteRepo(ctx *context.Context) {

View file

@ -8,7 +8,6 @@ import (
"strings"
"github.com/Unknwon/com"
"github.com/Unknwon/paginater"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
@ -17,6 +16,7 @@ import (
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/mailer"
"github.com/gogits/gogs/modules/setting"
"github.com/gogits/gogs/routers"
)
const (
@ -30,22 +30,8 @@ func Users(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminUsers"] = true
total := models.CountUsers()
page := ctx.QueryInt("page")
if page <= 1 {
page = 1
}
ctx.Data["Page"] = paginater.New(int(total), setting.AdminUserPagingNum, page, 5)
users, err := models.Users(page, setting.AdminUserPagingNum)
if err != nil {
ctx.Handle(500, "Users", err)
return
}
ctx.Data["Users"] = users
ctx.Data["Total"] = total
ctx.HTML(200, USERS)
routers.RenderUserSearch(ctx, models.USER_TYPE_INDIVIDUAL, models.CountUsers, models.Users,
setting.AdminUserPagingNum, "id ASC", USERS)
}
func NewUser(ctx *context.Context) {

View file

@ -27,7 +27,7 @@ func CreateOrg(ctx *context.Context, form api.CreateOrgOption) {
Website: form.Website,
Location: form.Location,
IsActive: true,
Type: models.ORGANIZATION,
Type: models.USER_TYPE_ORGANIZATION,
}
if err := models.CreateOrganization(org, u); err != nil {
if models.IsErrUserAlreadyExist(err) ||

View file

@ -21,21 +21,21 @@ import (
// https://github.com/gogits/go-gogs-client/wiki/Repositories#search-repositories
func Search(ctx *context.Context) {
opt := models.SearchOption{
Keyword: path.Base(ctx.Query("q")),
Uid: com.StrTo(ctx.Query("uid")).MustInt64(),
Limit: com.StrTo(ctx.Query("limit")).MustInt(),
opts := &models.SearchRepoOptions{
Keyword: path.Base(ctx.Query("q")),
OwnerID: com.StrTo(ctx.Query("uid")).MustInt64(),
PageSize: com.StrTo(ctx.Query("limit")).MustInt(),
}
if opt.Limit == 0 {
opt.Limit = 10
if opts.PageSize == 0 {
opts.PageSize = 10
}
// Check visibility.
if ctx.IsSigned && opt.Uid > 0 {
if ctx.User.Id == opt.Uid {
opt.Private = true
if ctx.IsSigned && opts.OwnerID > 0 {
if ctx.User.Id == opts.OwnerID {
opts.Private = true
} else {
u, err := models.GetUserByID(opt.Uid)
u, err := models.GetUserByID(opts.OwnerID)
if err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
@ -44,13 +44,13 @@ func Search(ctx *context.Context) {
return
}
if u.IsOrganization() && u.IsOwnedBy(ctx.User.Id) {
opt.Private = true
opts.Private = true
}
// FIXME: how about collaborators?
}
}
repos, err := models.SearchRepositoryByName(opt)
repos, _, err := models.SearchRepositoryByName(opts)
if err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,

View file

@ -15,15 +15,16 @@ import (
// https://github.com/gogits/go-gogs-client/wiki/Users#search-users
func Search(ctx *context.Context) {
opt := models.SearchOption{
Keyword: ctx.Query("q"),
Limit: com.StrTo(ctx.Query("limit")).MustInt(),
opts := &models.SearchUserOptions{
Keyword: ctx.Query("q"),
Type: models.USER_TYPE_INDIVIDUAL,
PageSize: com.StrTo(ctx.Query("limit")).MustInt(),
}
if opt.Limit == 0 {
opt.Limit = 10
if opts.PageSize == 0 {
opts.PageSize = 10
}
us, err := models.SearchUserByName(opt)
users, _, err := models.SearchUserByName(opts)
if err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
@ -32,16 +33,16 @@ func Search(ctx *context.Context) {
return
}
results := make([]*api.User, len(us))
for i := range us {
results := make([]*api.User, len(users))
for i := range users {
results[i] = &api.User{
ID: us[i].Id,
UserName: us[i].Name,
AvatarUrl: us[i].AvatarLink(),
FullName: us[i].FullName,
ID: users[i].Id,
UserName: users[i].Name,
AvatarUrl: users[i].AvatarLink(),
FullName: users[i].FullName,
}
if ctx.IsSigned {
results[i].Email = us[i].Email
results[i].Email = users[i].Email
}
}

View file

@ -19,6 +19,7 @@ import (
const (
HOME base.TplName = "home"
EXPLORE_REPOS base.TplName = "explore/repos"
EXPLORE_USERS base.TplName = "explore/users"
)
func Home(ctx *context.Context) {
@ -43,23 +44,44 @@ func Home(ctx *context.Context) {
ctx.HTML(200, HOME)
}
func Explore(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("explore")
ctx.Data["PageIsExplore"] = true
ctx.Data["PageIsExploreRepositories"] = true
func RenderRepoSearch(ctx *context.Context,
counter func() int64, ranger func(int, int) ([]*models.Repository, error),
pagingNum int, orderBy string, tplName base.TplName) {
page := ctx.QueryInt("page")
if page <= 1 {
page = 1
}
ctx.Data["Page"] = paginater.New(int(models.CountPublicRepositories()), setting.ExplorePagingNum, page, 5)
var (
repos []*models.Repository
count int64
err error
)
repos, err := models.GetRecentUpdatedRepositories(page)
if err != nil {
ctx.Handle(500, "GetRecentUpdatedRepositories", err)
return
keyword := ctx.Query("q")
if len(keyword) == 0 {
repos, err = ranger(page, pagingNum)
if err != nil {
ctx.Handle(500, "ranger", err)
return
}
count = counter()
} else {
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
Keyword: keyword,
OrderBy: orderBy,
Page: page,
PageSize: pagingNum,
})
if err != nil {
ctx.Handle(500, "SearchRepositoryByName", err)
return
}
}
ctx.Data["Keyword"] = keyword
ctx.Data["Total"] = count
ctx.Data["Page"] = paginater.New(int(count), pagingNum, page, 5)
for _, repo := range repos {
if err = repo.GetOwner(); err != nil {
ctx.Handle(500, "GetOwner", fmt.Errorf("%d: %v", repo.ID, err))
@ -68,7 +90,68 @@ func Explore(ctx *context.Context) {
}
ctx.Data["Repos"] = repos
ctx.HTML(200, EXPLORE_REPOS)
ctx.HTML(200, tplName)
}
func ExploreRepos(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("explore")
ctx.Data["PageIsExplore"] = true
ctx.Data["PageIsExploreRepositories"] = true
RenderRepoSearch(ctx, models.CountPublicRepositories, models.GetRecentUpdatedRepositories,
setting.ExplorePagingNum, "updated_unix DESC", EXPLORE_REPOS)
}
func RenderUserSearch(ctx *context.Context, userType models.UserType,
counter func() int64, ranger func(int, int) ([]*models.User, error),
pagingNum int, orderBy string, tplName base.TplName) {
page := ctx.QueryInt("page")
if page <= 1 {
page = 1
}
var (
users []*models.User
count int64
err error
)
keyword := ctx.Query("q")
if len(keyword) == 0 {
users, err = ranger(page, pagingNum)
if err != nil {
ctx.Handle(500, "ranger", err)
return
}
count = counter()
} else {
users, count, err = models.SearchUserByName(&models.SearchUserOptions{
Keyword: keyword,
Type: userType,
OrderBy: orderBy,
Page: page,
PageSize: pagingNum,
})
if err != nil {
ctx.Handle(500, "SearchUserByName", err)
return
}
}
ctx.Data["Keyword"] = keyword
ctx.Data["Total"] = count
ctx.Data["Page"] = paginater.New(int(count), pagingNum, page, 5)
ctx.Data["Users"] = users
ctx.HTML(200, tplName)
}
func ExploreUsers(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("explore")
ctx.Data["PageIsExplore"] = true
ctx.Data["PageIsExploreUsers"] = true
RenderUserSearch(ctx, models.USER_TYPE_INDIVIDUAL, models.CountUsers, models.Users,
setting.ExplorePagingNum, "updated_unix DESC", EXPLORE_USERS)
}
func NotFound(ctx *context.Context) {

View file

@ -33,7 +33,7 @@ func CreatePost(ctx *context.Context, form auth.CreateOrgForm) {
org := &models.User{
Name: form.OrgName,
IsActive: true,
Type: models.ORGANIZATION,
Type: models.USER_TYPE_ORGANIZATION,
}
if err := models.CreateOrganization(org, ctx.User); err != nil {