PageRenderTime 57ms CodeModel.GetById 23ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/controllers/admin.go

http://github.com/TheOnly92/bloody.go
Go | 345 lines | 303 code | 40 blank | 2 comment | 68 complexity | 9c93ba708eb7b85a45b36113402cf6f8 MD5 | raw file
  1package main
  2
  3import (
  4	"mustache"
  5	"time"
  6	"web"
  7	"session"
  8	"crypto/sha1"
  9	"hash"
 10	"strconv"
 11	"encoding/hex"
 12	"os/exec"
 13	"os"
 14	//"fmt"
 15	//"reflect"
 16)
 17
 18type Admin struct {}
 19
 20func (c *Admin) render(output string, title string) string {
 21	template, err := mustache.ParseFile("templates/admin/layout.mustache")
 22	if err != nil {
 23		panic(err)
 24	}
 25	vars := make(map[string]interface{})
 26	vars["Body"] = output
 27	if title != "" {
 28		vars["Title"] = map[string]string {"Name": title}
 29	}
 30	if blogConfig.Get("markdown") == "1" {
 31		vars["markdown"] = "1"
 32	}
 33	return template.Render(vars)
 34}
 35
 36func (c *Admin) IndexGet(ctx *web.Context) string {
 37	sessionH := session.Start(ctx, h)
 38	defer sessionH.Save()
 39	if sessionH.Data["logged"] != nil {
 40		ctx.Redirect(302, "/admin/post/list")
 41		return ""
 42	}
 43	ctx.Redirect(302, "/admin/login")
 44	return ""
 45}
 46
 47func (c *Admin) LoginGet(ctx *web.Context) string {
 48	sessionH := session.Start(ctx, h)
 49	defer sessionH.Save()
 50	if sessionH.Data["logged"] != nil {
 51		ctx.Redirect(302, "/admin/post/list")
 52		return ""
 53	}
 54	output := mustache.RenderFile("templates/admin/login.mustache")
 55	return render(output, "Login")
 56}
 57
 58func (c *Admin) LoginPost(ctx *web.Context) {
 59	sessionH := session.Start(ctx, h)
 60	defer sessionH.Save()
 61	if sessionH.Data["logged"] != nil {
 62		ctx.Redirect(302, "/admin/post/list")
 63		return
 64	}
 65	if ctx.Params["username"] == config.Get("adminuser") && ctx.Params["password"] == config.Get("adminpasswd") {
 66		t := time.Now()
 67		var h hash.Hash = sha1.New()
 68		h.Write([]byte(strconv.FormatInt(t.Unix(), 10)))
 69		sessionH.Data["logged"] = hex.EncodeToString(h.Sum(nil))
 70	}
 71	ctx.Redirect(302, "/admin/post/list")
 72}
 73
 74func (c *Admin) PreferencesGet(ctx *web.Context) string {
 75	sessionH := session.Start(ctx, h)
 76	defer sessionH.Save()
 77	if sessionH.Data["logged"] == nil {
 78		ctx.Redirect(302, "/admin/login")
 79		return ""
 80	}
 81	
 82	vars := map[string]string {"DateFormat": blogConfig.Get("dateFormat"), "PostsPerPage": blogConfig.Get("postsPerPage")}
 83	comment := blogConfig.Get("enableComment")
 84	if comment != "" {
 85		vars["EnableComment"] = comment
 86	}
 87	if blogConfig.Get("markdown") == "1" {
 88		vars["markdown"] = "1"
 89	}
 90	output := mustache.RenderFile("templates/admin/preferences.mustache", vars)
 91	return c.render(output, "Blog Preferernces")
 92}
 93
 94func (c *Admin) PreferencesPost(ctx *web.Context) {
 95	sessionH := session.Start(ctx, h)
 96	defer sessionH.Save()
 97	if sessionH.Data["logged"] == nil {
 98		ctx.Redirect(302, "/admin/login")
 99		return
100	}
101	
102	blogConfig.Update("dateFormat", ctx.Params["dateFormat"])
103	blogConfig.Update("postsPerPage", ctx.Params["postsPerPage"])
104	blogConfig.Update("enableComment", ctx.Params["enableComment"])
105	markdown := "0"
106	if ctx.Params["markdown"] == "on" {
107		markdown = "1"
108	}
109	blogConfig.Update("markdown", markdown)
110	ctx.Redirect(302, "/admin/preferences")
111}
112
113func (c *Admin) NewPostGet(ctx *web.Context) string {
114	sessionH := session.Start(ctx, h)
115	defer sessionH.Save()
116	if sessionH.Data["logged"] == nil {
117		ctx.Redirect(302, "/admin/login")
118		return ""
119	}
120	var output string
121	if _, exists := ctx.Params["markdown"]; exists {
122		output = mustache.RenderFile("templates/admin/new-post-markdown.mustache")
123	} else {
124		output = mustache.RenderFile("templates/admin/new-post.mustache")
125	}
126	return c.render(output, "New Post")
127}
128
129func (c *Admin) NewPostPost(ctx *web.Context) {
130	sessionH := session.Start(ctx, h)
131	defer sessionH.Save()
132	if sessionH.Data["logged"] == nil {
133		ctx.Redirect(302, "/admin/login")
134		return
135	}
136	p := PostModelInit()
137	var markdown uint
138	markdown = 0
139	if _, exists := ctx.Params["markdown"]; exists {
140		markdown = 1
141	}
142	p.Create(ctx.Params["title"], ctx.Params["content"], ctx.Params["status"], markdown)
143	ctx.Redirect(302, "/admin/post/list")
144}
145
146func (c *Admin) ListPost(ctx *web.Context) string {
147	sessionH := session.Start(ctx, h)
148	defer sessionH.Save()
149	if sessionH.Data["logged"] == nil {
150		ctx.Redirect(302, "/admin/login")
151		return ""
152	}
153	page := 0
154	if temp, exists := ctx.Params["page"]; exists {
155		page, _ = strconv.Atoi(temp)
156	}
157	p := PostModelInit()
158	results := p.PostListing(page)
159	
160	vars := map[string]interface{} {"posts":results}
161	
162	output := mustache.RenderFile("templates/admin/list-post.mustache", vars)
163	return c.render(output, "List Posts")
164}
165
166func (c *Admin) EditPostGet(ctx *web.Context, postId string) string {
167	sessionH := session.Start(ctx, h)
168	defer sessionH.Save()
169	if sessionH.Data["logged"] == nil {
170		ctx.Redirect(302, "/admin/login")
171		return ""
172	}
173	p := PostModelInit()
174	result := p.Get(postId)
175	
176	templateVars := map[string]interface{} {"Title":result.Title, "Content":result.Content, "id":objectIdHex(result.Id.String())}
177	if result.Status == 0 {
178		templateVars["Draft"] = 1
179	}
180	if result.Status == 1 {
181		templateVars["Publish"] = 1
182	}
183	var output string
184	if result.Type == 1 {
185		output = mustache.RenderFile("templates/admin/edit-post-markdown.mustache", templateVars)
186	} else {
187		output = mustache.RenderFile("templates/admin/edit-post.mustache", templateVars)
188	}
189	
190	return c.render(output, "Edit Post")
191}
192
193func (c *Admin) EditPostPost(ctx *web.Context, postId string) {
194	sessionH := session.Start(ctx, h)
195	defer sessionH.Save()
196	if sessionH.Data["logged"] == nil {
197		ctx.Redirect(302, "/admin/login")
198		return
199	}
200	p := PostModelInit()
201	p.Update(ctx.Params["title"], ctx.Params["content"], ctx.Params["status"], postId)
202	
203	ctx.Redirect(302, "/admin/post/list")
204}
205
206func (c *Admin) DelPost(ctx *web.Context, postId string) {
207	sessionH := session.Start(ctx, h)
208	defer sessionH.Save()
209	if sessionH.Data["logged"] == nil {
210		ctx.Redirect(302, "/admin/login")
211		return
212	}
213	p := PostModelInit()
214	p.Delete(postId)
215	
216	ctx.Redirect(302, "/admin/post/list")
217}
218
219func (c *Admin) BulkActions(ctx *web.Context) {
220	sessionH := session.Start(ctx, h)
221	defer sessionH.Save()
222	if sessionH.Data["logged"] == nil {
223		ctx.Redirect(302, "/admin/login")
224		return
225	}
226	p := PostModelInit()
227	switch ctx.Params["action"] {
228	case "delete":
229		p.DeleteBulk(ctx.FullParams["posts[]"])
230	case "publish":
231		p.PublishBulk(ctx.FullParams["posts[]"])
232	}
233	
234	ctx.Redirect(302, "/admin/post/list")
235}
236
237func (c *Admin) NewPageGet(ctx *web.Context) string {
238	sessionH := session.Start(ctx, h)
239	defer sessionH.Save()
240	if sessionH.Data["logged"] == nil {
241		ctx.Redirect(302, "/admin/login")
242		return ""
243	}
244	output := mustache.RenderFile("templates/admin/new-page.mustache")
245	return c.render(output, "New Page")
246}
247
248func (c *Admin) NewPagePost(ctx *web.Context) {
249	sessionH := session.Start(ctx, h)
250	defer sessionH.Save()
251	if sessionH.Data["logged"] == nil {
252		ctx.Redirect(302, "/admin/login")
253		return
254	}
255	p := PageModelInit()
256	p.Create(ctx.Params["title"], ctx.Params["content"])
257	ctx.Redirect(302, "/admin/page/list")
258}
259
260func (c *Admin) ListPagesGet(ctx *web.Context) string {
261	sessionH := session.Start(ctx, h)
262	defer sessionH.Save()
263	if sessionH.Data["logged"] == nil {
264		ctx.Redirect(302, "/admin/login")
265		return ""
266	}
267	p := PageModelInit()
268	results := p.List()
269	
270	output := mustache.RenderFile("templates/admin/list-pages.mustache", map[string][]map[string]string {"pages":results})
271	return c.render(output, "List Pages")
272}
273
274func (c *Admin) EditPageGet(ctx *web.Context, id string) string {
275	sessionH := session.Start(ctx, h)
276	defer sessionH.Save()
277	if sessionH.Data["logged"] == nil {
278		ctx.Redirect(302, "/admin/login")
279		return ""
280	}
281	p := PageModelInit()
282	result := p.Get(id)
283	
284	output := mustache.RenderFile("templates/admin/edit-page.mustache", map[string]string {"Title":result.Title, "Content":result.Content, "id":objectIdHex(result.Id.String())})
285	return c.render(output, "Edit Post")
286}
287
288func (c *Admin) EditPagePost(ctx *web.Context, id string) {
289	sessionH := session.Start(ctx, h)
290	defer sessionH.Save()
291	if sessionH.Data["logged"] == nil {
292		ctx.Redirect(302, "/admin/login")
293		return
294	}
295	p := PageModelInit()
296	p.Update(ctx.Params["title"], ctx.Params["content"], id)
297	
298	ctx.Redirect(302, "/admin/page/list")
299}
300
301func (c *Admin) DelPage(ctx *web.Context, id string) {
302	sessionH := session.Start(ctx, h)
303	defer sessionH.Save()
304	if sessionH.Data["logged"] == nil {
305		ctx.Redirect(302, "/admin/login")
306		return
307	}
308	p := PageModelInit()
309	p.Delete(id)
310	
311	ctx.Redirect(302, "/admin/page/list")
312}
313
314func (c *Admin) DelComment(ctx *web.Context, postId string, id string) {
315	sessionH := session.Start(ctx, h)
316	defer sessionH.Save()
317	if sessionH.Data["logged"] == nil {
318		ctx.Redirect(302, "/admin/login")
319		return
320	}
321	
322	p := PostModelInit()
323	p.DeleteComment(postId, id)
324	
325	ctx.Redirect(302, "/post/"+postId)
326}
327
328func (c *Admin) RestartBloody(ctx *web.Context) string {
329	sessionH := session.Start(ctx, h)
330	defer sessionH.Save()
331	if sessionH.Data["logged"] == nil {
332		ctx.Redirect(302, "/admin/login")
333		return ""
334	}
335	
336	pid := os.Getpid()
337	dir, _ := os.Getwd()
338	command1 := exec.Command("kill",strconv.Itoa(pid))
339	command2 := exec.Command(dir+"/bloody")
340	command2.Start()
341	command1.Start()
342	
343	output := mustache.RenderFile("templates/admin/restart.mustache")
344	return c.render(output, "Restarting Bloody")
345}