Esempio n. 1
0
func fetchBlog(res *wcg.Response, req *wcg.Request, includePosts bool) (*models.Blog, *supports.Page, error) {
	// default query for posts.
	id := req.Param("blog_id")
	driver := models.NewBlogDriver(gae.NewContext(req), req.Logger)
	blog, query, err := driver.PostQuery(id)
	if err != nil {
		if err == models.ErrBlogNotFound {
			res.WriteHeader(404)
			res.WriteString("Not found")
			res.End()
			return nil, nil, err
		}
		res.RenderInternalError(err.Error())
		return nil, nil, err
	}

	if !includePosts {
		return blog, supports.EmptyPage, nil
	}
	query = query.Filter("IsNew =", false).Filter("IsDraft =", false).Order("-PostDate").Order("-UpdatedAt")
	per_page := wcg.ParseInt(req.Query("num"), AppConfig.DefaultPostsFetched, 0, AppConfig.MaxPostsFetched)
	current := wcg.ParseInt(req.Query("p"), 0, 0, wcg.ParseIntMax)
	page, err := supports.NewPage(current, per_page, query)
	if err != nil {
		res.RenderInternalError("Post pagination error: %v", err)
		return nil, nil, err
	}
	return blog, page, nil
}
Esempio n. 2
0
func fetchPost(res *wcg.Response, req *wcg.Request, blog *models.Blog) (*models.Post, error) {
	driver := models.NewPostDriver(gae.NewContext(req), req.Logger)
	id := req.Param("post_id")
	blogId := req.Param("blog_id")
	post, err := driver.FindPostById(id, blogId)
	if err != nil {
		if err == models.ErrPostNotFound {
			res.WriteHeader(404)
			res.WriteString("Not found")
			res.End()
			return nil, err
		}
		res.RenderInternalError(err.Error())
		return nil, err
	}
	if post.PostState() != models.PostStatePublished {
		if post.OwnerId != req.User.Id() {
			res.WriteHeader(404)
			res.WriteString("Not found")
			res.End()
			return nil, err
		}
	}
	return post, err
}
Esempio n. 3
0
func fetchPostForUpdate(req *wcg.Request, res *wcg.Response, blog *models.Blog) (*models.Post, error) {
	driver := models.NewPostDriver(gae.NewContext(req), req.Logger)
	id := req.Param("post_id")
	blogId := req.Param("blog_id")
	post, err := driver.FindPostById(id, blogId)
	if err != nil {
		if err == models.ErrPostNotFound {
			res.WriteHeader(404)
			res.WriteString("Not found")
			res.End()
			return nil, err
		}
		res.RenderInternalError(err.Error())
		return nil, err
	}
	if blog.Id != post.BlogId {
		res.WriteHeader(403)
		res.WriteString("You could not manage that blog.")
		res.End()
	}

	if post.OwnerId != req.User.Id() {
		res.WriteHeader(403)
		res.WriteString("You could not manage this post.")
		res.End()
		return nil, err
	}
	return post, err
}
Esempio n. 4
0
func createPostHandler(res *wcg.Response, req *wcg.Request) {
	blog, _, err := fetchBlogForUpdate(res, req, false)
	if err != nil {
		return
	}
	post := models.NewPost(req.User.Id(), blog.Id)
	err = models.NewPostDriver(gae.NewContext(req), req.Logger).SavePost(post)
	if err != nil {
		res.RenderInternalError(err.Error())
		return
	}
	res.Redirect(fmt.Sprintf("/admin/blogs/%s/%s/", blog.Id, post.Id), http.StatusSeeOther)
}
Esempio n. 5
0
func deleteBlogHandler(res *wcg.Response, req *wcg.Request) {
	driver := models.NewBlogDriver(gae.NewContext(req), req.Logger)
	err := driver.DeleteBlog(req.Param("blog_id"), req.User.Id())
	if err != nil {
		if supports.IsValidationError(err) {
			res.WriteJsonWithStatus(400, nil, err)
		} else if err == models.ErrBlogNotOwned {
			res.WriteJsonWithStatus(403, nil, no_permission)
		} else if err == models.ErrBlogNotFound {
			res.WriteJsonWithStatus(404, nil, not_found)
		} else {
			res.RenderInternalError(err.Error())
		}
		return
	}
	res.WriteJsonWithStatus(200, nil, ok)
}
Esempio n. 6
0
func deletePostHandler(res *wcg.Response, req *wcg.Request) {
	blog, _, err := fetchBlogForUpdate(res, req, false)
	if err != nil {
		return
	}
	post, err := fetchPostForUpdate(req, res, blog)
	if err != nil {
		return
	}
	err = models.NewPostDriver(gae.NewContext(req), req.Logger).DeletePost(post)
	if err != nil {
		if supports.IsValidationError(err) {
			res.WriteJsonWithStatus(400, nil, err)
		} else {
			res.RenderInternalError(err.Error())
		}
		return
	}
	res.WriteJsonWithStatus(200, nil, ok)
}
Esempio n. 7
0
func updatePostHandler(res *wcg.Response, req *wcg.Request) {
	blog, _, err := fetchBlogForUpdate(res, req, false)
	if err != nil {
		return
	}
	post, err := fetchPostForUpdate(req, res, blog)
	if err != nil {
		return
	}
	post.Title = req.Form("title")
	post.Content = req.Form("content")
	post.IsDraft = req.Form("is_draft") == "true"
	post.IsNew = false
	post.PostDate, err = time.Parse(wcg.FormDateFormat, req.Form("post_date"))
	post.Tags = strings.Split(req.Form("tags"), ",")
	for i, v := range post.Tags {
		post.Tags[i] = strings.TrimSpace(v)
	}

	if err != nil {
		res.WriteHeader(400)
		res.WriteString("Invalid date format.")
		res.End()
	}
	driver := models.NewPostDriver(gae.NewContext(req), req.Logger)
	if AppConfig.GithubMarkdown {
		driver.HttpClient = gae.NewHttpClient(req)
	}
	err = driver.SavePost(post)
	if err != nil {
		if supports.IsValidationError(err) {
			res.WriteJsonWithStatus(400, nil, err)
		} else {
			res.RenderInternalError(err.Error())
		}
		return
	}

	req.Logger.Info("A post is created at %s.", post.Id)
	res.WriteJsonWithStatus(200, nil, ok)
}
Esempio n. 8
0
func createBlogHandler(res *wcg.Response, req *wcg.Request) {
	driver := models.NewBlogDriver(gae.NewContext(req), req.Logger)
	blog, err := driver.CreateBlog(
		req.Form("path"),
		req.Form("title"),
		req.Form("description"),
		req.User.Id(),
	)
	if err != nil {
		if err == models.ErrBlogAlreadyExists {
			res.WriteJsonWithStatus(409, nil, already_taken)
		} else if supports.IsValidationError(err) {
			res.WriteJsonWithStatus(400, nil, err)
		} else {
			res.RenderInternalError(err.Error())
		}
		return
	}
	res.WriteJsonWithStatus(201, map[string]string{
		"location": wcg.AbsoluteUrl(req, "/"+blog.Id),
	}, ok)
}