// Get implemented Get method for HomeRouter. func (this *PostListRouter) TopicSubmit() { slug := this.GetString(":slug") topic := models.Topic{Slug: slug} if err := topic.Read("Slug"); err != nil { this.Abort("404") return } result := map[string]interface{}{ "success": false, } if this.IsAjax() { action := this.GetString("action") switch action { case "favorite": if this.isLogin { qs := models.FollowTopics().Filter("User", &this.user).Filter("Topic", &topic) if qs.Exist() { qs.Delete() } else { fav := models.FollowTopic{User: &this.user, Topic: &topic} fav.Insert() } topic.RefreshFollowers() this.user.RefreshFavTopics() result["success"] = true } } } this.Data["json"] = result this.ServeJson() }
// view for new object save func (this *TopicAdminRouter) Save() { form := models.TopicAdminForm{Create: true} if this.ValidFormSets(&form) == false { return } var topic models.Topic form.SetToTopic(&topic) if err := topic.Insert(); err == nil { this.FlashRedirect(fmt.Sprintf("/admin/topic/%d", topic.Id), 302, "CreateSuccess") return } else { beego.Error(err) this.Data["Error"] = err } }
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) } }
func (this *PostRouter) New() { this.TplNames = "post/new.html" if this.CheckActiveRedirect() { return } form := models.PostForm{Locale: this.Locale} form.Lang = this.Locale.Index() 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) this.SetFormSets(&form) }
// Get implemented Get method for HomeRouter. func (this *PostListRouter) Topic() { slug := this.GetString(":slug") switch slug { default: // View topic. this.TplNames = "post/topic.html" topic := models.Topic{Slug: slug} if err := topic.Read("Slug"); err != nil { this.Abort("404") return } pers := 25 qs := models.Posts().Filter("Topic", &topic) qs = this.postsFilter(qs) cnt, _ := models.CountObjects(qs) pager := this.SetPaginator(pers, cnt) qs = qs.OrderBy("-Created").Limit(pers, pager.Offset()).RelatedSel() var posts []models.Post models.ListObjects(qs, &posts) this.Data["Posts"] = posts this.Data["Topic"] = &topic this.Data["IsTopic"] = true HasFavorite := false if this.isLogin { HasFavorite = models.FollowTopics().Filter("User", &this.user).Filter("Topic", &topic).Exist() } this.Data["HasFavorite"] = HasFavorite } }