forked from jimmykuu/gopher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.go
195 lines (158 loc) · 5.28 KB
/
site.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
酷站
*/
package gopher
import (
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/jimmykuu/wtforms"
"gopkg.in/mgo.v2/bson"
)
// URL: /sites
// 酷站首页,列出所有分类及站点
func sitesHandler(handler *Handler) {
var categories []SiteCategory
c := handler.DB.C(SITE_CATEGORIES)
c.Find(nil).All(&categories)
handler.renderTemplate("site/index.html", BASE, map[string]interface{}{
"categories": categories,
"active": "site",
})
}
// URL: /site/new
// 提交站点
func newSiteHandler(handler *Handler) {
user, _ := currentUser(handler)
var categories []SiteCategory
c := handler.DB.C(SITE_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.NewTextField("name", "网站名称", "", wtforms.Required{}),
wtforms.NewTextField("url", "地址", "", wtforms.Required{}, wtforms.URL{}),
wtforms.NewTextArea("description", "描述", ""),
wtforms.NewSelectField("category", "分类", choices, "", wtforms.Required{}),
)
if handler.Request.Method == "POST" {
if !form.Validate(handler.Request) {
handler.renderTemplate("site/form.html", BASE, map[string]interface{}{"form": form, "action": "/site/new", "title": "新建"})
return
}
var site Site
c = handler.DB.C(CONTENTS)
err := c.Find(bson.M{"url": form.Value("url")}).One(&site)
if err == nil {
form.AddError("url", "该站点已经有了")
handler.renderTemplate("site/form.html", BASE, map[string]interface{}{"form": form, "action": "/site/new", "title": "新建"})
return
}
id_ := bson.NewObjectId()
c.Insert(&Site{
Id_: id_,
Content: Content{
Id_: id_,
Type: TypeSite,
Title: form.Value("name"),
Markdown: form.Value("description"),
CreatedBy: user.Id_,
CreatedAt: time.Now(),
},
Url: form.Value("url"),
CategoryId: bson.ObjectIdHex(form.Value("category")),
})
http.Redirect(handler.ResponseWriter, handler.Request, "/sites#site-"+id_.Hex(), http.StatusFound)
return
}
handler.renderTemplate("site/form.html", BASE, map[string]interface{}{
"form": form,
"action": "/site/new",
"title": "新建",
"active": "site",
})
}
// URL: /site/{siteId}/edit
// 修改提交过的站点信息,提交者自己或者管理员可以修改
func editSiteHandler(handler *Handler) {
user, _ := currentUser(handler)
siteId := bson.ObjectIdHex(mux.Vars(handler.Request)["siteId"])
var site Site
c := handler.DB.C(CONTENTS)
err := c.Find(bson.M{"_id": siteId, "content.type": TypeSite}).One(&site)
if err != nil {
message(handler, "错误的连接", "错误的连接", "error")
return
}
if !site.CanEdit(user.Username, handler.DB) {
message(handler, "没有权限", "你没有权限可以修改站点", "error")
return
}
var categories []SiteCategory
c = handler.DB.C(SITE_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.NewTextField("name", "网站名称", site.Title, wtforms.Required{}),
wtforms.NewTextField("url", "地址", site.Url, wtforms.Required{}, wtforms.URL{}),
wtforms.NewTextArea("description", "描述", site.Markdown),
wtforms.NewSelectField("category", "分类", choices, site.CategoryId.Hex(), wtforms.Required{}),
)
if handler.Request.Method == "POST" && form.Validate(handler.Request) {
// 检查是否用重复
var site2 Site
c = handler.DB.C(CONTENTS)
err := c.Find(bson.M{"url": form.Value("url"), "_id": bson.M{"$ne": site.Id_}}).One(&site2)
if err == nil {
form.AddError("url", "该站点已经有了")
handler.renderTemplate("site/form.html", BASE, map[string]interface{}{
"form": form,
"action": "/site/" + siteId.Hex() + "/edit",
"title": "编辑",
})
return
}
c.Update(bson.M{"_id": site.Id_},
bson.M{"$set": bson.M{
"content.title": form.Value("name"),
"content.markdown": form.Value("description"),
"content.updatedby": user.Id_.Hex(),
"content.updatedat": time.Now(),
"url": form.Value("url"),
"categoryid": bson.ObjectIdHex(form.Value("category")),
},
})
http.Redirect(handler.ResponseWriter, handler.Request, "/sites#site-"+site.Id_.Hex(), http.StatusFound)
return
}
handler.renderTemplate("site/form.html", BASE, map[string]interface{}{
"form": form,
"action": "/site/" + siteId.Hex() + "/edit",
"title": "编辑",
"active": "site",
})
}
// URL: /site/{siteId}/delete
// 删除站点,提交者自己或者管理员可以删除
func deleteSiteHandler(handler *Handler) {
user, _ := currentUser(handler)
siteId := bson.ObjectIdHex(mux.Vars(handler.Request)["siteId"])
var site Site
c := handler.DB.C(CONTENTS)
err := c.Find(bson.M{"_id": siteId}).One(&site)
if err != nil {
message(handler, "错误的连接", "错误的连接", "error")
return
}
if !site.CanEdit(user.Username, handler.DB) {
message(handler, "没有权限", "你没有权限可以删除站点", "error")
return
}
c.Remove(bson.M{"_id": site.Id_})
http.Redirect(handler.ResponseWriter, handler.Request, "/sites", http.StatusFound)
}