Ejemplo n.º 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
}
Ejemplo n.º 2
0
func (this *ApiRouter) Post() {
	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 && this.user.IsAdmin {

		switch action {
		case "toggle-best":
			id, _ := utils.StrTo(this.GetString("post")).Int()
			if id > 0 {
				post := models.Post{Id: id}
				if err := post.Read(); err == nil {
					post.IsBest = !post.IsBest
					post.Update("IsBest")
					result["success"] = true
					result["isBest"] = post.IsBest
				}
			}
		}
	}
}
Ejemplo n.º 3
0
func (form *UserAdminForm) Valid(v *validation.Validation) {
	qs := Users()

	if CheckIsExist(qs, "UserName", form.UserName, form.Id) {
		v.SetError("UserName", "auth.username_already_taken")
	}

	if CheckIsExist(qs, "Email", form.Email, form.Id) {
		v.SetError("Email", "auth.email_already_taken")
	}

	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "Can not be empty")
	}

	if len(form.LangAdds) > 0 {
		adds := make(SliceStringField, 0, len(form.LangAdds))
		for _, l := range form.LangAdds {
			if d, err := utils.StrTo(l).Int(); err == nil {
				if form.Lang == d {
					continue
				}
				if len(i18n.GetLangByIndex(form.Lang)) == 0 {
					v.SetError("Lang", "Can not be empty")
					return
				}
				adds = append(adds, l)
			}
		}
		form.LangAdds = adds
	}
}
Ejemplo n.º 4
0
func (m *Image) DecodeToken(token string) error {
	number := utils.NumberDecode(token, utils.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
}
Ejemplo n.º 5
0
func (this *ApiRouter) User() {
	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" {
					models.UserFollow(&this.user, &fuser)
				} else {
					models.UserUnFollow(&this.user, &fuser)
				}
				result["success"] = true
			}
		}
	}
}
Ejemplo n.º 6
0
func (form *ProfileForm) Valid(v *validation.Validation) {
	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "Can not be empty")
	}

	if len(form.LangAdds) > 0 {
		adds := make(SliceStringField, 0, len(form.LangAdds))
		for _, l := range form.LangAdds {
			if d, err := utils.StrTo(l).Int(); err == nil {
				if form.Lang == d {
					continue
				}
				if len(i18n.GetLangByIndex(form.Lang)) == 0 {
					v.SetError("Lang", "Can not be empty")
					return
				}
				adds = append(adds, l)
			}
		}
		form.LangAdds = adds
	}
}