Beispiel #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
}
Beispiel #2
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)
}
Beispiel #3
0
func TopHandler(res *Response, req *Request) {
	if !IsGuest(req.User) {
		driver := models.NewBlogDriver(gae.NewContext(req), req.Logger)
		blogs, err := driver.FindBlogsByOwner(req.User.Id())
		if err != nil {
			res.RenderInternalError(err.Error())
			return
		}
		if len(blogs) != 0 {
			res.SetLocal("blogs", blogs)
		} else {
			res.SetLocal("no_blogs", true)
		}
	}
	res.SetLocal("js", "/static/js/index.js")
	res.Templates("index.html", "header.html", "footer.html")
}
Beispiel #4
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)
}
Beispiel #5
0
func myblogRedirector(public bool) func(res *wcg.Response, req *wcg.Request) {
	return func(res *wcg.Response, req *wcg.Request) {
		if wcg.IsGuest(req.User) {
			res.Redirect("/", http.StatusSeeOther)
		} else {
			driver := models.NewBlogDriver(gae.NewContext(req), req.Logger)
			blogs, err := driver.FindBlogsByOwner(req.User.Id())
			if err != nil {
				res.RenderInternalError(err.Error())
				return
			}
			if len(blogs) > 0 {
				blog := blogs[0]
				if public {
					res.Redirect(fmt.Sprintf("/%s/", blog.Id), http.StatusSeeOther)
				} else {
					res.Redirect(fmt.Sprintf("/admin/blogs/%s/", blog.Id), http.StatusSeeOther)
				}
			} else {
				res.Redirect("/", http.StatusSeeOther)
			}
		}
	}
}