Example #1
0
// query object and set to template
func (this *ModelAdminRouter) QueryObject() bool {
	id, _ := utils.StrTo(this.GetString(":id")).Int()
	if id <= 0 {
		this.Abort("404")
		return false
	}

	var app ModelFinder
	if a, ok := this.AppController.(ModelFinder); ok {
		app = a
	} else {
		panic("ModelAdmin AppController need implement ModelFinder")
	}

	object := app.Object()
	qs := app.ObjectQs()

	// query object
	if err := qs.Filter("Id", id).Limit(1).One(object); err != nil {
		this.Abort("404")
		if err != orm.ErrNoRows {
			beego.Error("SetObject: ", err)
		}
		return false

	} else {
		this.Data["Object"] = object
	}

	return true
}
Example #2
0
func (m *Image) DecodeToken(token string) error {
	number := utils.NumberDecode(token, setting.ImageLinkAlphabets)
	if len(number) < 9 {
		return fmt.Errorf("token `%s` too short <- `%s`", token, number)
	}

	if t, err := beego.DateParse(number[:8], "ymds"); err != nil {
		return fmt.Errorf("token `%s` date parse error <- `%s`", token, number)
	} else {
		m.Created = t
	}

	var err error
	m.Id, err = utils.StrTo(number[8:]).Int()
	if err != nil {
		return fmt.Errorf("token `%s` id parse error <- `%s`", token, err)
	}

	return nil
}
Example #3
0
func (this *ApiRouter) Users() {
	result := map[string]interface{}{
		"success": false,
	}

	defer func() {
		this.Data["json"] = result
		this.ServeJson()
	}()

	if !this.IsAjax() {
		return
	}

	action := this.GetString("action")

	if this.IsLogin {

		switch action {
		case "get-follows":
			var data []orm.ParamsList
			this.User.FollowingUsers().ValuesList(&data, "FollowUser__NickName", "FollowUser__UserName")
			result["success"] = true
			result["data"] = data

		case "follow", "unfollow":
			id, err := utils.StrTo(this.GetString("user")).Int()
			if err == nil && id != this.User.Id {
				fuser := models.User{Id: id}
				if action == "follow" {
					auth.UserFollow(&this.User, &fuser)
				} else {
					auth.UserUnFollow(&this.User, &fuser)
				}
				result["success"] = true
			}
		}
	}
}
Example #4
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
}