// Get details about a visualization func getVisualization(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) vars := mux.Vars(r) key, err := datastore.DecodeKey(vars["key"]) if err != nil { common.Serve404(w) return } var e model.Visualization err = datastore.Get(c, key, &e) 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) }
// Get a file that is part of a visualization func getVisualizationFile(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) vars := mux.Vars(r) // Get visualization object key, err := datastore.DecodeKey(vars["key"]) if err != nil { common.Serve404(w) return } var e model.Visualization err = datastore.Get(c, key, &e) if err != nil { if err == datastore.ErrNoSuchEntity { common.Serve404(w) return } common.ServeError(c, w, err) return } // Find blob key filename := vars["filename"] for i := range e.Files { if e.Files[i].Filename == filename { blobstore.Send(w, appengine.BlobKey(e.Files[i].BlobKey)) return } } common.Serve404(w) }
// Get a list of visualizations func listVisualizations(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) q := datastore.NewQuery("visualization").Order("-Date") // Parse filter parameters r.ParseForm() fUser := r.Form["user"] if len(fUser) > 0 && fUser[0] == "me" { u := user.Current(c) if u == nil { common.Serve401(w) return } q = q.Ancestor(model.GetAccountKey(c, u)) } // Get visualizations var e []model.Visualization keys, err := q.GetAll(c, &e) if err != nil { common.ServeError(c, w, err) return } // Prepare output output := make([]map[string]interface{}, 0) for i := range keys { output = append(output, map[string]interface{}{"Key": keys[i], "Title": e[i].Title, "Date": e[i].Date}) } common.WriteJson(c, w, output) }
// Create a new module func newModule(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) u := user.Current(c) acc, err := model.GetAccount(c, u) if err != nil { common.ServeError(c, w, err) return } if acc == nil { common.Serve401(w) return } // Parse form data devname := r.PostFormValue("devname") name := r.PostFormValue("name") // Set user developer name if acc.Devname == "" { if devname == "" { common.Serve404(w) // TODO: maybe 400 instead return } acc.Devname = devname err := model.SaveAccount(c, u, acc) if err != nil { common.ServeError(c, w, err) return } } else { devname = acc.Devname } e := model.Module{devname, name, time.Now(), nil} _, err = datastore.Put(c, datastore.NewKey(c, "module", name, 0, model.GetAccountKey(c, u)), &e) if err != nil { common.ServeError(c, w, err) return } common.WriteJson(c, w, map[string]string{"Devname": devname, "Name": name}) }
// Get an URL for uploading a file to a visualization func getVisualizationFileUploadUrl(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) vars := mux.Vars(r) uploadUrl, err := blobstore.UploadURL(c, "/api/visualizations/"+vars["key"]+"/files/upload", nil) if err != nil { common.ServeError(c, w, err) return } common.WriteJson(c, w, uploadUrl.Path) }
// Serves the AngularJS application func root(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) // Get user and account u := user.Current(c) acc, err := model.GetAccount(c, u) if err != nil { common.ServeError(c, w, err) return } templates.ExecuteTemplate(w, "index", IndexData{u, acc, domain, port}) }
// Get a file that is part of a module func getModuleFile(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) vars := mux.Vars(r) // Get module object accKey, _, err := model.GetAccountByDevname(c, vars["devname"]) if err != nil { common.ServeError(c, w, err) return } if accKey == nil { common.Serve404(w) return } key := datastore.NewKey(c, "module", vars["name"], 0, accKey) var e model.Module err = datastore.Get(c, key, &e) if err != nil { if err == datastore.ErrNoSuchEntity { common.Serve404(w) return } common.ServeError(c, w, err) return } // Find blob key filename := vars["filename"] for i := range e.Files { if e.Files[i].Filename == filename { blobstore.Send(w, appengine.BlobKey(e.Files[i].BlobKey)) return } } common.Serve404(w) }
// 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 } }
// Get details about a module func getModule(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) vars := mux.Vars(r) accKey, _, err := model.GetAccountByDevname(c, vars["devname"]) if err != nil { if err == datastore.ErrNoSuchEntity { common.Serve404(w) return } common.ServeError(c, w, err) return } key := datastore.NewKey(c, "module", vars["name"], 0, accKey) var e model.Module err = datastore.Get(c, key, &e) 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(), "Devname": e.Devname, "Name": e.Name, "Date": e.Date, "Files": e.Files, } common.WriteJson(c, w, res) }
// Create a new visualization func newVisualization(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) u := user.Current(c) if u == nil { common.Serve401(w) return } e := model.Visualization{"Untitled", time.Now(), nil} key, err := datastore.Put(c, datastore.NewIncompleteKey(c, "visualization", model.GetAccountKey(c, u)), &e) if err != nil { common.ServeError(c, w, err) return } common.WriteJson(c, w, map[string]*datastore.Key{"key": key}) }
// Login using Google account func loginHandle(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") c := appengine.NewContext(r) u := user.Current(c) if u == nil { url, err := user.LoginURL(c, "/") if err != nil { common.ServeError(c, w, err) return } w.Header().Set("Location", url) w.WriteHeader(http.StatusFound) return } w.Header().Set("Location", "/") w.WriteHeader(http.StatusFound) }
// Get index file (html) for a visualization func getVisualizationIndex(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) vars := mux.Vars(r) // Get visualization object key, err := datastore.DecodeKey(vars["key"]) if err != nil { common.Serve404(w) return } var e model.Visualization err = datastore.Get(c, key, &e) if err != nil { if err == datastore.ErrNoSuchEntity { common.Serve404(w) return } common.ServeError(c, w, err) return } // Check if avy.js exists exists := false for i := range e.Files { if e.Files[i].Filename == "avy.js" { exists = true break } } if exists { templates.ExecuteTemplate(w, "index", nil) } else { templates.ExecuteTemplate(w, "noavyjs", nil) } }
// 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) }