Esempio n. 1
0
// URL: /admin/ad/new
// 添加广告
func adminNewAdHandler(handler *Handler) {
	defer deferclient.Persist()

	choices := []wtforms.Choice{
		wtforms.Choice{"top", "顶部"},
		wtforms.Choice{"frontpage", "首页"},
		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,
	})
}
// ULR: /admin/link_exchange/new
// 增加友链
func adminNewLinkExchangeHandler(handler *Handler) {
	defer deferclient.Persist()

	form := wtforms.NewForm(
		wtforms.NewTextField("name", "名称", "", wtforms.Required{}),
		wtforms.NewTextField("url", "URL", "", wtforms.Required{}, wtforms.URL{}),
		wtforms.NewTextField("description", "描述", "", wtforms.Required{}),
		wtforms.NewTextField("logo", "Logo", ""),
	)

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

		c := handler.DB.C(LINK_EXCHANGES)
		var linkExchange LinkExchange
		err := c.Find(bson.M{"url": form.Value("url")}).One(&linkExchange)

		if err == nil {
			form.AddError("url", "该URL已经有了")
			handler.renderTemplate("link_exchange/form.html", ADMIN, map[string]interface{}{
				"form":  form,
				"isNew": true,
			})
			return
		}

		err = c.Insert(&LinkExchange{
			Id_:         bson.NewObjectId(),
			Name:        form.Value("name"),
			URL:         form.Value("url"),
			Description: form.Value("description"),
			Logo:        form.Value("logo"),
			IsOnHome:    handler.Request.FormValue("is_on_home") == "on",
			IsOnBottom:  handler.Request.FormValue("is_on_bottom") == "on",
		})

		if err != nil {
			panic(err)
		}

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

	handler.renderTemplate("link_exchange/form.html", ADMIN, map[string]interface{}{
		"form":  form,
		"isNew": true,
	})
}
Esempio n. 3
0
// 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,
	})
}
Esempio n. 4
0
// 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})
}
// URL: /admin/link_exchange/{linkExchangeId}/edit
// 编辑友情链接
func adminEditLinkExchangeHandler(handler *Handler) {
	defer deferclient.Persist()

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

	c := handler.DB.C(LINK_EXCHANGES)
	var linkExchange LinkExchange
	c.Find(bson.M{"_id": bson.ObjectIdHex(linkExchangeId)}).One(&linkExchange)

	form := wtforms.NewForm(
		wtforms.NewTextField("name", "名称", linkExchange.Name, wtforms.Required{}),
		wtforms.NewTextField("url", "URL", linkExchange.URL, wtforms.Required{}, wtforms.URL{}),
		wtforms.NewTextField("description", "描述", linkExchange.Description, wtforms.Required{}),
		wtforms.NewTextField("logo", "Logo", linkExchange.Logo),
	)

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

		err := c.Update(bson.M{"_id": linkExchange.Id_}, bson.M{"$set": bson.M{
			"name":         form.Value("name"),
			"url":          form.Value("url"),
			"description":  form.Value("description"),
			"logo":         form.Value("logo"),
			"is_on_home":   handler.Request.FormValue("is_on_home") == "on",
			"is_on_bottom": handler.Request.FormValue("is_on_bottom") == "on",
		}})

		if err != nil {
			panic(err)
		}

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

	handler.renderTemplate("link_exchange/form.html", ADMIN, map[string]interface{}{
		"linkExchange": linkExchange,
		"form":         form,
		"isNew":        false,
	})
}
Esempio n. 6
0
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,
	})
}
// URL: /admin/package_category/new
// 新建包分类
func adminNewPackageCategoryHandler(handler *Handler) {
	defer deferclient.Persist()

	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,
	})
}
Esempio n. 8
0
func downloadHandler(handler *Handler) {
	defer deferclient.Persist()

	file, err := os.Open("etc/download.json")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	dec := json.NewDecoder(file)

	var versions []Version

	err = dec.Decode(&versions)

	if err != nil {
		panic(err)
	}
	handler.renderTemplate("download.html", BASE, map[string]interface{}{"versions": versions, "active": "download"})
}
Esempio n. 9
0
// 生成users.json字符串
func generateUsersJson(db *mgo.Database) {
	defer deferclient.Persist()

	var users []User
	c := db.C(USERS)
	err := c.Find(nil).All(&users)
	if err != nil {
		panic(err)
	}
	var usernames []string
	for _, user := range users {
		usernames = append(usernames, user.Username)
	}

	b, err := json.Marshal(usernames)
	if err != nil {
		panic(err)
	}
	usersJson = b
}
Esempio n. 10
0
// URL: ws://.../get/package
// 和页面WebSocket通信
func getPackageHandler(ws *websocket.Conn) {
	defer deferclient.Persist()
	defer ws.Close()

	var err error

	for {
		var name string

		if err = websocket.Message.Receive(ws, &name); err != nil {
			fmt.Println("can't receive")
			break
		}

		fmt.Println("received back from client:", name)

		cmd := exec.Command("go", "get", "-u", "-v", name)
		cmd.Env = os.Environ()[:]
		cmd.Env = append(cmd.Env, "GOPATH="+Config.GoGetPath)

		writer := NewConsoleWriter(ws)

		cmd.Stdout = writer
		cmd.Stderr = writer
		err := cmd.Start()
		if err != nil {
			fmt.Println(err)
			break
		}

		err = cmd.Wait()
		if err != nil {
			fmt.Println(err)
			break
		}

		// 压缩
		for _, packageName := range writer.packages {
			tarFilename := strings.Replace(packageName, "/", ".", -1) + ".tar"
			message := Message{
				Type: "command",
				Msg:  fmt.Sprintf("tar %s %s", tarFilename, packageName),
			}
			websocket.JSON.Send(ws, message)

			cmd := exec.Command("tar", "-cf", filepath.Join(Config.PackagesDownloadPath, tarFilename), packageName)
			cmd.Dir = filepath.Join(Config.GoGetPath, "src")
			err = cmd.Run()
			if err != nil {
				panic(err)
			}

			message = Message{
				Type: "command",
				Msg:  fmt.Sprintf("gzip -f %s.tar\n", strings.Replace(packageName, "/", ".", -1)),
			}
			websocket.JSON.Send(ws, message)

			cmd = exec.Command("gzip", "-f", tarFilename)
			cmd.Dir = Config.PackagesDownloadPath
			err = cmd.Run()
			if err != nil {
				panic(err)
			}

			// 发送可以可以下载的package
			websocket.JSON.Send(ws, Message{
				Type: "download",
				Msg:  packageName,
			})
		}

		websocket.JSON.Send(ws, Message{
			Type: "completed",
			Msg:  "------Done------",
		})

		session, err := mgo.Dial(Config.DB)
		if err != nil {
			panic(err)
		}

		session.SetMode(mgo.Monotonic, true)

		defer session.Close()

		DB := session.DB("gopher")

		c := DB.C(DOWNLOADED_PACKAGES)

		err = c.Find(bson.M{"name": name}).One(nil)
		if err == nil {
			c.Update(bson.M{"name": name}, bson.M{"$inc": bson.M{"count": 1}})
		} else {
			c.Insert(&DownloadedPackage{
				Name:  name,
				Count: 1,
			})
		}
		break
	}
}
Esempio n. 11
0
// URL: /admin/ad/{id}/edit
// 编辑广告
func adminEditAdHandler(handler *Handler) {
	defer deferclient.Persist()

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

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

	choices := []wtforms.Choice{
		wtforms.Choice{"top", "顶部"},
		wtforms.Choice{"frontpage", "首页"},
		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.NewTextField("index", "序号", strconv.Itoa(ad.Index), wtforms.Required{}),
		wtforms.NewTextArea("code", "代码", ad.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": false,
			})
			return
		}

		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": 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"),
			"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": false,
	})
}