Exemplo n.º 1
0
// 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)
}
Exemplo n.º 2
0
/*
 * 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)
}
Exemplo n.º 3
0
// 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)
}
Exemplo n.º 4
0
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")
	}
}
Exemplo n.º 5
0
// 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)
}
Exemplo n.º 6
0
// 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)
}