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 (form *PostAdminForm) SetToPost(post *models.Post) { utils.SetFormValues(form, post) post.UserId = form.User post.LastReplyId = form.LastReply post.LastAuthorId = form.LastAuthor post.TopicId = form.Topic //get category if topic, err := models.GetTopicById(form.Topic); err == nil { post.CategoryId = topic.CategoryId } post.ContentCache = utils.RenderMarkdown(post.Content) }
func (form *PostAdminForm) Valid(v *validation.Validation) { var err error if _, err = models.GetUserById(form.User); err != nil { v.SetError("User", "admin.not_found_by_id") } if _, err = models.GetUserById(form.LastReply); err != nil { v.SetError("LastReply", "admin.not_found_by_id") } if _, err = models.GetUserById(form.LastAuthor); err != nil { v.SetError("LastReply", "admin.not_found_by_id") } if _, err = models.GetTopicById(form.Topic); err != nil { v.SetError("Topic", "admin.not_found_by_id") } if len(i18n.GetLangByIndex(form.Lang)) == 0 { v.SetError("Lang", "Not Found") } }