func acceptSuggestion(w http.ResponseWriter, r *http.Request) { if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { trimPath := strings.Trim(r.URL.Path, "/admin/post/suggestion/accept") postID, _ := strconv.Atoi(trimPath) postID64 := int64(postID) c := appengine.NewContext(r) keyS := datastore.NewKey(c, "PostSuggestion", "", postID64, nil) var suggestion PostSuggestion datastore.Get(c, keyS, &suggestion) var post Post keyP := datastore.NewIncompleteKey(c, "Post", nil) var counter Counter keyC := datastore.NewKey(c, "Counter", "", 1, nil) datastore.Get(c, keyC, &counter) counter.Count = counter.Count + 1 // Add Cache Counter mCount := new(bytes.Buffer) encCount := gob.NewEncoder(mCount) encCount.Encode(counter) cache.AddCache(r, "Counter", mCount.Bytes()) post.Content = suggestion.Content post.Sequence = counter.Count datastore.Put(c, keyP, &post) datastore.Put(c, keyC, &counter) datastore.Delete(c, keyS) http.Redirect(w, r, "/admin/post/suggestion", http.StatusFound) } }
/* * Edit the robots.txt file */ func editRobot(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) // Write the new file if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { Robots := Robots{ Content: []byte(r.FormValue("robots")), } _, err := datastore.Put(c, datastore.NewKey(c, "Robots", "Robots.txt", 0, nil), &Robots) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } cache.DeleteCache(r, "getRobot") } // Merge the models type PassedData struct { CSRFToken string Robots []byte } passedData := PassedData{ CSRFToken: csrf.GetToken(r), Robots: getRobot(r), } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/robots.txt/templates/editRobot.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
// Delete a page func deletePage(w http.ResponseWriter, r *http.Request) { identifier := strings.Replace(r.URL.Path, "/admin/pages/delete/", "", 1) if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { c := appengine.NewContext(r) key := datastore.NewKey(c, "Pages", identifier, 0, nil) datastore.Delete(c, key) cache.DeleteCache(r, "getPage-") cache.DeleteCache(r, "getAllPages") } http.Redirect(w, r, "/admin/pages/", http.StatusFound) }
// Delete a file in the blobstore func deleteFile(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) // Check the CSRF if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { // Get the file ID trimPath := strings.Trim(r.URL.Path, "/admin/files/delete/") key := appengine.BlobKey(trimPath) // Delete blobstore entry blobstore.Delete(c, key) // Redirect http.Redirect(w, r, r.Referer(), http.StatusFound) } }
func editPost(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) // post id'sini al trimPath := strings.Trim(r.URL.Path, "/admin/post/edit/") postSeq, _ := strconv.Atoi(trimPath) postSeq64 := int64(postSeq) var p []Post q := datastore.NewQuery("Post").Filter("Sequence=", postSeq64) keys, _ := q.GetAll(c, &p) if p != nil { post := p[0] if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { if r.Method == "POST" { c := appengine.NewContext(r) post.Content = []byte(r.FormValue("ContentString")) datastore.Put(c, datastore.NewKey(c, "Post", "", keys[0].IntID(), nil), &post) cache.DeleteCache(r, "post-"+strconv.Itoa(postSeq)) } } post.ContentString = string(post.Content) type PassedData struct { CSRFToken string Post Post } passedData := PassedData{ CSRFToken: csrf.GetToken(r), Post: post, } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/admin/sidebar.html")).Execute(passedTemplate, nil) template.Must(template.ParseFiles("templates/admin/post/edit.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) } else { fmt.Fprintln(w, "Böyle bir bilgi yok bro") } }
func deleteSuggestion(w http.ResponseWriter, r *http.Request) { if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { if r.Method == "POST" { trimPath := strings.Trim(r.URL.Path, "/admin/post/suggestion/delete/") postID, _ := strconv.Atoi(trimPath) postID64 := int64(postID) c := appengine.NewContext(r) key := datastore.NewKey(c, "PostSuggestion", "", postID64, nil) datastore.Delete(c, key) http.Redirect(w, r, "/admin/post/suggestion", http.StatusFound) } } }
// Edit a page func editPage(w http.ResponseWriter, r *http.Request) { // Get identifier identifier := strings.Replace(r.URL.Path, "/admin/pages/edit/", "", 1) // Check if page exists exists, page := getPage(identifier, r) if exists == false { http.Redirect(w, r, "/admin/pages/", http.StatusFound) return } // Edit page if csrf.ValidateToken(r, r.FormValue("CSRFToken")) && r.FormValue("url") != "" { c := appengine.NewContext(r) page := Pages{ URL: r.FormValue("url"), Title: r.FormValue("title"), Content: []byte(r.FormValue("content")), } _, err := datastore.Put(c, datastore.NewKey(c, "Pages", r.FormValue("url"), 0, nil), &page) if err != nil { errorHandler.Error(w, r, err.Error()) return } cache.DeleteCache(r, "getPage-"+r.FormValue("url")) cache.DeleteCache(r, "getPage-"+identifier) cache.DeleteCache(r, "getAllPages") http.Redirect(w, r, "/admin/pages/", http.StatusFound) return } // Merge the data type PassedData struct { CSRFToken string Page Pages } passedData := PassedData{ CSRFToken: csrf.GetToken(r), Page: page, } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/pages/templates/editPage.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
func editSuggestion(w http.ResponseWriter, r *http.Request) { trimPath := strings.Trim(r.URL.Path, "/admin/post/suggestion/edit/") postID, _ := strconv.Atoi(trimPath) postID64 := int64(postID) c := appengine.NewContext(r) key := datastore.NewKey(c, "PostSuggestion", "", postID64, nil) var suggestion PostSuggestion datastore.Get(c, key, &suggestion) if suggestion.Content != nil { if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { if r.Method == "POST" { c := appengine.NewContext(r) suggestion.Content = []byte(r.FormValue("ContentString")) datastore.Put(c, datastore.NewKey(c, "PostSuggestion", "", postID64, nil), &suggestion) http.Redirect(w, r, r.Referer(), http.StatusFound) } } suggestion.ID = postID64 suggestion.ContentString = string(suggestion.Content) type PassedData struct { CSRFToken string Suggestion PostSuggestion } passedData := PassedData{ CSRFToken: csrf.GetToken(r), Suggestion: suggestion, } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/admin/sidebar.html")).Execute(passedTemplate, nil) template.Must(template.ParseFiles("templates/admin/suggestion/edit.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) } else { fmt.Fprintln(w, "Böyle bir bilgi yok bro") } }
func managePosts(w http.ResponseWriter, r *http.Request) { if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { if r.Method == "POST" { http.Redirect(w, r, "/admin/post/edit/"+r.FormValue("Sequence"), http.StatusFound) } } type PassedData struct { CSRFToken string } passedData := PassedData{ CSRFToken: csrf.GetToken(r), } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/admin/sidebar.html")).Execute(passedTemplate, nil) template.Must(template.ParseFiles("templates/admin/post/index.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
func newPost(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) u := user.Current(c) if u != nil { if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { if r.Method == "POST" { var post Post keyP := datastore.NewIncompleteKey(c, "Post", nil) var counter Counter keyC := datastore.NewKey(c, "Counter", "", 1, nil) datastore.Get(c, keyC, &counter) counter.Count = counter.Count + 1 // Add Cache Counter mCount := new(bytes.Buffer) encCount := gob.NewEncoder(mCount) encCount.Encode(counter) cache.AddCache(r, "Counter", mCount.Bytes()) c := appengine.NewContext(r) post.Content = []byte(r.FormValue("Content")) post.Sequence = counter.Count datastore.Put(c, keyP, &post) datastore.Put(c, keyC, &counter) } } } type PassedData struct { CSRFToken string } passedData := PassedData{ CSRFToken: csrf.GetToken(r), } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/admin/sidebar.html")).Execute(passedTemplate, nil) template.Must(template.ParseFiles("templates/admin/post/new.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
// Create a new page func newPage(w http.ResponseWriter, r *http.Request) { if csrf.ValidateToken(r, r.FormValue("CSRFToken")) && r.FormValue("url") != "" { c := appengine.NewContext(r) page := Pages{ URL: r.FormValue("url"), Title: r.FormValue("title"), Content: []byte(r.FormValue("content")), } _, err := datastore.Put(c, datastore.NewKey(c, "Pages", r.FormValue("url"), 0, nil), &page) if err != nil { errorHandler.Error(w, r, err.Error()) return } cache.DeleteCache(r, "getAllPages") http.Redirect(w, r, "/admin/pages/", http.StatusFound) } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/pages/templates/newPage.html")).Execute(passedTemplate, csrf.GetToken(r)) render.Render(w, r, passedTemplate) }
// Admin page with the search settings func searchSettings(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) // Write the new settings if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { Settings := Settings{ APIKey: r.FormValue("apikey"), CX: r.FormValue("cx"), } _, err := datastore.Put(c, datastore.NewKey(c, "SearchSettings", "Settings", 0, nil), &Settings) if err != nil { errorHandler.Error(w, r, err.Error()) return } } // Get the Settings key := datastore.NewKey(c, "SearchSettings", "Settings", 0, nil) var settings Settings datastore.Get(c, key, &settings) // Merge the models type PassedData struct { CSRFToken string APIKey string CX string } passedData := PassedData{ CSRFToken: csrf.GetToken(r), APIKey: settings.APIKey, CX: settings.CX, } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/search/templates/settings.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
// Manages the menu func menu(w http.ResponseWriter, r *http.Request) { // Parse the post data if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { cache.DeleteCache(r, "getMenu") c := appengine.NewContext(r) // Check if the user wants to move up if r.FormValue("moveUp") != "" { //get the item to move var navigation Navigation ID, _ := strconv.Atoi(r.FormValue("moveUp")) key := datastore.NewKey(c, "Menu", "", int64(ID), nil) if err := datastore.Get(c, key, &navigation); err != nil { errorHandler.Error(w, r, err.Error()) return } // Move the item over 1 down itemBelowQuery := datastore.NewQuery("Menu").Filter("Order =", navigation.Order-1) var itemBelow []Navigation itemBelowKey, err := itemBelowQuery.GetAll(c, &itemBelow) for i := 0; i < len(itemBelow); i++ { itemBelowStruct := Navigation{ NavTitle: itemBelow[i].NavTitle, NavLink: itemBelow[i].NavLink, Order: itemBelow[i].Order + 1, } _, err = datastore.Put(c, datastore.NewKey(c, "Menu", "", itemBelowKey[i].IntID(), nil), &itemBelowStruct) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } // Move the item 1 up originalItem := Navigation{ NavTitle: navigation.NavTitle, NavLink: navigation.NavLink, Order: navigation.Order - 1, } _, err = datastore.Put(c, datastore.NewKey(c, "Menu", "", int64(ID), nil), &originalItem) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } // Check if the user wants to move down if r.FormValue("moveDown") != "" { //get the item to move var navigation Navigation ID, _ := strconv.Atoi(r.FormValue("moveDown")) key := datastore.NewKey(c, "Menu", "", int64(ID), nil) if err := datastore.Get(c, key, &navigation); err != nil { errorHandler.Error(w, r, err.Error()) return } // Move the item below 1 up itemBelowQuery := datastore.NewQuery("Menu").Filter("Order =", navigation.Order+1) var itemBelow []Navigation itemBelowKey, err := itemBelowQuery.GetAll(c, &itemBelow) for i := 0; i < len(itemBelow); i++ { itemBelowStruct := Navigation{ NavTitle: itemBelow[i].NavTitle, NavLink: itemBelow[i].NavLink, Order: itemBelow[i].Order - 1, } _, err = datastore.Put(c, datastore.NewKey(c, "Menu", "", itemBelowKey[i].IntID(), nil), &itemBelowStruct) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } // Move the item 1 down originalItem := Navigation{ NavTitle: navigation.NavTitle, NavLink: navigation.NavLink, Order: navigation.Order + 1, } _, err = datastore.Put(c, datastore.NewKey(c, "Menu", "", int64(ID), nil), &originalItem) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } // Check if the user wants to delete a menu if r.FormValue("delete") != "" { // delete var navigation Navigation ID, _ := strconv.Atoi(r.FormValue("delete")) key := datastore.NewKey(c, "Menu", "", int64(ID), nil) if err := datastore.Get(c, key, &navigation); err != nil { errorHandler.Error(w, r, err.Error()) return } // Move the item below 1 down itemBelowQuery := datastore.NewQuery("Menu").Filter("Order >", navigation.Order) var itemBelow []Navigation itemBelowKey, err := itemBelowQuery.GetAll(c, &itemBelow) for i := 0; i < len(itemBelow); i++ { itemBelowStruct := Navigation{ NavTitle: itemBelow[i].NavTitle, NavLink: itemBelow[i].NavLink, Order: itemBelow[i].Order - 1, } _, err = datastore.Put(c, datastore.NewKey(c, "Menu", "", itemBelowKey[i].IntID(), nil), &itemBelowStruct) if err != nil { errorHandler.Error(w, r, err.Error()) } } // Delete datastore.Delete(c, key) } NavTitle, NavLink := r.FormValue("NavTitle"), r.FormValue("NavLink") // Check if there is any data if NavTitle != "" && NavLink != "" { c := appengine.NewContext(r) dbQuery := datastore.NewQuery("Menu").Order("Order") dbCount, _ := dbQuery.Count(c) menu := Navigation{ NavTitle: NavTitle, NavLink: NavLink, Order: dbCount + 1, } // We the ID as Key _, err := datastore.Put(c, datastore.NewIncompleteKey(c, "Menu", nil), &menu) if err != nil { errorHandler.Error(w, r, err.Error()) return } } } // Get the menus c := appengine.NewContext(r) q := datastore.NewQuery("Menu").Order("Order") navigation, keys := getAllMenu(r) models := make([]NavigationVM, len(navigation)) for i := 0; i < len(navigation); i++ { if navigation[i].Order == 1 { models[i].First = true } count, _ := q.Count(c) if navigation[i].Order == count { models[i].Latest = true } models[i].Key = keys[i].IntID() models[i].NavTitle = navigation[i].NavTitle models[i].NavLink = navigation[i].NavLink } // Merge the data type PassedData struct { CSRFToken string Navigation []NavigationVM } passedData := PassedData{ CSRFToken: csrf.GetToken(r), Navigation: models, } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/menu/templates/manage.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }