func deleteCheckin(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) if err := users.CheckPerm(w, r, users.OP_UPDATE); err != nil { return } r.ParseForm() if r.Form["id"] == nil { app.ServeError(c, w, errors.New("Checkin not deleted. ID expected")) return } id, err := strconv.ParseInt(r.Form["id"][0], 10, 64) if err != nil { app.ServeError(c, w, errors.New("Checkin not deleted. Bad ID")) return } k := datastore.NewKey(c, "checkins", "", id, nil) // Elimino punto err = datastore.Delete(c, k) if err != nil { app.ServeError(c, w, errors.New("Checkin not deleted. Entity not found")) return } }
func addUser(w http.ResponseWriter, r *http.Request) { var err error c := appengine.NewContext(r) if err := CheckPerm(w, r, OP_ADMIN); err != nil { return } var nu NUser decoder := json.NewDecoder(r.Body) err = decoder.Decode(&nu) if err != nil { app.ServeError(c, w, err) return } if err = nu.IsValid(); err != nil { app.ServeError(c, w, err) return } key := datastore.NewKey(c, "users", "", 0, nil) key, err = datastore.Put(c, key, &nu) nu.Id = key.IntID() jbody, err := json.Marshal(nu) if err != nil { app.ServeError(c, w, err) return } fmt.Fprintf(w, "%s", string(jbody[:len(jbody)])) }
func getPoint(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) if err := users.CheckPerm(w, r, users.OP_VIEW); err != nil { return } r.ParseForm() if r.Form["id"] != nil { id, err := strconv.ParseInt(r.Form["id"][0], 10, 64) if err != nil { app.ServeError(c, w, errors.New("User not found. Bad ID")) return } var p Point // Busco información del usuario en la base de datos // y relleno el objeto User para enviar k := datastore.NewKey(c, "points", "", id, nil) datastore.Get(c, k, &p) p.Id = k.IntID() msg, _ := json.Marshal(p) fmt.Fprintf(w, "%s", msg) } if r.Form["userId"] != nil { // Busco puntos de un usuario uid, err := strconv.ParseInt(r.Form["userId"][0], 10, 64) if err != nil { app.ServeError(c, w, errors.New("User not found. Bad ID")) return } var points []Point q := datastore.NewQuery("points").Filter("UserId =", uid) keys, _ := q.GetAll(c, &points) for i, key := range keys { points[i].Id = key.IntID() } msg, _ := json.Marshal(points) fmt.Fprintf(w, "%s", msg) } }
func cleanImage(w http.ResponseWriter, r *http.Request) { // Con esta manejador borramos las imagenes que no están asociadas a // ningún punto y han quedado huerfanas c := appengine.NewContext(r) var blobs []blobstore.BlobInfo q := datastore.NewQuery("__BlobInfo__") keys, _ := q.GetAll(c, &blobs) for _, key := range keys { var imgk = appengine.BlobKey(key.StringID()) // Busco algun punto con esa key como imagen var points []points.Point qp := datastore.NewQuery("points").Filter("ImageKey = ", imgk) qp.GetAll(c, &points) if len(points) == 0 { // borro la imagen c.Infof("borro imagen %s", imgk) err := blobstore.Delete(c, imgk) if err != nil { app.ServeError(c, w, err) return } } } }
func newPoint(w http.ResponseWriter, r *http.Request) { var err error c := appengine.NewContext(r) if err := users.CheckPerm(w, r, users.OP_UPDATE); err != nil { return } var p Point decoder := json.NewDecoder(r.Body) err = decoder.Decode(&p) if err != nil { app.ServeError(c, w, err) return } if err = p.IsValid(); err != nil { app.ServeError(c, w, err) return } /* - From API reference in google cloud API: NewKey creates a new key. kind cannot be empty. Either one or both of stringID and intID must be zero. If both are zero, the key returned is incomplete. parent must either be a complete key or nil. */ key := datastore.NewKey(c, "points", "", p.Id, nil) key, err = datastore.Put(c, key, &p) p.Id = key.IntID() c.Infof("%d", key.IntID()) jbody, err := json.Marshal(p) if err != nil { app.ServeError(c, w, err) return } fmt.Fprintf(w, "%s", string(jbody[:len(jbody)])) }
func storeImage(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) if err := users.CheckPerm(w, r, users.OP_UPDATE); err != nil { return } blobs, _, err := blobstore.ParseUpload(r) if err != nil { app.ServeError(c, w, err) return } file := blobs["img"] if len(file) == 0 { app.ServeError(c, w, errors.New(ERR_FILENOTSTORED)) return } key := file[0].BlobKey info, err := blobstore.Stat(c, key) if err != nil { app.ServeError(c, w, err) return } if info.Size > MAXSZIMAGE { blobstore.Delete(c, key) app.ServeError(c, w, errors.New(ERR_FILENOTVALID)) return } /* key,err=resizeImage(c,key) if err != nil { app.ServeError(c, w, errors.New(ERR_FILENOTVALID)) return } */ fmt.Fprintf(w, "%s", string(key)) }
func redirectLogin(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) url, err := user.LoginURL(c, r.URL.String()) if err != nil { app.ServeError(c, w, err) return } w.Header().Set("Location", url) w.WriteHeader(http.StatusFound) }
func newCheckin(w http.ResponseWriter, r *http.Request) { var err error c := appengine.NewContext(r) if err := users.CheckPerm(w, r, users.OP_UPDATE); err != nil { return } var ck Checkin decoder := json.NewDecoder(r.Body) err = decoder.Decode(&ck) if err != nil { app.ServeError(c, w, err) return } if err = ck.IsValid(); err != nil { app.ServeError(c, w, err) return } // Paso del objeto TimeStamp que me llega por JSON a // un time.Time que es el unico compatible con el Datastore ck.DBStamp = time.Time(ck.Stamp) key := datastore.NewKey(c, "checkins", "", 0, nil) key, err = datastore.Put(c, key, &ck) ck.Id = key.IntID() jbody, err := json.Marshal(ck) if err != nil { app.ServeError(c, w, err) return } fmt.Fprintf(w, "%s", string(jbody[:len(jbody)])) }
func deleteImage(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) if err := users.CheckPerm(w, r, users.OP_UPDATE); err != nil { return } key := appengine.BlobKey(r.FormValue("blobKey")) err := blobstore.Delete(c, key) if err != nil { app.ServeError(c, w, err) return } }
func deletePoint(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) if err := users.CheckPerm(w, r, users.OP_UPDATE); err != nil { return } r.ParseForm() if r.Form["id"] == nil { app.ServeError(c, w, errors.New("Point not deleted. ID expected")) return } id, err := strconv.ParseInt(r.Form["id"][0], 10, 64) if err != nil { app.ServeError(c, w, errors.New("Point not deleted. Bad ID")) return } k := datastore.NewKey(c, "points", "", id, nil) // Eliminar imagen asociada a el del blobStore var p Point datastore.Get(c, k, &p) if p.ImageKey != "" { blobstore.Delete(c, appengine.BlobKey(p.ImageKey)) } // Elimino punto err = datastore.Delete(c, k) if err != nil { app.ServeError(c, w, errors.New("Point not deleted. Entity not found")) return } }
func newImage(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) if err := users.CheckPerm(w, r, users.OP_UPDATE); err != nil { return } url, err := blobstore.UploadURL(c, "/images/upload", nil) if err != nil { app.ServeError(c, w, err) return } fmt.Fprintf(w, "%s", url) }
func getUser(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) CheckPerm(w, r, OP_VIEW) r.ParseForm() var nu NUser if r.Form["mail"] != nil { var nus []NUser q := datastore.NewQuery("users").Filter("Mail =", r.Form["mail"][0]) c.Infof(r.Form["mail"][0]) keys, err := q.GetAll(c, &nus) if (len(keys) == 0) || err != nil { app.ServeError(c, w, errors.New("User not found. Bad mail")) return } nu = nus[0] nu.Id = keys[0].IntID() } if r.Form["id"] != nil { id, err := strconv.ParseInt(r.Form["id"][0], 10, 64) if err != nil { app.ServeError(c, w, errors.New("User not found. Bad ID")) return } if id != 0 { // Busco información del usuario en la base de datos // y relleno el objeto User para enviar k := datastore.NewKey(c, "users", "", id, nil) datastore.Get(c, k, &nu) } else { // Busco información del usuario de sesión u := user.Current(c) if u == nil { redirectLogin(w, r) return } q := datastore.NewQuery("users").Filter("Mail =", u.Email) var nusers []NUser keys, _ := q.GetAll(c, &nusers) if len(nusers) <= 0 { app.ServeError(c, w, errors.New("No user id found")) return } nu = nusers[0] nu.Id = keys[0].IntID() } } jbody, err := json.Marshal(nu) if err != nil { app.ServeError(c, w, err) return } fmt.Fprintf(w, "%s", string(jbody[:len(jbody)])) }
func getCheckin(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) if err := users.CheckPerm(w, r, users.OP_VIEW); err != nil { return } r.ParseForm() if r.Form["id"] != nil { id, err := strconv.ParseInt(r.Form["id"][0], 10, 64) if err != nil { app.ServeError(c, w, errors.New("Checkin not found. Bad ID")) return } var ck Checkin // Busco un checkin concreto k := datastore.NewKey(c, "checkins", "", id, nil) datastore.Get(c, k, &ck) msg, _ := json.Marshal(ck) fmt.Fprintf(w, "%s", msg) return } var checkins []Checkin if r.Form["userId"] != nil { // Busco checkins de un usuario uid, err := strconv.ParseInt(r.Form["userId"][0], 10, 64) if err != nil { app.ServeError(c, w, errors.New("User not found. Bad ID")) return } var nu users.NUser k := datastore.NewKey(c, "users", "", uid, nil) datastore.Get(c, k, &nu) q := datastore.NewQuery("checkins").Filter("UserId =", uid).Order("-DBStamp") keys, _ := q.GetAll(c, &checkins) // Relleno claves y fechas en formato TimeStamp para aplanarlas for i, key := range keys { checkins[i].Id = key.IntID() checkins[i].Stamp = TimeStamp(checkins[i].DBStamp) checkins[i].Username = nu.Name } } if r.Form["pointId"] != nil { // Busco checkins de un punto pid, err := strconv.ParseInt(r.Form["pointId"][0], 10, 64) if err != nil { app.ServeError(c, w, errors.New("Point not found. Bad ID")) return } q := datastore.NewQuery("checkins").Filter("PointId =", pid) keys, _ := q.GetAll(c, &checkins) for i, key := range keys { checkins[i].Id = key.IntID() checkins[i].Stamp = TimeStamp(checkins[i].DBStamp) var nu users.NUser k := datastore.NewKey(c, "users", "", checkins[i].UserId, nil) datastore.Get(c, k, &nu) checkins[i].Username = nu.Name } } msg, _ := json.Marshal(checkins) fmt.Fprintf(w, "%s", msg) }