func (this *PostRouter) Post() { if this.CheckActiveRedirect() { return } var postMd models.Post if this.loadPost(&postMd, &this.User) { return } if !postMd.CanEdit { this.FlashRedirect(postMd.Path(), 302, "CanNotEditPost") } form := post.PostForm{} form.SetFromPost(&postMd) models.FindTopics(&form.Topics) if !this.ValidFormSets(&form) { return } if err := form.UpdatePost(&postMd, &this.User); err == nil { this.JsStorage("deleteKey", "post/edit") this.Redirect(postMd.Link()) return } this.Render("post/edit.html", this.Data) }
func (this *NewPost) Get() error { if this.CheckActiveRedirect() { return nil } form := post.PostForm{Locale: this.Locale} topicSlug := this.GetString("topic") if len(topicSlug) > 0 { topic, err := models.GetTopicBySlug(topicSlug) if err != nil { this.Redirect(setting.AppUrl) return nil } form.Topic = topic.Id form.Category = topic.CategoryId err = models.FindTopicsByCategoryId(&form.Topics, topic.CategoryId) if err != nil { this.Redirect(setting.AppUrl) return nil } this.Data["Topic"] = topic } else { catSlug := this.GetString("category") if len(catSlug) > 0 { category, err := models.GetCategoryBySlug(catSlug) if err != nil { return err } form.Category = category.Id err = models.FindTopicsByCategoryId(&form.Topics, category.Id) if err != nil { return err } this.Data["Category"] = category } else { this.Redirect(setting.AppUrl) return nil } } this.SetFormSets(&form) return this.Render("post/new.html", this.Data) }
func (this *NewPost) Post() error { if this.CheckActiveRedirect() { return nil } var err error form := post.PostForm{Locale: this.Locale} topicSlug := this.GetString("topic") if len(topicSlug) > 0 { topic, err := models.GetTopicBySlug(topicSlug) if err == nil { form.Category = topic.CategoryId form.Topic = topic.Id this.Data["Topic"] = topic } else { log.Error("Can not find topic by slug:", topicSlug) } } else { topicId, err := this.GetInt("Topic") if err == nil { topic, err := models.GetTopicById(topicId) if err == nil { form.Category = topic.CategoryId form.Topic = topic.Id this.Data["Topic"] = topic } else { log.Error("Can not find topic by id:", topicId) } } else { log.Error("Parse param Topic from request failed", err) } } if categorySlug := this.GetString("category"); categorySlug != "" { log.Debug("Find category slug:", categorySlug) category, err := models.GetCategoryBySlug(categorySlug) if err != nil { log.Error("Get category error", err) } this.Data["Category"] = &category } err = models.FindTopics(&form.Topics) if err != nil { return err } if !this.ValidFormSets(&form) { this.Redirect("/new") return nil } var post models.Post if err := form.SavePost(&post, &this.User); err == nil { this.JsStorage("deleteKey", "post/new") this.Redirect(post.Link()) return nil } return this.Render("post/new.html", this.Data) }
func (this *EditPost) Get() { if this.CheckActiveRedirect() { return } var postMd models.Post if this.loadPost(&postMd, &this.User) { return } if !postMd.CanEdit { this.Redirect(postMd.Link()) return } form := post.PostForm{} form.SetFromPost(&postMd) models.FindTopics(&form.Topics) this.SetFormSets(&form) this.Render("post/edit.html", this.Data) }