示例#1
0
func ArticlePermaLinkHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	vars := mux.Vars(r)
	id, err := strconv.ParseInt(vars["id"], 10, 64)
	if err != nil {
		core.HandleError(c, w, err)
		return
	}

	article, err := GetArticleById(c, id, true)
	if err != nil {
		core.HandleNotFound(c, w)
		return
	}

	if !article.IsPublic {
		user := auth.CurrentUser(c)
		if !user.IsAdmin {
			core.HandleAuthRequired(c, w)
			return
		}
	}

	redirectTo, err := article.URL()
	if err != nil {
		core.HandleNotFound(c, w)
		return
	}
	http.Redirect(w, r, redirectTo.Path, 302)
}
示例#2
0
func ArticleDeleteHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	user := core.AdminUser(c, w)
	if user == nil {
		return
	}

	vars := mux.Vars(r)
	id, err := strconv.ParseInt(vars["id"], 10, 64)
	if err != nil {
		core.HandleError(c, w, err)
		return
	}

	article, err := GetArticleById(c, id, false)
	if err != nil {
		core.HandleNotFound(c, w)
		return
	}

	err = DeleteArticle(c, article)
	if err != nil {
		core.HandleError(c, w, err)
		return
	}

	http.Redirect(w, r, "/", 302)
}
示例#3
0
func ArticleUpdateHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	user := core.AdminUser(c, w)
	if user == nil {
		return
	}

	vars := mux.Vars(r)
	id, err := strconv.ParseInt(vars["id"], 10, 64)
	if err != nil {
		core.HandleError(c, w, err)
		return
	}

	article, err := GetArticleById(c, id, false)
	if err != nil {
		core.HandleNotFound(c, w)
		return
	}

	form := NewArticleForm(article)

	if r.Method == "POST" {
		if err := r.ParseForm(); err != nil {
			core.HandleError(c, w, err)
			return
		}

		if gforms.IsFormValid(form, r.Form) {
			err := UpdateArticle(c, article,
				form.Title.Value(),
				form.Text.Value(),
				form.IsPublic.Value(),
			)
			if err != nil {
				core.HandleError(c, w, err)
				return
			}

			redirectTo, err := article.URL()
			if err != nil {
				core.HandleError(c, w, err)
				return
			}
			http.Redirect(w, r, redirectTo.Path, 302)
		}
	}

	context := map[string]interface{}{
		"article": article,
		"form":    form,
	}
	core.RenderTemplate(c, w, context,
		"templates/blog/articleUpdate.html", "templates/blog/articleForm.html", LAYOUT)
}
示例#4
0
func ArticleHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	user := auth.CurrentUser(c)

	vars := mux.Vars(r)
	id, err := strconv.ParseInt(vars["id"], 10, 64)
	if err != nil {
		core.HandleError(c, w, err)
		return
	}

	article, err := GetArticleById(c, id, !user.IsAdmin)
	if err != nil {
		core.HandleNotFound(c, w)
		return
	}

	if !article.IsPublic && !user.IsAdmin {
		core.HandleAuthRequired(c, w)
		return
	}

	viewedArticles := make([]string, 0)
	if cookie, err := r.Cookie("viewedArticles"); err != http.ErrNoCookie {
		viewedArticles = strings.Split(cookie.Value, ",")
	}

	articleID := strconv.FormatInt(article.Key().IntID(), 32)
	if !isViewedArticle(viewedArticles, articleID) {
		viewedArticles = append(viewedArticles, articleID)
		http.SetCookie(w, &http.Cookie{
			Name:    "viewedArticles",
			Value:   strings.Join(viewedArticles, ","),
			Path:    "/",
			Expires: time.Now().Add(time.Duration(30*24) * time.Hour),
		})

		if err := ChangeArticleViewsCount(c, article.Key(), +1); err != nil {
			core.HandleError(c, w, err)
			return
		}
	}

	context := tmplt.Context{"article": article}
	core.RenderTemplate(c, w, context, "templates/blog/article.html", LAYOUT)
}