Пример #1
0
func (this *PostRouter) SingleSubmit() {
	this.TplNames = "post/post.html"

	if this.CheckActiveRedirect() {
		return
	}

	var post models.Post
	if this.loadPost(&post, nil) {
		return
	}

	var redir bool

	defer func() {
		if !redir {
			var comments []*models.Comment
			this.loadComments(&post, &comments)
		}
	}()

	form := models.CommentForm{}
	if !this.ValidFormSets(&form) {
		return
	}

	comment := models.Comment{}
	if err := form.SaveComment(&comment, &this.user, &post); err == nil {
		this.JsStorage("deleteKey", "post/comment")
		this.Redirect(post.Link(), 302)
		redir = true

		models.PostReplysCount(&post)
	}
}
Пример #2
0
func (this *PostRouter) loadComments(post *models.Post, comments *[]*models.Comment) {
	qs := post.Comments()
	if num, err := qs.RelatedSel("User").OrderBy("Id").All(comments); err == nil {
		this.Data["Comments"] = *comments
		this.Data["CommentsNum"] = num
	}
}
Пример #3
0
// view for new object save
func (this *PostAdminRouter) Save() {
	form := this.GetForm(true)
	if !this.ValidFormSets(&form) {
		return
	}

	var post models.Post
	form.SetToPost(&post)
	if err := post.Insert(); err == nil {
		this.FlashRedirect(fmt.Sprintf("/admin/post/%d", post.Id), 302, "CreateSuccess")
		return
	} else {
		beego.Error(err)
		this.Data["Error"] = err
	}
}
Пример #4
0
func (this *PostRouter) NewSubmit() {
	this.TplNames = "post/new.html"

	if this.CheckActiveRedirect() {
		return
	}

	if this.IsAjax() {
		result := map[string]interface{}{
			"success": false,
		}
		action := this.GetString("action")
		switch action {
		case "preview":
			content := this.GetString("content")
			result["preview"] = models.RenderPostContent(content)

			models.FilterMentions(&this.user, models.RenderPostContent(content))
			result["success"] = true
		}
		this.Data["json"] = result
		this.ServeJson()
		return
	}

	form := models.PostForm{Locale: this.Locale}
	slug := this.GetString("topic")
	if len(slug) > 0 {
		topic := models.Topic{Slug: slug}
		topic.Read("Slug")
		form.Topic = topic.Id
		this.Data["Topic"] = &topic
	}

	models.ListCategories(&form.Categories)
	models.ListTopics(&form.Topics)
	if !this.ValidFormSets(&form) {
		return
	}

	var post models.Post
	if err := form.SavePost(&post, &this.user); err == nil {
		this.JsStorage("deleteKey", "post/new")
		this.Redirect(post.Link(), 302)
	}
}
Пример #5
0
func (this *PostRouter) EditSubmit() {
	this.TplNames = "post/edit.html"

	if this.CheckActiveRedirect() {
		return
	}

	var post models.Post
	if this.loadPost(&post, &this.user) {
		return
	}

	if this.IsAjax() {
		result := map[string]interface{}{
			"success": false,
		}
		action := this.GetString("action")
		switch action {
		case "preview":
			content := this.GetString("content")
			result["preview"] = models.RenderPostContent(content)
			result["success"] = true
		}
		this.Data["json"] = result
		this.ServeJson()
		return
	}

	form := models.PostForm{}
	form.SetFromPost(&post)
	models.ListCategories(&form.Categories)
	models.ListTopics(&form.Topics)
	if !this.ValidFormSets(&form) {
		return
	}

	if err := form.UpdatePost(&post, &this.user); err == nil {
		this.JsStorage("deleteKey", "post/edit")
		this.Redirect(post.Link(), 302)
	}
}
Пример #6
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
				}
			}
		}
	}
}