Exemplo n.º 1
0
// Seat a title for a visualization
func setVisualizationTitle(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	vars := mux.Vars(r)
	u := user.Current(c)

	// Check if user is logged in
	if u == nil {
		common.Serve401(w)
		return
	}

	title := r.PostFormValue("title")

	key, err := datastore.DecodeKey(vars["key"])
	if err != nil {
		common.Serve404(w)
		return
	}

	// Check if user is the owner
	if key.Parent().StringID() != u.ID {
		common.Serve403(w)
		return
	}

	// Start a datastore transaction
	var e model.Visualization
	err = datastore.RunInTransaction(c, func(c appengine.Context) error {
		// Get the visualization object
		err = datastore.Get(c, key, &e)
		if err != nil {
			return err
		}

		// Change the title
		e.Title = title

		// Save the visualization object
		key, err = datastore.Put(c, key, &e)
		return err
	}, nil)
	if err != nil {
		if err == datastore.ErrNoSuchEntity {
			common.Serve404(w)
			return
		}
		common.ServeError(c, w, err)
		return
	}
}
Exemplo n.º 2
0
// Upload a file to a visualization
func postVisualizationFile(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	vars := mux.Vars(r)
	u := user.Current(c)

	// Check if user is logged in
	if u == nil {
		common.Serve401(w)
		return
	}

	key, err := datastore.DecodeKey(vars["key"])
	if err != nil {
		common.Serve404(w)
		return
	}

	// Check if user is the owner
	if key.Parent().StringID() != u.ID {
		common.Serve403(w)
		return
	}

	// Start a datastore transaction
	var e model.Visualization
	err = datastore.RunInTransaction(c, func(c appengine.Context) error {
		// Get the visualization object
		err = datastore.Get(c, key, &e)
		if err != nil {
			return err
		}

		if vars["action"] == "delete" {
			e.Files, err = deleteFile(c, r, e.Files)
			if err != nil {
				return err
			}
		} else {
			e.Files, err = uploadFile(c, r, e.Files)
			if err != nil {
				return err
			}
		}

		// Save the visualization object
		key, err = datastore.Put(c, key, &e)
		return err
	}, nil)
	if err != nil {
		if err == datastore.ErrNoSuchEntity {
			common.Serve404(w)
			return
		}
		common.ServeError(c, w, err)
		return
	}

	res := map[string]interface{}{
		"Owner": key.Parent().StringID(),
		"Title": e.Title,
		"Date":  e.Date,
		"Files": e.Files,
	}

	common.WriteJson(c, w, res)
}