Example #1
0
func (this *AdminRouter) ModelGet() {
	id := this.GetString("id")
	model := this.GetString("model")
	result := map[string]interface{}{
		"success": false,
	}

	var data []orm.ParamsList

	defer func() {
		if len(data) > 0 {
			result["success"] = true
			result["data"] = data[0]
		}
		this.Data["json"] = result
		this.ServeJson()
	}()

	var qs orm.QuerySeter

	switch model {
	case "User":
		qs = models.Users()
	}

	qs = qs.Filter("Id", id).Limit(1)

	switch model {
	case "User":
		qs.ValuesList(&data, "Id", "UserName")
	}
}
Example #2
0
// 获取查询列表
func ListObjects(qs orm.QuerySeter, objs interface{}) (int64, error) {
	nums, err := qs.All(objs)
	if err != nil {
		beego.Error("models.ListObjects ", err)
		return 0, err
	}
	return nums, err
}
Example #3
0
// 获取总记录数
func CountObjects(qs orm.QuerySeter) (int64, error) {
	cnt, err := qs.Count()
	if err != nil {
		beego.Error("models.CountObjects ", err)
		return 0, err
	}
	return cnt, err
}
Example #4
0
func (this *PostListRouter) postsFilter(qs orm.QuerySeter) orm.QuerySeter {
	args := []string{utils.ToStr(this.Locale.Index())}
	if this.isLogin {
		args = append(args, this.user.LangAdds...)
		args = append(args, utils.ToStr(this.user.Lang))
	}
	qs = qs.Filter("Lang__in", args)
	return qs
}
func GetUser(email, passwd string) ([]User, error) {
	o := orm.NewOrm()

	var users []User
	var cond *orm.Condition
	cond = orm.NewCondition()
	cond = cond.And("email", email)
	cond = cond.And("passwd", passwd)
	var qs orm.QuerySeter

	qs = o.QueryTable("user").SetCond(cond)
	_, err := qs.All(&users)
	return users, err
}
Example #6
0
// query objects and set to template
func (this *ModelAdminRouter) SetObjects(qs orm.QuerySeter, objects interface{}) error {
	cnt, err := qs.Count()
	if err != nil {
		return err
	}
	// create paginator
	p := this.SetPaginator(20, cnt)
	if cnt, err := qs.Limit(p.PerPageNums, p.Offset()).RelatedSel().All(objects); err != nil {
		return err
	} else {
		this.Data["Objects"] = objects
		this.Data["ObjectsCnt"] = cnt
	}
	return nil
}
Example #7
0
File: rdb.go Project: donh/query
func queryOneOrGetDefault(
	query orm.QuerySeter,
	resultHolder interface{}, defaultFunc func() interface{},
	nilValue interface{},
) (interface{}, error) {
	err := query.One(resultHolder)

	switch err {
	case nil:
		break
	case orm.ErrNoRows:
		return defaultFunc(), nil
	default:
		return nilValue, err
	}

	return resultHolder, nil
}
Example #8
0
func (this *TeamController) Teams() {
	query := strings.TrimSpace(this.GetString("query", ""))
	if utils.HasDangerousCharacters(query) {
		this.ServeErrJson("query is invalid")
		return
	}

	per := this.MustGetInt("per", 10)
	me := this.Ctx.Input.GetData("CurrentUser").(*User)

	var teams orm.QuerySeter
	if me.Role == ROOT_ADMIN_ROLE {
		teams = QueryAllTeams(query)
	} else {
		var err error
		teams, err = QueryMineTeams(query, me.Id)
		if err != nil {
			this.ServeErrJson("occur error " + err.Error())
			return
		}
	}

	total, err := teams.Count()
	if err != nil {
		this.ServeErrJson("occur error " + err.Error())
		return
	}

	pager := this.SetPaginator(per, total)
	teams = teams.Limit(per, pager.Offset())

	var ts []Team
	_, err = teams.All(&ts)
	if err != nil {
		this.ServeErrJson("occur error " + err.Error())
		return
	}

	nteams := make([]map[string]interface{}, 0)
	for _, v := range ts {
		cu := ReadUserById(v.Creator)
		t := make(map[string]interface{})
		t["Id"] = v.Id
		t["Name"] = v.Name
		t["Resume"] = v.Resume
		t["CreatorCnname"] = cu.Cnname
		t["CreatorName"] = cu.Name
		t["IsAdmin"] = (v.IsAdmin(me.Id) || me.Role == ROOT_ADMIN_ROLE)
		nteams = append(nteams, t)
	}

	this.Data["Teams"] = nteams
	this.Data["Query"] = query
	this.Data["Me"] = me
	this.Data["IamRoot"] = me.Role == ROOT_ADMIN_ROLE
	this.TplName = "team/list.html"
}
Example #9
0
// 检查结果集是否存在
func CheckIsExist(qs orm.QuerySeter, field string, value interface{}, skipId int) bool {
	qs = qs.Filter(field, value)
	if skipId > 0 {
		qs = qs.Exclude("Id", skipId)
	}
	return qs.Exist()
}
Example #10
0
func (this *UserRouter) getFollows(user *models.User, following bool) []map[string]interface{} {
	limit := 20

	var qs orm.QuerySeter

	if following {
		qs = user.FollowingUsers()
	} else {
		qs = user.FollowerUsers()
	}

	nums, _ := qs.Count()

	pager := this.SetPaginator(limit, nums)

	qs = qs.Limit(limit, pager.Offset())

	var follows []*models.Follow

	if following {
		qs.RelatedSel("FollowUser").All(&follows, "FollowUser")
	} else {
		qs.RelatedSel("User").All(&follows, "User")
	}

	if len(follows) == 0 {
		return nil
	}

	ids := make([]int, 0, len(follows))
	for _, follow := range follows {
		if following {
			ids = append(ids, follow.FollowUser.Id)
		} else {
			ids = append(ids, follow.User.Id)
		}
	}

	var eids orm.ParamsList
	this.User.FollowingUsers().Filter("FollowUser__in", ids).ValuesFlat(&eids, "FollowUser__Id")

	var fids map[int]bool
	if len(eids) > 0 {
		fids = make(map[int]bool)
		for _, id := range eids {
			tid, _ := utils.StrTo(utils.ToStr(id)).Int()
			if tid > 0 {
				fids[tid] = true
			}
		}
	}

	users := make([]map[string]interface{}, 0, len(follows))
	for _, follow := range follows {
		IsFollowed := false
		var u *models.User
		if following {
			u = follow.FollowUser
		} else {
			u = follow.User
		}
		if fids != nil {
			IsFollowed = fids[u.Id]
		}
		users = append(users, map[string]interface{}{
			"User":       u,
			"IsFollowed": IsFollowed,
		})
	}

	return users
}