當前位置: 首頁>>代碼示例>>Golang>>正文


Golang wtforms.NewTextArea函數代碼示例

本文整理匯總了Golang中github.com/jimmykuu/wtforms.NewTextArea函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewTextArea函數的具體用法?Golang NewTextArea怎麽用?Golang NewTextArea使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewTextArea函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: newPackageHandler

// URL: /package/new
// 新建第三方包
func newPackageHandler(handler *Handler) {
	user, _ := currentUser(handler)

	var categories []PackageCategory

	c := handler.DB.C(PACKAGE_CATEGORIES)
	c.Find(nil).All(&categories)

	var choices []wtforms.Choice

	for _, category := range categories {
		choices = append(choices, wtforms.Choice{Value: category.Id_.Hex(), Label: category.Name})
	}

	form := wtforms.NewForm(
		wtforms.NewHiddenField("html", ""),
		wtforms.NewTextField("name", "名稱", "", wtforms.Required{}),
		wtforms.NewSelectField("category_id", "分類", choices, ""),
		wtforms.NewTextField("url", "網址", "", wtforms.Required{}, wtforms.URL{}),
		wtforms.NewTextArea("editormd-markdown-doc", "內容", ""),
		wtforms.NewTextArea("editormd-html-code", "HTML", ""),
	)

	if handler.Request.Method == "POST" && form.Validate(handler.Request) {
		c = handler.DB.C(CONTENTS)
		id := bson.NewObjectId()
		categoryId := bson.ObjectIdHex(form.Value("category_id"))
		html := form.Value("editormd-html-code")
		c.Insert(&Package{
			Content: Content{
				Id_:       id,
				Type:      TypePackage,
				Title:     form.Value("name"),
				Markdown:  form.Value("editormd-markdown-doc"),
				Html:      template.HTML(html),
				CreatedBy: user.Id_,
				CreatedAt: time.Now(),
			},
			Id_:        id,
			CategoryId: categoryId,
			Url:        form.Value("url"),
		})

		c = handler.DB.C(PACKAGE_CATEGORIES)
		// 增加數量
		c.Update(bson.M{"_id": categoryId}, bson.M{"$inc": bson.M{"packagecount": 1}})

		http.Redirect(handler.ResponseWriter, handler.Request, "/p/"+id.Hex(), http.StatusFound)
		return
	}
	handler.renderTemplate("package/form.html", BASE, map[string]interface{}{
		"form":   form,
		"title":  "提交第三方包",
		"action": "/package/new",
		"active": "package",
	})
}
開發者ID:ZuiGuangYin,項目名稱:gopher,代碼行數:59,代碼來源:package.go

示例2: profileHandler

// URL /profile
// 用戶設置頁麵,顯示用戶設置,用戶頭像,密碼修改
func profileHandler(w http.ResponseWriter, r *http.Request) {
	user, ok := currentUser(r)

	if !ok {
		http.Redirect(w, r, "/signin?next=/profile", http.StatusFound)
		return
	}

	profileForm := wtforms.NewForm(
		wtforms.NewTextField("email", "電子郵件", user.Email, wtforms.Email{}),
		wtforms.NewTextField("website", "個人網站", user.Website),
		wtforms.NewTextField("location", "所在地", user.Location),
		wtforms.NewTextField("tagline", "簽名", user.Tagline),
		wtforms.NewTextArea("bio", "個人簡介", user.Bio),
	)

	if r.Method == "POST" {
		if profileForm.Validate(r) {
			c := db.C("users")
			c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{"website": profileForm.Value("website"),
				"location": profileForm.Value("location"),
				"tagline":  profileForm.Value("tagline"),
				"bio":      profileForm.Value("bio"),
			}})
			http.Redirect(w, r, "/profile", http.StatusFound)
			return
		}
	}

	renderTemplate(w, r, "account/profile.html", map[string]interface{}{"user": user, "profileForm": profileForm})
}
開發者ID:RaymondChou,項目名稱:gopher_blog,代碼行數:33,代碼來源:account.go

示例3: adminNewAdHandler

// URL: /admin/ad/new
// 添加廣告
func adminNewAdHandler(handler *Handler) {
	defer dps.Persist()

	choices := []wtforms.Choice{
		wtforms.Choice{"top0", "最頂部"},
		wtforms.Choice{"top", "頂部"},
		wtforms.Choice{"frontpage", "首頁"},
		wtforms.Choice{"content", "主題內"},
		wtforms.Choice{"2cols", "2列寬度"},
		wtforms.Choice{"3cols", "3列寬度"},
		wtforms.Choice{"4cols", "4列寬度"},
	}
	form := wtforms.NewForm(
		wtforms.NewSelectField("position", "位置", choices, "", wtforms.Required{}),
		wtforms.NewTextField("name", "名稱", "", wtforms.Required{}),
		wtforms.NewTextField("index", "序號", "", wtforms.Required{}),
		wtforms.NewTextArea("code", "代碼", "", wtforms.Required{}),
	)

	if handler.Request.Method == "POST" {
		if !form.Validate(handler.Request) {
			handler.renderTemplate("ad/form.html", ADMIN, map[string]interface{}{
				"form":  form,
				"isNew": true,
			})
			return
		}

		c := handler.DB.C(ADS)
		index, err := strconv.Atoi(form.Value("index"))
		if err != nil {
			form.AddError("index", "請輸入正確的數字")
			handler.renderTemplate("ad/form.html", ADMIN, map[string]interface{}{
				"form":  form,
				"isNew": true,
			})
			return
		}

		err = c.Insert(&AD{
			Id_:      bson.NewObjectId(),
			Position: form.Value("position"),
			Name:     form.Value("name"),
			Code:     form.Value("code"),
			Index:    index,
		})

		if err != nil {
			panic(err)
		}

		http.Redirect(handler.ResponseWriter, handler.Request, "/admin/ads", http.StatusFound)
		return
	}

	handler.renderTemplate("ad/form.html", ADMIN, map[string]interface{}{
		"form":  form,
		"isNew": true,
	})
}
開發者ID:ZuiGuangYin,項目名稱:gopher,代碼行數:62,代碼來源:ad.go

示例4: profileHandler

// URL /profile
// 用戶設置頁麵,顯示用戶設置,用戶頭像,密碼修改
func profileHandler(w http.ResponseWriter, r *http.Request) {
	user, _ := currentUser(r)

	profileForm := wtforms.NewForm(
		wtforms.NewTextField("email", "電子郵件", user.Email, wtforms.Email{}),
		wtforms.NewTextField("website", "個人網站", user.Website),
		wtforms.NewTextField("location", "所在地", user.Location),
		wtforms.NewTextField("tagline", "簽名", user.Tagline),
		wtforms.NewTextArea("bio", "個人簡介", user.Bio),
		wtforms.NewTextField("github_username", "GitHub用戶名", user.GitHubUsername),
		wtforms.NewTextField("weibo", "新浪微博", user.Weibo),
	)

	if r.Method == "POST" {
		if profileForm.Validate(r) {
			c := DB.C(USERS)
			c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{
				"website":        profileForm.Value("website"),
				"location":       profileForm.Value("location"),
				"tagline":        profileForm.Value("tagline"),
				"bio":            profileForm.Value("bio"),
				"githubusername": profileForm.Value("github_username"),
				"weibo":          profileForm.Value("weibo"),
			}})
			http.Redirect(w, r, "/profile", http.StatusFound)
			return
		}
	}

	renderTemplate(w, r, "account/profile.html", BASE, map[string]interface{}{
		"user":           user,
		"profileForm":    profileForm,
		"defaultAvatars": defaultAvatars,
	})
}
開發者ID:nicai1900,項目名稱:gopher,代碼行數:37,代碼來源:account.go

示例5: adminNewNodeHandler

// URL: /admin/node/new
// 新建節點
func adminNewNodeHandler(w http.ResponseWriter, r *http.Request) {
	user, ok := currentUser(r)
	if !ok {
		http.Redirect(w, r, "/signin?next=/node/new", http.StatusFound)
		return
	}

	if !user.IsSuperuser {
		message(w, r, "沒有權限", "你沒有新建節點的權限", "error")
		return
	}

	form := wtforms.NewForm(
		wtforms.NewTextField("id", "ID", "", &wtforms.Required{}),
		wtforms.NewTextField("name", "名稱", "", &wtforms.Required{}),
		wtforms.NewTextArea("description", "描述", "", &wtforms.Required{}),
	)

	if r.Method == "POST" {
		if form.Validate(r) {
			c := db.C("nodes")
			node := Node{}

			err := c.Find(bson.M{"id": form.Value("id")}).One(&node)

			if err == nil {
				form.AddError("id", "該ID已經存在")

				renderTemplate(w, r, "node/new.html", map[string]interface{}{"form": form, "adminNav": ADMIN_NAV})
				return
			}

			err = c.Find(bson.M{"name": form.Value("name")}).One(&node)

			if err == nil {
				form.AddError("name", "該名稱已經存在")

				renderTemplate(w, r, "node/new.html", map[string]interface{}{"form": form, "adminNav": ADMIN_NAV})
				return
			}

			Id_ := bson.NewObjectId()
			err = c.Insert(&Node{
				Id_:         Id_,
				Id:          form.Value("id"),
				Name:        form.Value("name"),
				Description: form.Value("description")})

			if err != nil {
				panic(err)
			}

			http.Redirect(w, r, "/admin/node/new", http.StatusFound)
		}
	}

	renderTemplate(w, r, "node/new.html", map[string]interface{}{"form": form, "adminNav": ADMIN_NAV})
}
開發者ID:RaymondChou,項目名稱:gopher_blog,代碼行數:60,代碼來源:admin.go

示例6: newPackageHandler

// URL: /package/new
// 新建第三方包
func newPackageHandler(w http.ResponseWriter, r *http.Request) {
	user, _ := currentUser(r)

	var categories []PackageCategory

	c := DB.C("packagecategories")
	c.Find(nil).All(&categories)

	var choices []wtforms.Choice

	for _, category := range categories {
		choices = append(choices, wtforms.Choice{Value: category.Id_.Hex(), Label: category.Name})
	}

	form := wtforms.NewForm(
		wtforms.NewHiddenField("html", ""),
		wtforms.NewTextField("name", "名稱", "", wtforms.Required{}),
		wtforms.NewSelectField("category_id", "分類", choices, ""),
		wtforms.NewTextField("url", "網址", "", wtforms.Required{}, wtforms.URL{}),
		wtforms.NewTextArea("description", "描述", "", wtforms.Required{}),
	)

	if r.Method == "POST" && form.Validate(r) {
		c = DB.C("contents")
		id := bson.NewObjectId()
		categoryId := bson.ObjectIdHex(form.Value("category_id"))
		html := form.Value("html")
		html = strings.Replace(html, "<pre>", `<pre class="prettyprint linenums">`, -1)
		c.Insert(&Package{
			Content: Content{
				Id_:       id,
				Type:      TypePackage,
				Title:     form.Value("name"),
				Markdown:  form.Value("description"),
				Html:      template.HTML(html),
				CreatedBy: user.Id_,
				CreatedAt: time.Now(),
			},
			Id_:        id,
			CategoryId: categoryId,
			Url:        form.Value("url"),
		})

		c = DB.C("packagecategories")
		// 增加數量
		c.Update(bson.M{"_id": categoryId}, bson.M{"$inc": bson.M{"packagecount": 1}})

		http.Redirect(w, r, "/p/"+id.Hex(), http.StatusFound)
		return
	}
	renderTemplate(w, r, "package/form.html", map[string]interface{}{
		"form":   form,
		"title":  "提交第三方包",
		"action": "/package/new",
		"active": "package",
	})
}
開發者ID:jinzhe,項目名稱:gopher,代碼行數:59,代碼來源:package.go

示例7: editBookHandler

// URL: /admin/book/{id}/edit
// 編輯圖書
func editBookHandler(handler *Handler) {
	defer deferclient.Persist()

	bookId := mux.Vars(handler.Request)["id"]

	c := handler.DB.C(BOOKS)
	var book Book
	c.Find(bson.M{"_id": bson.ObjectIdHex(bookId)}).One(&book)

	form := wtforms.NewForm(
		wtforms.NewTextField("title", "書名", book.Title, wtforms.Required{}),
		wtforms.NewTextField("cover", "封麵", book.Cover, wtforms.Required{}),
		wtforms.NewTextField("author", "作者", book.Author, wtforms.Required{}),
		wtforms.NewTextField("translator", "譯者", book.Translator),
		wtforms.NewTextArea("introduction", "簡介", book.Introduction),
		wtforms.NewTextField("pages", "頁數", strconv.Itoa(book.Pages), wtforms.Required{}),
		wtforms.NewTextField("language", "語言", book.Language, wtforms.Required{}),
		wtforms.NewTextField("publisher", "出版社", book.Publisher),
		wtforms.NewTextField("publication_date", "出版年月日", book.PublicationDate),
		wtforms.NewTextField("isbn", "ISBN", book.ISBN),
	)

	if handler.Request.Method == "POST" {
		if form.Validate(handler.Request) {
			pages, _ := strconv.Atoi(form.Value("pages"))

			err := c.Update(bson.M{"_id": book.Id_}, bson.M{"$set": bson.M{
				"title":            form.Value("title"),
				"cover":            form.Value("cover"),
				"author":           form.Value("author"),
				"translator":       form.Value("translator"),
				"introduction":     form.Value("introduction"),
				"pages":            pages,
				"language":         form.Value("language"),
				"publisher":        form.Value("publisher"),
				"publication_date": form.Value("publication_date"),
				"isbn":             form.Value("isbn"),
			}})

			if err != nil {
				panic(err)
			}

			http.Redirect(handler.ResponseWriter, handler.Request, "/admin/books", http.StatusFound)
			return
		}
	}

	handler.renderTemplate("book/form.html", ADMIN, map[string]interface{}{
		"book":  book,
		"form":  form,
		"isNew": false,
	})
}
開發者ID:makohill,項目名稱:androidfancier.cn,代碼行數:56,代碼來源:book.go

示例8: editUserInfoHandler

// URL /user_center/edit_info
// 修改用戶資料
func editUserInfoHandler(handler *Handler) {
	user, _ := currentUser(handler)

	profileForm := wtforms.NewForm(
		wtforms.NewTextField("email", "電子郵件", user.Email, wtforms.Email{}),
		wtforms.NewTextField("website", "個人網站", user.Website),
		wtforms.NewTextField("location", "所在地", user.Location),
		wtforms.NewTextField("tagline", "簽名", user.Tagline),
		wtforms.NewTextArea("bio", "個人簡介", user.Bio),
		wtforms.NewTextField("github_username", "GitHub用戶名", user.GitHubUsername),
		wtforms.NewTextField("weibo", "新浪微博", user.Weibo),
	)

	if handler.Request.Method == "POST" {
		if profileForm.Validate(handler.Request) {
			c := handler.DB.C(USERS)

			// 檢查郵箱
			result := new(User)
			err := c.Find(bson.M{"email": profileForm.Value("email")}).One(result)
			if err == nil && result.Id_ != user.Id_ {
				profileForm.AddError("email", "電子郵件地址已經被使用")

				handler.renderTemplate("user_center/info_form.html", BASE, map[string]interface{}{
					"user":        user,
					"profileForm": profileForm,
					"active":      "edit_info",
				})
				return
			}

			c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{
				"email":          profileForm.Value("email"),
				"website":        profileForm.Value("website"),
				"location":       profileForm.Value("location"),
				"tagline":        profileForm.Value("tagline"),
				"bio":            profileForm.Value("bio"),
				"githubusername": profileForm.Value("github_username"),
				"weibo":          profileForm.Value("weibo"),
			}})
			handler.redirect("/user_center/edit_info", http.StatusFound)
			return
		}
	}

	handler.renderTemplate("user_center/info_form.html", BASE, map[string]interface{}{
		"user":        user,
		"profileForm": profileForm,
		"active":      "edit_info",
	})
}
開發者ID:ZuiGuangYin,項目名稱:gopher,代碼行數:53,代碼來源:user_center.go

示例9: adminNewNodeHandler

// URL: /admin/node/new
// 新建節點
func adminNewNodeHandler(handler *Handler) {
	defer deferclient.Persist()

	form := wtforms.NewForm(
		wtforms.NewTextField("id", "ID", "", &wtforms.Required{}),
		wtforms.NewTextField("name", "名稱", "", &wtforms.Required{}),
		wtforms.NewTextArea("description", "描述", "", &wtforms.Required{}),
	)

	if handler.Request.Method == "POST" {
		if form.Validate(handler.Request) {
			c := handler.DB.C(NODES)
			node := Node{}

			err := c.Find(bson.M{"id": form.Value("id")}).One(&node)

			if err == nil {
				form.AddError("id", "該ID已經存在")

				handler.renderTemplate("node/new.html", ADMIN, map[string]interface{}{"form": form})
				return
			}

			err = c.Find(bson.M{"name": form.Value("name")}).One(&node)

			if err == nil {
				form.AddError("name", "該名稱已經存在")

				handler.renderTemplate("node/new.html", ADMIN, map[string]interface{}{"form": form})
				return
			}

			Id_ := bson.NewObjectId()
			err = c.Insert(&Node{
				Id_:         Id_,
				Id:          form.Value("id"),
				Name:        form.Value("name"),
				Description: form.Value("description")})

			if err != nil {
				panic(err)
			}

			http.Redirect(handler.ResponseWriter, handler.Request, "/admin/node/new", http.StatusFound)
		}
	}

	handler.renderTemplate("node/new.html", ADMIN, map[string]interface{}{"form": form})
}
開發者ID:makohill,項目名稱:androidfancier.cn,代碼行數:51,代碼來源:node.go

示例10: adminEditAdHandler

// URL: /admin/ad/{id}/edit
// 編輯廣告
func adminEditAdHandler(w http.ResponseWriter, r *http.Request) {
	id := mux.Vars(r)["id"]

	c := DB.C("ads")
	var ad AD
	c.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&ad)

	choices := []wtforms.Choice{
		wtforms.Choice{"frongpage", "首頁"},
		wtforms.Choice{"3cols", "3列寬度"},
		wtforms.Choice{"4cols", "4列寬度"},
	}
	form := wtforms.NewForm(
		wtforms.NewSelectField("position", "位置", choices, ad.Position, wtforms.Required{}),
		wtforms.NewTextField("name", "名稱", ad.Name, wtforms.Required{}),
		wtforms.NewTextArea("code", "代碼", ad.Code, wtforms.Required{}),
	)

	if r.Method == "POST" {
		if !form.Validate(r) {
			renderTemplate(w, r, "admin/ad_form.html", map[string]interface{}{
				"adminNav": ADMIN_NAV,
				"form":     form,
				"isNew":    false,
			})
			return
		}

		err := c.Update(bson.M{"_id": ad.Id_}, bson.M{"$set": bson.M{
			"position": form.Value("position"),
			"name":     form.Value("name"),
			"code":     form.Value("code"),
		}})

		if err != nil {
			panic(err)
		}

		http.Redirect(w, r, "/admin/ads", http.StatusFound)
		return
	}

	renderTemplate(w, r, "admin/ad_form.html", map[string]interface{}{
		"adminNav": ADMIN_NAV,
		"form":     form,
		"isNew":    false,
	})
}
開發者ID:nickelchen,項目名稱:gopher,代碼行數:50,代碼來源:admin.go

示例11: newBookHandler

func newBookHandler(handler *Handler) {
	defer deferclient.Persist()

	form := wtforms.NewForm(
		wtforms.NewTextField("title", "書名", "", wtforms.Required{}),
		wtforms.NewTextField("cover", "封麵", "", wtforms.Required{}),
		wtforms.NewTextField("author", "作者", "", wtforms.Required{}),
		wtforms.NewTextField("translator", "譯者", ""),
		wtforms.NewTextArea("introduction", "簡介", ""),
		wtforms.NewTextField("pages", "頁數", "", wtforms.Required{}),
		wtforms.NewTextField("language", "語言", "", wtforms.Required{}),
		wtforms.NewTextField("publisher", "出版社", ""),
		wtforms.NewTextField("publication_date", "出版年月日", ""),
		wtforms.NewTextField("isbn", "ISBN", ""),
	)

	if handler.Request.Method == "POST" {
		if form.Validate(handler.Request) {
			pages, _ := strconv.Atoi(form.Value("pages"))
			c := handler.DB.C(BOOKS)
			err := c.Insert(&Book{
				Id_:             bson.NewObjectId(),
				Title:           form.Value("title"),
				Cover:           form.Value("cover"),
				Author:          form.Value("author"),
				Translator:      form.Value("translator"),
				Pages:           pages,
				Language:        form.Value("language"),
				Publisher:       form.Value("publisher"),
				PublicationDate: form.Value("publication_date"),
				Introduction:    form.Value("introduction"),
				ISBN:            form.Value("isbn"),
			})

			if err != nil {
				panic(err)
			}
			http.Redirect(handler.ResponseWriter, handler.Request, "/admin/books", http.StatusFound)
			return
		}
	}

	handler.renderTemplate("book/form.html", ADMIN, map[string]interface{}{
		"form":  form,
		"isNew": true,
	})
}
開發者ID:makohill,項目名稱:androidfancier.cn,代碼行數:47,代碼來源:book.go

示例12: adminNewAdHandler

// URL: /admin/ad/new
// 添加廣告
func adminNewAdHandler(w http.ResponseWriter, r *http.Request) {
	choices := []wtforms.Choice{
		wtforms.Choice{"frongpage", "首頁"},
		wtforms.Choice{"2cols", "2列寬度"},
		wtforms.Choice{"3cols", "3列寬度"},
		wtforms.Choice{"4cols", "4列寬度"},
	}
	form := wtforms.NewForm(
		wtforms.NewSelectField("position", "位置", choices, "", wtforms.Required{}),
		wtforms.NewTextField("name", "名稱", "", wtforms.Required{}),
		wtforms.NewTextArea("code", "代碼", "", wtforms.Required{}),
	)

	if r.Method == "POST" {
		if !form.Validate(r) {
			renderTemplate(w, r, "admin/ad_form.html", map[string]interface{}{
				"adminNav": ADMIN_NAV,
				"form":     form,
				"isNew":    true,
			})
			return
		}

		c := DB.C("ads")
		err := c.Insert(&AD{
			Id_:      bson.NewObjectId(),
			Position: form.Value("position"),
			Name:     form.Value("name"),
			Code:     form.Value("code"),
		})

		if err != nil {
			panic(err)
		}

		http.Redirect(w, r, "/admin/ads", http.StatusFound)
		return
	}

	renderTemplate(w, r, "admin/ad_form.html", map[string]interface{}{
		"adminNav": ADMIN_NAV,
		"form":     form,
		"isNew":    true,
	})
}
開發者ID:nickelchen,項目名稱:gopher,代碼行數:47,代碼來源:admin.go

示例13: newTopicHandler

// URL: /topic/new
// 新建主題
func newTopicHandler(w http.ResponseWriter, r *http.Request) {
	nodeId := mux.Vars(r)["node"]

	var nodes []Node
	c := DB.C("nodes")
	c.Find(nil).All(&nodes)

	var choices = []wtforms.Choice{wtforms.Choice{}} // 第一個選項為空

	for _, node := range nodes {
		choices = append(choices, wtforms.Choice{Value: node.Id_.Hex(), Label: node.Name})
	}

	form := wtforms.NewForm(
		wtforms.NewHiddenField("html", ""),
		wtforms.NewSelectField("node", "節點", choices, nodeId, &wtforms.Required{}),
		wtforms.NewTextArea("title", "標題", "", &wtforms.Required{}),
		wtforms.NewTextArea("content", "內容", ""),
	)

	var content string
	var html template.HTML

	if r.Method == "POST" {
		if form.Validate(r) {
			session, _ := store.Get(r, "user")
			username, _ := session.Values["username"]
			username = username.(string)

			user := User{}
			c = DB.C("users")
			c.Find(bson.M{"username": username}).One(&user)

			c = DB.C("contents")

			id_ := bson.NewObjectId()

			now := time.Now()

			html2 := form.Value("html")
			html2 = strings.Replace(html2, "<pre>", `<pre class="prettyprint linenums">`, -1)

			nodeId := bson.ObjectIdHex(form.Value("node"))
			err := c.Insert(&Topic{
				Content: Content{
					Id_:       id_,
					Type:      TypeTopic,
					Title:     form.Value("title"),
					Markdown:  form.Value("content"),
					Html:      template.HTML(html2),
					CreatedBy: user.Id_,
					CreatedAt: now,
				},
				Id_:             id_,
				NodeId:          nodeId,
				LatestRepliedAt: now,
			})

			if err != nil {
				fmt.Println("newTopicHandler:", err.Error())
				return
			}

			// 增加Node.TopicCount
			c = DB.C("nodes")
			c.Update(bson.M{"_id": nodeId}, bson.M{"$inc": bson.M{"topiccount": 1}})

			c = DB.C("status")
			var status Status
			c.Find(nil).One(&status)

			c.Update(bson.M{"_id": status.Id_}, bson.M{"$inc": bson.M{"topiccount": 1}})

			http.Redirect(w, r, "/t/"+id_.Hex(), http.StatusFound)
			return
		}

		content = form.Value("content")
		html = template.HTML(form.Value("html"))
		form.SetValue("html", "")
	}

	renderTemplate(w, r, "topic/form.html", map[string]interface{}{
		"form":    form,
		"title":   "新建",
		"html":    html,
		"content": content,
		"action":  "/topic/new",
		"active":  "topic",
	})
}
開發者ID:jinzhe,項目名稱:gopher,代碼行數:93,代碼來源:topic.go

示例14: editArticleHandler

// URL: /a/{articleId}/edit
// 編輯主題
func editArticleHandler(w http.ResponseWriter, r *http.Request) {
	user, _ := currentUser(r)

	articleId := mux.Vars(r)["articleId"]

	c := DB.C("contents")
	var article Article
	err := c.Find(bson.M{"_id": bson.ObjectIdHex(articleId)}).One(&article)

	if err != nil {
		message(w, r, "沒有該文章", "沒有該文章,不能編輯", "error")
		return
	}

	if !article.CanEdit(user.Username) {
		message(w, r, "沒用該權限", "對不起,你沒有權限編輯該文章", "error")
		return
	}

	var categorys []ArticleCategory
	c = DB.C("articlecategories")
	c.Find(nil).All(&categorys)

	var choices []wtforms.Choice

	for _, category := range categorys {
		choices = append(choices, wtforms.Choice{Value: category.Id_.Hex(), Label: category.Name})
	}

	form := wtforms.NewForm(
		wtforms.NewHiddenField("html", ""),
		wtforms.NewTextField("title", "標題", article.Title, wtforms.Required{}),
		wtforms.NewTextArea("content", "內容", article.Markdown, wtforms.Required{}),
		wtforms.NewTextField("original_source", "原始出處", article.OriginalSource, wtforms.Required{}),
		wtforms.NewTextField("original_url", "原始鏈接", article.OriginalUrl, wtforms.URL{}),
		wtforms.NewSelectField("category", "分類", choices, article.CategoryId.Hex()),
	)

	content := article.Markdown
	html := article.Html

	if r.Method == "POST" {
		if form.Validate(r) {
			html := form.Value("html")
			html = strings.Replace(html, "<pre>", `<pre class="prettyprint linenums">`, -1)

			categoryId := bson.ObjectIdHex(form.Value("category"))
			c = DB.C("contents")
			err = c.Update(bson.M{"_id": article.Id_}, bson.M{"$set": bson.M{
				"categoryid":        categoryId,
				"originalsource":    form.Value("original_source"),
				"originalurl":       form.Value("original_url"),
				"content.title":     form.Value("title"),
				"content.markdown":  form.Value("content"),
				"content.html":      template.HTML(html),
				"content.updatedby": user.Id_.Hex(),
				"content.updatedat": time.Now(),
			}})

			if err != nil {
				fmt.Println("update error:", err.Error())
				return
			}

			http.Redirect(w, r, "/a/"+article.Id_.Hex(), http.StatusFound)
			return
		}

		content = form.Value("content")
		html = template.HTML(form.Value("html"))
	}

	renderTemplate(w, r, "article/form.html", map[string]interface{}{
		"form":    form,
		"title":   "編輯",
		"action":  "/a/" + articleId + "/edit",
		"html":    html,
		"content": content,
		"active":  "article",
	})
}
開發者ID:jinzhe,項目名稱:gopher,代碼行數:83,代碼來源:article.go

示例15: editTopicHandler

// URL: /t/{topicId}/edit
// 編輯主題
func editTopicHandler(w http.ResponseWriter, r *http.Request) {
	user, _ := currentUser(r)

	topicId := mux.Vars(r)["topicId"]

	c := DB.C("contents")
	var topic Topic
	err := c.Find(bson.M{"_id": bson.ObjectIdHex(topicId), "content.type": TypeTopic}).One(&topic)

	if err != nil {
		message(w, r, "沒有該主題", "沒有該主題,不能編輯", "error")
		return
	}

	if !topic.CanEdit(user.Username) {
		message(w, r, "沒有該權限", "對不起,你沒有權限編輯該主題", "error")
		return
	}

	var nodes []Node
	c = DB.C("nodes")
	c.Find(nil).All(&nodes)

	var choices = []wtforms.Choice{wtforms.Choice{}} // 第一個選項為空

	for _, node := range nodes {
		choices = append(choices, wtforms.Choice{Value: node.Id_.Hex(), Label: node.Name})
	}

	form := wtforms.NewForm(
		wtforms.NewHiddenField("html", ""),
		wtforms.NewSelectField("node", "節點", choices, topic.NodeId.Hex(), &wtforms.Required{}),
		wtforms.NewTextArea("title", "標題", topic.Title, &wtforms.Required{}),
		wtforms.NewTextArea("content", "內容", topic.Markdown),
	)

	content := topic.Markdown
	html := topic.Html

	if r.Method == "POST" {
		if form.Validate(r) {
			html2 := form.Value("html")
			html2 = strings.Replace(html2, "<pre>", `<pre class="prettyprint linenums">`, -1)

			nodeId := bson.ObjectIdHex(form.Value("node"))
			c = DB.C("contents")
			c.Update(bson.M{"_id": topic.Id_}, bson.M{"$set": bson.M{
				"nodeid":            nodeId,
				"content.title":     form.Value("title"),
				"content.markdown":  form.Value("content"),
				"content.html":      template.HTML(html2),
				"content.updatedat": time.Now(),
				"content.updatedby": user.Id_.Hex(),
			}})

			// 如果兩次的節點不同,更新節點的主題數量
			if topic.NodeId != nodeId {
				c = DB.C("nodes")
				c.Update(bson.M{"_id": topic.NodeId}, bson.M{"$inc": bson.M{"topiccount": -1}})
				c.Update(bson.M{"_id": nodeId}, bson.M{"$inc": bson.M{"topiccount": 1}})
			}

			http.Redirect(w, r, "/t/"+topic.Id_.Hex(), http.StatusFound)
			return
		}

		content = form.Value("content")
		html = template.HTML(form.Value("html"))
		form.SetValue("html", "")
	}

	renderTemplate(w, r, "topic/form.html", map[string]interface{}{
		"form":    form,
		"title":   "編輯",
		"action":  "/t/" + topicId + "/edit",
		"html":    html,
		"content": content,
		"active":  "topic",
	})
}
開發者ID:jinzhe,項目名稱:gopher,代碼行數:82,代碼來源:topic.go


注:本文中的github.com/jimmykuu/wtforms.NewTextArea函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。