Exemple #1
0
func (this *AuthorController) ByName() {
	this.TplName = "author.tpl"

	author_name := this.Ctx.Input.Param(":name")

	if post_count, err := models.NewPostModel().Count("Author__AuthorName", author_name); err == nil {
		paginator := pagination.SetPaginator(this.Ctx, postsPerPage, post_count)
		if author, err := models.NewAuthorModel().ByName(author_name); err == nil {
			this.Data["Author"] = author
			posts, err := models.NewPostModel().ByAuthorId(author.AuthorId, "-PostId", paginator.Offset(), postsPerPage, false, true, true)
			if err != nil {
				panic(err)
			}

			this.SetPostViews(posts)
			this.LoadSidebar([]string{"LatestComments", "MostPopular"})

			this.Data["Posts"] = posts
			this.Data["PageTitle"] = fmt.Sprintf("%s - Author - %s", author.DisplayName, blogTitle)
		} else {
			this.Abort("404")
		}
	} else {
		panic(err)
	}
}
Exemple #2
0
func (this *AdminController) AllPosts() {
	this.TplName = "admin-posts.tpl"
	this.Data["PageTitle"] = fmt.Sprintf("All Posts - %s - Admin - %s", this.loggedUser.DisplayName, blogTitle)

	post_count, err := models.NewPostModel().Count("", nil, true)
	if err != nil {
		panic(err)
	}
	paginator := pagination.SetPaginator(this.Ctx, postsPerPage, post_count)
	posts, _ := models.NewPostModel().Offset("", nil, "-PostId", paginator.Offset(), postsPerPage, true, false, true)
	this.Data["Posts"] = posts

	views := make(map[int]int)
	bot_views := make(map[int]int)
	for _, post := range posts {
		for _, view := range post.PostViews {
			if view.ViewedBy == "human" {
				views[post.PostId] = view.Views
			} else if view.ViewedBy == "bot" {
				bot_views[post.PostId] = view.Views
			}
		}
	}
	this.Data["Views"] = views
	this.Data["BotViews"] = bot_views
}
Exemple #3
0
func (this *FeedController) RSS() {
	posts, err := models.NewPostModel().Offset("", nil, "-PostId", 0, 10)
	if err != nil {
		panic(err)
	}
	var rss_posts []*models.RSSPost
	for _, post := range posts {
		rss_post := models.NewRSSPost(post)
		rss_post.PostLink = blogUrl + rss_post.PostLink
		rss_post.GUID = rss_post.PostLink
		rss_posts = append(rss_posts, rss_post)
	}
	rss := models.NewRSSFeed()
	channel := models.NewRSSChannel()
	channel.PubDate = time.Now().Format("Mon, 02 Jan 2006 15:04:05 MST")
	channel.ChannelTitle = blogTitle
	channel.ChannelLink = blogUrl
	channel.ChannelDesc = blogDesc
	channel.RSSPosts = rss_posts
	channel.Self = models.NewRSSSelf(blogUrl + "/rss")
	rss.Channel = channel

	this.Data["xml"] = rss
	this.ServeXML()
}
Exemple #4
0
func (this *AdminController) Prepare() {
	this.BaseController.Prepare()
	this.CheckLogged()

	post_count, _ := models.NewPostModel().Count("", nil)
	this.Data["PostCount"] = post_count
}
Exemple #5
0
func (this *PostController) ById() {
	id_str := this.Ctx.Input.Param(":id")
	post_id, _ := strconv.Atoi(id_str)
	post, err := models.NewPostModel().ById(post_id)
	if err != nil {
		this.Abort("404")
	}
	this.Redirect(post.PostLink(), 301)
}
Exemple #6
0
func (this *MainController) Get() {
	post_count, err := models.NewPostModel().Count("", "")
	if err != nil {
		panic(err)
	}
	paginator := pagination.SetPaginator(this.Ctx, postsPerPage, post_count)

	posts, err := models.NewPostModel().Offset("", nil, "-PostId", paginator.Offset(), postsPerPage, false, true, true)
	if err != nil {
		panic(err)
	}
	this.Data["Posts"] = posts

	this.SetPostViews(posts)
	this.LoadSidebar([]string{"LatestComments", "MostPopular", "Links", "TagCloud"})

	this.Data["PageTitle"] = fmt.Sprintf("%s %s", blogTitle, blogDesc)
	this.TplName = "home.tpl"
}
Exemple #7
0
func (this *ErrorController) Error404() {
	post_count, err := models.NewPostModel().Count("", "")
	if err != nil {
		panic(err)
	}
	paginator := pagination.SetPaginator(this.Ctx, postsPerPage, post_count)

	posts, err := models.NewPostModel().Offset("", nil, "-PostId", paginator.Offset(), postsPerPage, false, true, true)
	if err != nil {
		panic(err)
	}

	this.SetPostViews(posts)
	this.LoadSidebar([]string{"LatestComments", "MostPopular"})

	this.Data["Posts"] = posts
	this.Data["PageTitle"] = blogTitle
	this.Data["Content"] = "404 - 您要找的页面不见啦,不如看看其它内容吧"
	this.TplName = "404.tpl"
}
Exemple #8
0
func (this *TagController) ByName() {
	this.TplName = "home.tpl"
	tag_name := this.Ctx.Input.Param(":tag")

	post_count, err := models.NewPostModel().Count("Tags__Tag__TagSlug", tag_name)
	if err != nil {
		panic(err)
	}
	paginator := pagination.SetPaginator(this.Ctx, postsPerPage, post_count)

	posts, err := models.NewPostModel().ByTagName(tag_name, "-PostId", paginator.Offset(), postsPerPage, false, true, true)
	if err != nil {
		this.Abort("404")
	}
	this.Data["Posts"] = posts

	this.SetPostViews(posts)
	this.LoadSidebar([]string{"LatestComments", "MostPopular"})

	if tag, err := models.NewTagModel().BySlug(tag_name); err == nil {
		this.Data["Content"] = fmt.Sprintf("Tag - %s", tag.TagName)
		this.Data["PageTitle"] = fmt.Sprintf("%s - Tag - %s", tag.TagName, blogTitle)
	}
}
Exemple #9
0
func (this *AdminController) DoPostEdit() {
	id := this.GetString("post-id")
	post_id, err := strconv.Atoi(id)
	if err != nil {
		panic(err)
	}
	post_title := this.GetString("post-title")
	post_slug := this.GetString("post-slug")
	_, err = strconv.Atoi(post_slug)
	if err == nil {
		panic(errors.New("error: post slug should not be a number"))
	}
	post_content := this.GetString("post-content")
	post_content_md := this.GetString("post-content-md")
	if post_title == "" || post_content == "" || post_content_md == "" || post_slug == "" {
		panic(errors.New("error: empty post title, slug or content"))
	}
	comment_status, err := this.GetInt("comment-status", 1)
	if err != nil {
		panic(err)
	}
	post_tags := this.Input()["post-tags"]
	var tags []*models.Tag
	for _, t := range post_tags {
		t = strings.TrimSpace(t)
		tag := &models.Tag{
			TagName: t,
			TagSlug: strings.ToLower(t),
		}
		tags = append(tags, tag)
	}

	post := models.NewPost()
	post.PostId = post_id
	post.CommentStatus = comment_status
	post.PostTitle = post_title
	post.PostSlug = post_slug
	post.PostContent = post_content
	post.PostContentMd = post_content_md
	post.PostModifiedTime = time.Now().Format("2006-01-02 15:04:05")
	post.Tags = tags

	err = models.NewPostModel().PostEdit(post)
	if err != nil {
		panic(err)
	}
	this.Redirect(post.PostLink(), 302)
}
Exemple #10
0
func (this *PostController) BySlug() {
	post_slug := this.Ctx.Input.Param(":slug")
	post_model := models.NewPostModel()
	post, err := post_model.BySlug(post_slug)
	if err != nil {
		this.Abort("404")
	}
	this.setPost(post)

	ua := user_agent.New(this.Ctx.Request.UserAgent())
	if ua.Bot() {
		post_model.ViewedBy(post.PostId, "bot")
	} else {
		post_model.ViewedBy(post.PostId, "human")
	}
}
Exemple #11
0
func (this *AdminController) PostEdit() {
	this.TplName = "admin-post.tpl"
	id := this.Ctx.Input.Param(":id")
	post_id, _ := strconv.Atoi(id)

	post, err := models.NewPostModel().ById(post_id, true)
	if err != nil {
		panic(err)
	} else if post.Author.AuthorId != this.loggedUser.AuthorId {
		panic(errors.New("error: can't edit another one's post"))
	}

	tags, _ := models.NewTagModel().AllTags()
	this.Data["Post"] = post
	this.Data["AllTags"] = tags
	this.Data["PageTitle"] = fmt.Sprintf("Editing Post: %s - %s", post.PostTitle, blogTitle)
}
Exemple #12
0
func (this *AdminController) PostRestore() {
	id := this.Ctx.Input.Param(":id")
	post_id, err := strconv.Atoi(id)
	if err != nil {
		panic(err)
	}
	post, err := models.NewPostModel().ById(post_id, true)
	if err != nil {
		panic(err)
	}
	post.PostStatus = 0
	err = post.Update("PostStatus")
	if err != nil {
		panic(err)
	}
	this.Redirect("/admin", 302)
}
Exemple #13
0
func (this *BaseController) LoadSidebar(widgets []string) {
	for _, widget := range widgets {
		if widget == "LatestComments" {
			if latest_comments, err := models.NewCommentModel().Latest(10); err == nil {
				this.Data["LatestComments"] = latest_comments
			}
		} else if widget == "MostPopular" {
			if posts, err := models.NewPostModel().MostPopular(10); err == nil {
				this.Data["MostPopular"] = posts
			}
		} else if widget == "Links" {
			if links, err := models.NewLinkModel().AllLinks(); err == nil {
				this.Data["Links"] = links
			}
		} else if widget == "TagCloud" {
			if tags, err := models.NewTagModel().MostPopular(20); err == nil {
				this.Data["TagCloud"] = tags
			}
		}
	}
}