代码拉取完成,页面将自动刷新
package gopher
import (
"net/http"
"github.com/gorilla/mux"
"github.com/jimmykuu/wtforms"
"gopkg.in/mgo.v2/bson"
)
// URL: /admin/package_categories
// 列出所有的包分类
func adminListPackageCategoriesHandler(handler *Handler) {
var categories []PackageCategory
c := handler.DB.C(PACKAGE_CATEGORIES)
c.Find(nil).All(&categories)
handler.renderTemplate("admin/package_categories.html", ADMIN, map[string]interface{}{"categories": categories})
}
// URL: /admin/package_category/new
// 新建包分类
func adminNewPackageCategoryHandler(handler *Handler) {
form := wtforms.NewForm(
wtforms.NewTextField("id", "ID", "", wtforms.Required{}),
wtforms.NewTextField("name", "名称", "", wtforms.Required{}),
)
if handler.Request.Method == "POST" {
if !form.Validate(handler.Request) {
handler.renderTemplate("package_category/form.html", ADMIN, map[string]interface{}{"form": form})
return
}
c := handler.DB.C(PACKAGE_CATEGORIES)
var category PackageCategory
err := c.Find(bson.M{"name": form.Value("name")}).One(&category)
if err == nil {
form.AddError("name", "该名称已经有了")
handler.renderTemplate("package_category/form.html", ADMIN, map[string]interface{}{"form": form})
return
}
err = c.Insert(&PackageCategory{
Id_: bson.NewObjectId(),
Id: form.Value("id"),
Name: form.Value("name"),
})
if err != nil {
panic(err)
}
http.Redirect(handler.ResponseWriter, handler.Request, "/admin/package_category/new", http.StatusFound)
}
handler.renderTemplate("package_category/form.html", ADMIN, map[string]interface{}{
"form": form,
"isNew": true,
})
}
// URL: /admin/package_category/{id}/edit
// 修改包分类
func adminEditPackageCategoryHandler(handler *Handler) {
id := mux.Vars(handler.Request)["id"]
c := handler.DB.C(PACKAGE_CATEGORIES)
var category PackageCategory
c.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&category)
form := wtforms.NewForm(
wtforms.NewTextField("id", "ID", category.Id, wtforms.Required{}),
wtforms.NewTextField("name", "名称", category.Name, wtforms.Required{}),
)
if handler.Request.Method == "POST" {
if !form.Validate(handler.Request) {
handler.renderTemplate("package_category/form.html", ADMIN, map[string]interface{}{"form": form})
return
}
c.Update(bson.M{"_id": bson.ObjectIdHex(id)}, bson.M{"$set": bson.M{
"id": form.Value("id"),
"name": form.Value("name"),
}})
http.Redirect(handler.ResponseWriter, handler.Request, "/admin/package_categories", http.StatusFound)
}
handler.renderTemplate("package_category/form.html", ADMIN, map[string]interface{}{
"form": form,
"isNew": false,
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。