Example #1
0
func (self *store) Dequeue(perma_blobref string, blobref string) (blobrefs []string, err os.Error) {
	parent := datastore.NewKey("perma", perma_blobref, 0, nil)
	key := datastore.NewKey("missing", blobref, 0, parent)
	var m missingStruct
	if err = datastore.Get(self.c, key, &m); err != nil {
		if err == datastore.ErrNoSuchEntity {
			return nil, nil
		}
	}
	err = datastore.Delete(self.c, key)
	if err != nil {
		return
	}
	for _, dep := range m.Pending {
		key := datastore.NewKey("pending", dep, 0, parent)
		var p pendingStruct
		if err = datastore.Get(self.c, key, &m); err != nil {
			if err != datastore.ErrNoSuchEntity {
				continue
			}
		}
		p.WaitingForCount--
		if p.WaitingForCount == 0 {
			blobrefs = append(blobrefs, dep)
			datastore.Delete(self.c, key)
		}
	}
	return
}
Example #2
0
func TouchSuc(c appengine.Context, IdSuc string, IdEmp string) error {
	/* llave de Cta-Empresa */
	ce := &CtaEmpresa{IdEmp: IdEmp}
	ceKey := datastore.NewKey(c, "CtaEmpresa", ce.IdEmp, 0, nil)
	err := datastore.Get(c, ceKey, ce)
	if err == datastore.ErrNoSuchEntity {
		return nil
	}

	/* parent de Empresa */
	cta := &Cta{Email: ce.Email}
	ctaKey := datastore.NewKey(c, "Cta", cta.Email, 0, nil)
	err = datastore.Get(c, ctaKey, cta)
	if err == datastore.ErrNoSuchEntity {
		return nil
	}

	/* Key de empresa */
	empKey := datastore.NewKey(c, "Empresa", IdEmp, 0, ctaKey)

	suc := &Sucursal{IdSuc: IdSuc}
	sucKey := datastore.NewKey(c, "Sucursal", IdSuc, 0, empKey)
	err = datastore.Get(c, sucKey, suc)
	if err == datastore.ErrNoSuchEntity {
		return err
	}
	suc.FechaHora = time.Now().Add(time.Duration(GMTADJ) * time.Second)
	if _, err = datastore.Put(c, sucKey, suc); err != nil {
		return err
	}
	return nil
}
Example #3
0
func edit(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	var now Period
	datastore.Get(c, currentSemesterKey(c), &now)
	arr := regexp.MustCompile("_").Split(r.FormValue("id"), 4)
	name := arr[0]
	count, cnterr := strconv.Atoi(arr[1])
	if cnterr != nil {
		log.Println("Something failed with counting for some reason")
	}

	sem, e1 := strconv.Atoi(arr[2])
	yr, e2 := strconv.Atoi(arr[3])
	if e1 != nil || e2 != nil {
		panic("neither year nor sem should be non-ints")
	}
	if now.Semester == sem && now.Year == yr {
		value := r.FormValue("value")
		k := datastore.NewKey(c, "Tile", strint(name, count), 0, tileRootKey(c, sem, yr))
		var uTile Tile
		datastore.Get(c, k, &uTile)
		//	log.Println(lcs.Diff(value, uTile.Desc))
		uTile.Desc = value
		uTile.LastUpdate = time.Now()
		datastore.Put(c, k, &uTile)
		w.Write([]byte(uTile.Desc))
	} else {
		w.Write([]byte("Edits discarded: cannot edit entries after the semester has ended"))
	}
}
Example #4
0
// logHandler displays log text for a given hash.
// It handles paths like "/log/hash".
func logHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-type", "text/plain; charset=utf-8")
	c := contextForRequest(r)
	hash := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
	key := datastore.NewKey(c, "Log", hash, 0, nil)
	l := new(Log)
	if err := datastore.Get(c, key, l); err != nil {
		if err == datastore.ErrNoSuchEntity {
			// Fall back to default namespace;
			// maybe this was on the old dashboard.
			c := appengine.NewContext(r)
			key := datastore.NewKey(c, "Log", hash, 0, nil)
			err = datastore.Get(c, key, l)
		}
		if err != nil {
			logErr(w, r, err)
			return
		}
	}
	b, err := l.Text()
	if err != nil {
		logErr(w, r, err)
		return
	}
	w.Write(b)
}
Example #5
0
// Increment increments the named counter.
func Increment(c appengine.Context, name string) error {
    // Get counter config.
    var cfg counterConfig
    ckey := datastore.NewKey(c, configKind, name, 0, nil)
    err := datastore.RunInTransaction(c, func(c appengine.Context) error {
        err := datastore.Get(c, ckey, &cfg)
        if err == datastore.ErrNoSuchEntity {
            cfg.Shards = defaultShards
            _, err = datastore.Put(c, ckey, &cfg)
        }
        return err
    }, nil)
    if err != nil {
        return err
    }
    err = datastore.RunInTransaction(c, func(c appengine.Context) error {
        shardName := fmt.Sprintf("%v%d", name, rand.Intn(cfg.Shards))
        key := datastore.NewKey(c, shardKind, shardName, 0, nil)
        var s shard
        err := datastore.Get(c, key, &s)
        // A missing entity and a present entity will both work.
        if err != nil && err != datastore.ErrNoSuchEntity {
            return err
        }
        s.Count++
		s.Name = name
        _, err = datastore.Put(c, key, &s)
        return err
    }, nil)
    if err != nil {
        return err
    }
    //memcache.Increment(c, memcacheKey(name), 1, 0)
    return nil
}
Example #6
0
// Métodos de Empresa
func GetEmpresa(c appengine.Context, id string) *Empresa {
	/* llave de Cta-Empresa */
	ce := &CtaEmpresa{IdEmp: id}
	ceKey := datastore.NewKey(c, "CtaEmpresa", ce.IdEmp, 0, nil)
	err := datastore.Get(c, ceKey, ce)
	if err == datastore.ErrNoSuchEntity {
		return nil
	}

	/* parent de Empresa */
	cta := &Cta{Email: ce.Email}
	ctaKey := datastore.NewKey(c, "Cta", cta.Email, 0, nil)
	err = datastore.Get(c, ctaKey, cta)
	if err == datastore.ErrNoSuchEntity {
		return nil
	}

	/* Key de empresa */
	emp := &Empresa{IdEmp: id}
	empKey := datastore.NewKey(c, "Empresa", emp.IdEmp, 0, ctaKey)
	err = datastore.Get(c, empKey, emp)
	if err == datastore.ErrNoSuchEntity {
		return nil
	}
	return emp
}
Example #7
0
func GetEmpSucursales(c appengine.Context, IdEmp string) *[]Sucursal {
	/* llave de Cta-Empresa */
	ce := &CtaEmpresa{IdEmp: IdEmp}
	ceKey := datastore.NewKey(c, "CtaEmpresa", ce.IdEmp, 0, nil)
	err := datastore.Get(c, ceKey, ce)
	if err == datastore.ErrNoSuchEntity {
		return nil
	}

	/* parent de Empresa */
	cta := &Cta{Email: ce.Email}
	ctaKey := datastore.NewKey(c, "Cta", cta.Email, 0, nil)
	err = datastore.Get(c, ctaKey, cta)
	if err == datastore.ErrNoSuchEntity {
		return nil
	}

	/* Key de empresa */
	empKey := datastore.NewKey(c, "Empresa", IdEmp, 0, ctaKey)
	q := datastore.NewQuery("Sucursal").Ancestor(empKey)
	n, _ := q.Count(c)
	sucursales := make([]Sucursal, 0, n)
	if _, err := q.GetAll(c, &sucursales); err != nil {
		return nil
	}
	return &sucursales
}
Example #8
0
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)
	}
}
Example #9
0
// GetProjects returns all projects owned by the given user.
// Optionally also include projects that are shared with the user.
func GetProjects(user string,
	include_shared bool,
	c *appengine.Context) ([]*datastore.Key, []*EncodedProject, error) {

	qr := datastore.NewQuery("EncodedProject").
		Filter("Owner = ", user).
		Order("-Created").Limit(100)

	keyset := make(map[string]bool)

	var projlist []*EncodedProject
	keylist, err := qr.GetAll(*c, &projlist)
	if err != nil {
		(*c).Errorf("GetProjects[1]: %v", err)
		return nil, nil, err
	}

	if !include_shared {
		return keylist, projlist, err
	}

	for _, k := range keylist {
		keyset[k.String()] = true
	}

	// Get project ids that are shared with this user
	ky2 := datastore.NewKey(*c, "SharingByUser", strings.ToLower(user),
		0, nil)
	var spu SharingByUser
	err = datastore.Get(*c, ky2, &spu)
	if err == datastore.ErrNoSuchEntity {
		// No projects shared with this user
		return keylist, projlist, nil
	}
	if err != nil {
		(*c).Errorf("GetProjects[2]: %v", err)
		return nil, nil, err
	}

	// Get the shared projects
	spvl := Clean_split(spu.Projects, ",")
	for _, spv := range spvl {
		ky := datastore.NewKey(*c, "EncodedProject", spv, 0, nil)
		_, ok := keyset[ky.String()]
		if ok {
			continue
		}
		keyset[ky.String()] = true
		pr := new(EncodedProject)
		err = datastore.Get(*c, ky, pr)
		if err != nil {
			(*c).Infof("GetProjects [3]: %v\n%v", spv, err)
			continue
		}
		keylist = append(keylist, ky)
		projlist = append(projlist, pr)
	}
	return keylist, projlist, nil
}
Example #10
0
func SetProperty(c appengine.Context, ref ArticleRef, propertyName string, propertyValue bool) ([]string, error) {
	articleKey, err := ref.key(c)
	if err != nil {
		return nil, err
	}

	article := new(Article)
	if err := datastore.Get(c, articleKey, article); err != nil && !IsFieldMismatch(err) {
		return nil, err
	}

	if propertyValue != article.HasProperty(propertyName) {
		wasUnread := article.IsUnread()
		wasLiked := article.IsLiked()
		unreadDelta := 0

		article.SetProperty(propertyName, propertyValue)

		// Update unread counts if necessary
		if wasUnread != article.IsUnread() {
			if wasUnread {
				unreadDelta = -1
			} else {
				unreadDelta = 1
			}
		}

		if _, err := datastore.Put(c, articleKey, article); err != nil {
			return nil, err
		}

		if wasLiked != article.IsLiked() {
			if wasLiked {
				article.updateLikeCount(c, -1)
			} else {
				article.updateLikeCount(c, 1)
			}
		}

		if unreadDelta != 0 {
			// Update unread counts - not critical
			subscriptionKey := articleKey.Parent()
			subscription := new(Subscription)

			if err := datastore.Get(c, subscriptionKey, subscription); err != nil {
				c.Warningf("Unread count update failed: subscription read error (%s)", err)
			} else if subscription.UnreadCount+unreadDelta >= 0 {
				subscription.UnreadCount += unreadDelta
				if _, err := datastore.Put(c, subscriptionKey, subscription); err != nil {
					c.Warningf("Unread count update failed: subscription write error (%s)", err)
				}
			}
		}
	}

	return article.Properties, nil
}
Example #11
0
func handleOpen(r *http.Request, c appengine.Context, u *user.User) (
	interface{}, error) {
	if r.Method != "POST" {
		return nil, ErrMethodNotAllowed
	}
	k, err := ds.DecodeKey(r.FormValue("doorKey"))
	if err != nil {
		return nil, ErrNoDoorKey
	}

	cnt := 0
	if cnt, err = ds.NewQuery("Permission").
		Filter("userKey=", ds.NewKey(c, "User", u.ID, 0, nil)).
		Filter("doorKey=", k).
		Filter("level>=", LevelOpener).
		Count(c); err != nil {
		return nil, err
	}
	if cnt == 0 {
		return nil, ErrNoOpenerPermission
	}

	config := Config{}
	if err = ds.Get(c, ds.NewKey(c, "Config", "singleton", 0, nil), &config); err != nil {
		if err == ds.ErrNoSuchEntity {
			err = ErrNoConfig
		}
		return nil, err
	}
	if config.ApiKey == "" {
		return nil, ErrNoConfig
	}

	door := Door{}
	if err = ds.Get(c, k, &door); err != nil {
		if err == ds.ErrNoSuchEntity {
			err = ErrNoOpenerPermission
		}
		return nil, err
	}

	client := urlfetch.Client(c)
	sender := gcm.Sender{config.ApiKey, client}
	msg := &gcm.Message{
		RegistrationIDs: []string{door.RegId},
		Data:            map[string]string{"action": "OPEN"}}

	var response *gcm.Response
	response, err = sender.SendNoRetry(msg)
	if err != nil {
		c.Errorf("Error while sending message to GCM service: %s", err.Error())
		return nil, Err{"Failed to open door.", http.StatusInternalServerError}
	}

	return response, nil
}
Example #12
0
// Increment increments the named counter.
func Increment(c appengine.Context, valName string) error {

	// Get counter config.
	wNumShards := dsu.WrapInt{}
	dsu.McacheGet(c, mCKNumShards(valName), &wNumShards)
	if wNumShards.I < 1 {
		ckey := datastore.NewKey(c, dsKindNumShards, mCKNumShards(valName), 0, nil)
		errTx := datastore.RunInTransaction(c,
			func(c appengine.Context) error {
				err := datastore.Get(c, ckey, &wNumShards)
				if err == datastore.ErrNoSuchEntity {
					wNumShards.I = defaultNumShards
					_, err = datastore.Put(c, ckey, &wNumShards)
				}
				return err
			}, nil)
		if errTx != nil {
			return errTx
		}
		dsu.McacheSet(c, mCKNumShards(valName), dsu.WrapInt{wNumShards.I})
	}

	// pick random counter and increment it
	errTx := datastore.RunInTransaction(c,
		func(c appengine.Context) error {
			shardId := rand.Intn(wNumShards.I)
			dsKey := datastore.NewKey(c, dsKindShard, dSKSingleShard(valName, shardId), 0, nil)
			var sd WrapShardData
			err := datastore.Get(c, dsKey, &sd)
			// A missing entity and a present entity will both work.
			if err != nil && err != datastore.ErrNoSuchEntity {
				return err
			}
			sd.Name = valName
			sd.ShardId = shardId
			sd.I++
			_, err = datastore.Put(c, dsKey, &sd)
			c.Infof("ds put %v %v", dsKey, sd)
			return err
		}, nil)
	if errTx != nil {
		return errTx
	}

	memcache.Increment(c, mCKValue(valName), 1, 0)

	// collect number of updates
	//    per valName per instance in memory
	//    for every interval of 10 minutes
	//
	//  a batch job checks if the number of shards should be increased or decreased
	//    and truncates this map
	updateSamplingFrequency[valName+util.TimeMarker()[:len("2006-01-02 15:0")]] += 1

	return nil
}
Example #13
0
func viewpage(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	url := r.URL.Path[len(viewpage_prefix):]
	if url == "" || strings.HasSuffix(url, "/") {
		url += "index"
	}
	hostkey := datastore.NewKey(c, "Host", r.URL.Host+"/", 0, nil)

	// check for a static first
	static := &Static{Url: url}
	key := datastore.NewKey(c, "Static", static.Key(), 0, hostkey)
	err := datastore.Get(c, key, static)
	if err != nil && err != datastore.ErrNoSuchEntity {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	} else if err == nil {
		static.PostLoad()

		// guess the MIME type based on the extension
		if dot := strings.LastIndex(static.Url, "."); dot >= 0 && len(static.Url) > dot+1 {
			ext := static.Url[dot:]
			if mimetype := mime.TypeByExtension(ext); mimetype != "" {
				w.Header()["Content-Type"] = []string{mimetype}
			} else {
				w.Header()["Content-Type"] = []string{http.DetectContentType(static.Contents)}
			}
		}
		w.Write(static.Contents)
		return
	}

	// try a rendered page
	page := NewPage(url)
	if !page.Validate() {
		http.Error(w, "Invalid page URL", http.StatusBadRequest)
		return
	}

	// get the page
	key = datastore.NewKey(c, "Page", page.Key(), 0, hostkey)
	err = datastore.Get(c, key, page)
	if err != nil && err == datastore.ErrNoSuchEntity {
		http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
		return
	} else if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	page.PostLoad()
	page.Render()
	if err = render(c, w, r.URL.Host, page); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
Example #14
0
// Remove_sharing removes the given users from the access list for the given project.
func Remove_sharing(project_name string,
	user_names []string,
	c *appengine.Context) error {

	// Map whose keys are the users to remove.
	rmu := make(map[string]bool)
	for i := 0; i < len(user_names); i++ {
		rmu[user_names[i]] = true
	}

	// Update SharingByProject.
	key := datastore.NewKey(*c, "SharingByProject", project_name, 0, nil)
	sproj := new(SharingByProject)
	err := datastore.Get(*c, key, sproj)
	if err == datastore.ErrNoSuchEntity {
		// OK
	} else if err != nil {
		return err
	} else {
		users := Clean_split(sproj.Users, ",")
		users = unique_svec(users)
		users = sdiff(users, rmu)
		sproj.Users = strings.Join(users, ",")
		_, err = datastore.Put(*c, key, sproj)
		if err != nil {
			return err
		}
	}

	// Update SharingByUser.
	for _, name := range user_names {
		pkey := datastore.NewKey(*c, "SharingByUser", strings.ToLower(name), 0, nil)
		suser := new(SharingByUser)
		err := datastore.Get(*c, pkey, suser)
		if err == datastore.ErrNoSuchEntity {
			// should not reach here
		} else if err != nil {
			return err
		} else {
			projlist := Clean_split(suser.Projects, ",")
			projlist = unique_svec(projlist)
			projlist = sdiff(projlist, map[string]bool{project_name: true})
			suser.Projects = strings.Join(projlist, ",")

			_, err = datastore.Put(*c, pkey, suser)
			if err != nil {
				return err
			}
		}
	}

	return nil
}
Example #15
0
func SetTags(c appengine.Context, ref ArticleRef, tags []string) ([]string, error) {
	articleKey, err := ref.key(c)
	if err != nil {
		return nil, err
	}

	article := new(Article)
	if err := datastore.Get(c, articleKey, article); err != nil && !IsFieldMismatch(err) {
		return nil, err
	}

	article.Tags = tags

	if _, err := datastore.Put(c, articleKey, article); err != nil {
		return nil, err
	}

	var userKey *datastore.Key
	if key, err := ref.UserID.key(c); err != nil {
		return nil, err
	} else {
		userKey = key
	}

	batchWriter := NewBatchWriter(c, BatchPut)
	for _, tagTitle := range tags {
		tagKey := datastore.NewKey(c, "Tag", tagTitle, 0, userKey)
		tag := Tag{}

		if err := datastore.Get(c, tagKey, &tag); err == nil || IsFieldMismatch(err) {
			// Already available
		} else if err == datastore.ErrNoSuchEntity {
			// Not yet available - add it
			tag.Title = tagTitle
			tag.Created = time.Now()

			if err := batchWriter.Enqueue(tagKey, &tag); err != nil {
				c.Errorf("Error queueing tag for batch write: %s", err)
				return nil, err
			}
		} else {
			// Some other error
			return nil, err
		}
	}

	if err := batchWriter.Flush(); err != nil {
		c.Errorf("Error flushing batch queue: %s", err)
		return nil, err
	}

	return article.Tags, nil
}
Example #16
0
func matcherVagas(c appengine.Context, r render.Render, req *http.Request) {
	log.Println(req.FormValue("vaga"))
	vagaId, err := strconv.ParseInt(req.FormValue("vaga"), 10, 64)
	if err != nil {
		// log.Println(err)
		// r.JSON(http.StatusInternalServerError, err.Error())
		return
	}
	vagaKey := datastore.NewKey(c, "Vaga", "", vagaId, nil)
	vaga := new(Vaga)
	err = datastore.Get(c, vagaKey, vaga)
	if err != nil {
		log.Println(err)
		r.JSON(http.StatusInternalServerError, err.Error())
		return
	}

	var curriculos []Curriculo
	q := datastore.NewQuery("Curriculo")
	_, err = q.GetAll(c, &curriculos)
	if err != nil {
		log.Println(err)
		r.JSON(http.StatusInternalServerError, err.Error())
		return
	}

	empresaKey := datastore.NewKey(c, "Empresa", vaga.Email, 0, nil)
	empresa := new(Empresa)
	err = datastore.Get(c, empresaKey, empresa)
	if err != nil {
		log.Println(err)
		r.JSON(http.StatusInternalServerError, err.Error())
		return
	}

	for _, curriculo := range curriculos {
		analise := &Analise{
			EmpresaId:     vaga.Email,
			VagaId:        vagaKey.IntID(),
			CurriculoId:   curriculo.Email,
			EmpresaNome:   empresa.Nome,
			VagaTitulo:    vaga.Titulo,
			CurriculoNome: curriculo.Nome,
		}

		necessidades := vaga.Habilidades
		capacidades := curriculo.Habilidades
		go matchSuperDesinteressante(necessidades, capacidades, analise, c)
	}

	r.JSON(http.StatusOK, "success")
}
Example #17
0
func TestDatastoreFixture(t *testing.T) {
	filepath := mkTempfile(`[{
    "_kind": "FixtureKind",
    "_key": "key1",
    "IntValue": 10,
    "FloatValue": 2.4,
    "BoolValue": true,
    "StringValue": "foobar",
    "BytesValue": "[]bytesfoobar",
    "DateTimeValue": "2014-01-02T14:02:50Z",
    "DateValue": "2014-01-02",
    "Children": [{
      "_kind": "FixtureKindChildren",
      "_key": "keyc1",
      "Foo": "bar"
    }]
  },{
    "_kind": "FixtureKind",
    "_key": "key1",
    "_ns": "ns1",
    "StringValue": "withns1"
  }
]`)

	assert := wcg.NewAssert(t)
	RunTestServer(func(ts *TestServer) {
		var fk FixtureKind
		var fkc1 FixtureKindChildren
		DatastoreFixture(ts.Context, filepath, nil)
		key := datastore.NewKey(ts.Context, "FixtureKind", "key1", 0, nil)
		keyc1 := datastore.NewKey(ts.Context, "FixtureKindChildren", "keyc1", 0, key)

		assert.Nil(datastore.Get(ts.Context, key, &fk), "datastore.Get('key1') ")
		assert.Nil(datastore.Get(ts.Context, keyc1, &fkc1), "datastore.Get('keyc1') ")

		assert.EqInt(10, fk.IntValue, "IntValue should be 10")
		assert.EqFloat32(2.4, fk.FloatValue, "FloatValue should be 2.4")
		assert.EqStr("foobar", fk.StringValue, "StringValue should be 'foobar'")
		assert.EqStr("bytesfoobar", string(fk.BytesValue), "BytesValue should be 'foobar'")

		assert.EqTime(time.Date(2014, 01, 02, 14, 02, 50, 0, time.UTC), fk.DateTimeValue, "DateTimeValue should be 2014-01-02T14:02:50Z")
		assert.EqTime(time.Date(2014, 01, 02, 0, 0, 0, 0, time.UTC), fk.DateValue, "DateTimeValue should be 2014-01-02T00:00:00Z")

		// namespace
		ns1, _ := appengine.Namespace(ts.Context, "ns1")
		key = datastore.NewKey(ns1, "FixtureKind", "key1", 0, nil)
		assert.Nil(datastore.Get(ns1, key, &fk), "datastore.Get('key1') /w ns1")
		assert.EqStr("withns1", fk.StringValue, "StringValue should be 'withns1'")
	})

}
Example #18
0
func SavePortalHttp(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	vars := mux.Vars(r)
	stringkey := vars["key"]
	defer r.Body.Close()
	body, _ := ioutil.ReadAll(r.Body)
	var portal Portal
	err := json.Unmarshal(body, &portal)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	if !portalExists(c, portal.Id) {
		c.Infof("Saving new portal %s id %s", portal.Title, portal.Id)
		portal.Address = GetGeoCode(c, portal.Lat/1000000, portal.Lon/1000000)
	} else {
		c.Infof("Portal EXISTS!!!")
		var existingPortal Portal
		if err := datastore.Get(c, datastore.NewKey(c, "Portal", portal.Id, 0, nil), &existingPortal); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		portal.Address = existingPortal.Address
		if len(portal.Address) == 0 {
			portal.Address = GetGeoCode(c, portal.Lat/1000000, portal.Lon/1000000)
		} else {
			c.Infof("Dato geoCode ya existe, no lo consulto de nuevo")
		}
	}

	key, err := portal.save(c, stringkey)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	var p1 Portal
	if err := datastore.Get(c, key, &p1); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	c.Infof("Success ! key = %s", key)
	b, _ := json.Marshal(portal)
	w.Header().Set("content-type", "application/json")
	fmt.Fprintf(w, string(b))

}
Example #19
0
func getNoteChunk(c appengine.Context, notebookID int64, period string) (*datastore.Key, NoteChunk, error) {
	var chunk NoteChunk
	notebookKey := datastore.NewKey(c, "Notebook", "", notebookID, nil)
	chunkKey := datastore.NewKey(c, "NoteChunk", period, 0, notebookKey)
	err := datastore.Get(c, chunkKey, &chunk)
	return chunkKey, chunk, err
}
Example #20
0
func accept(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	id := r.FormValue("id")
	intID, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	pkg := &Package{}
	key := datastore.NewKey(c, "Package", "", intID, nil)
	err = datastore.Get(c, key, pkg)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	// check if the package is already present
	acceptQuery := datastore.NewQuery("Package").
		Filter("Accepted =", true).
		Filter("Repo =", pkg.Repo)

	var packages []*Package
	keys, err := acceptQuery.GetAll(c, &packages)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if len(packages) > 1 {
		// just print an error to let admin know
		c.Errorf("More tha one package for repo: %v", pkg.Repo)
	}

	if len(packages) > 0 {
		// update the package and delete
		oldKey := keys[0]
		oldPkg := packages[0]
		oldPkg.Name = pkg.Name
		oldPkg.Description = pkg.Description
		oldPkg.IsLibrary = pkg.IsLibrary
		oldPkg.Category = pkg.Category
		oldPkg.Updated = time.Now()
		if _, err = datastore.Put(c, oldKey, oldPkg); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		if err = datastore.Delete(c, key); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	} else {
		// accept the new package
		pkg.Accepted = true
		if _, err = datastore.Put(c, key, pkg); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}
	memcache.Delete(c, pkg.Repo)
	memcache.Delete(c, CAT)
	memcache.Delete(c, ALL_QUERY)
}
Example #21
0
// commitHandler retrieves commit data or records a new commit.
//
// For GET requests it returns a Commit value for the specified
// packagePath and hash.
//
// For POST requests it reads a JSON-encoded Commit value from the request
// body and creates a new Commit entity. It also updates the "tip" Tag for
// each new commit at tip.
//
// This handler is used by a gobuilder process in -commit mode.
func commitHandler(r *http.Request) (interface{}, error) {
	c := appengine.NewContext(r)
	com := new(Commit)

	if r.Method == "GET" {
		com.PackagePath = r.FormValue("packagePath")
		com.Hash = r.FormValue("hash")
		if err := datastore.Get(c, com.Key(c), com); err != nil {
			return nil, fmt.Errorf("getting Commit: %v", err)
		}
		return com, nil
	}
	if r.Method != "POST" {
		return nil, errBadMethod(r.Method)
	}

	// POST request
	defer r.Body.Close()
	if err := json.NewDecoder(r.Body).Decode(com); err != nil {
		return nil, fmt.Errorf("decoding Body: %v", err)
	}
	if len(com.Desc) > maxDatastoreStringLen {
		com.Desc = com.Desc[:maxDatastoreStringLen]
	}
	if err := com.Valid(); err != nil {
		return nil, fmt.Errorf("validating Commit: %v", err)
	}
	defer cache.Tick(c)
	tx := func(c appengine.Context) error {
		return addCommit(c, com)
	}
	return nil, datastore.RunInTransaction(c, tx, nil)
}
Example #22
0
func GetState(c appengine.Context, table *Table, name string, show bool) (*State, os.Error) {
	rval := new(State)
	rval.TypeName = "state"
	rval.Table = table.Name
	rval.Round = table.round
	rval.Phase = table.phase
	rval.HandPot = table.handpot
	rval.SabaccPot = table.sabaccpot
	rval.Players = make([]Player, len(table.Players))
	for i := range table.Players {
		pname := &table.Players[i]
		pkey := datastore.NewKey(c, "Player", *pname+table.Name, 0, nil)
		player := &rval.Players[i]
		err := datastore.Get(c, pkey, player)
		if err != nil {
			// TODO recover?
			return nil, err
		}
		if *pname != name && (!show || player.Folded) {
			// remove private info from player object
			player.HandCards = nil
		}
	}
	return rval, nil
}
Example #23
0
// imgHandler is the HTTP handler for displaying images and painting
// moustaches; it handles "/img".
func imgHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	key := datastore.NewKey(c, "Image", r.FormValue("id"), 0, nil)
	im := new(Image)
	if err := datastore.Get(c, key, im); err != nil {
		writeError(w, r, err)
		return
	}

	m, _, err := image.Decode(bytes.NewBuffer(im.Data))
	if err != nil {
		writeError(w, r, err)
		return
	}

	get := func(n string) int { // helper closure
		i, _ := strconv.Atoi(r.FormValue(n))
		return i
	}
	x, y, s, d := get("x"), get("y"), get("s"), get("d")

	if x > 0 { // only draw if coordinates provided
		m = moustache(m, x, y, s, d)
	}

	b := &bytes.Buffer{}
	if err := jpeg.Encode(w, m, nil); err != nil {
		writeError(w, r, err)
		return
	}
	w.Header().Set("Content-type", "image/jpeg")
	b.WriteTo(w)
}
Example #24
0
func initHandler(w http.ResponseWriter, r *http.Request) {
	d := dashboardForRequest(r)
	c := d.Context(appengine.NewContext(r))
	defer cache.Tick(c)
	for _, p := range d.Packages {
		err := datastore.Get(c, p.Key(c), new(Package))
		if _, ok := err.(*datastore.ErrFieldMismatch); ok {
			// Some fields have been removed, so it's okay to ignore this error.
			err = nil
		}
		if err == nil {
			continue
		} else if err != datastore.ErrNoSuchEntity {
			logErr(w, r, err)
			return
		}
		if _, err := datastore.Put(c, p.Key(c), p); err != nil {
			logErr(w, r, err)
			return
		}
	}

	// Create secret key.
	key.Secret(c)

	fmt.Fprint(w, "OK")
}
Example #25
0
// Sign verifies or rejects a U2F response.
func Sign(ctx appengine.Context, userIdentity string, signResp u2f.SignResponse) error {
	ckey := makeKey(ctx, userIdentity, "SignChallenge")
	var challenge u2f.Challenge

	// Load the Challenge for this user
	if err := datastore.Get(ctx, ckey, &challenge); err != nil {
		return fmt.Errorf("datastore.Get error: %v", err)
	}

	// Load the Registrations
	keys, regis, err := loadRegistrations(ctx, userIdentity)
	if err != nil {
		return fmt.Errorf("loadRegistrations error %+v", err)
	}

	// Check each Registration
	for idx, regi := range regis {
		if err := testSignChallenge(challenge, *regi, signResp); err != nil {
			return fmt.Errorf("Sign error: %v", err)
		} else {
			// Update the counter for the regi.
			if _, err := datastore.Put(ctx, ckey, keys[idx]); err != nil {
				return fmt.Errorf("datastore.Put error: %v", err)
			}

			// Success -- A U2F response to a sign challenge succeeded.
			return nil
		}
		return fmt.Errorf("Challenge failed for known registrations.")
	}

	return nil
}
Example #26
0
func getPoll(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)

	r.ParseForm()

	i, err := strconv.ParseInt(r.Form.Get("Poll"), 10, 64)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	k := datastore.NewKey(c, "poll", "", i, nil)

	var poll Poll
	if err = datastore.Get(c, k, &poll); err != nil {
		c.Infof("Send:", err)
		fmt.Fprintf(w, "{ \"Status\":\"Fail\", \"Reason\":\"Poll could not be fetched.\" }")
		return
	}

	jsonPoll, _ := json.Marshal(poll)

	fmt.Fprintf(w, string(jsonPoll))

}
Example #27
0
func (db *Db) DeleteStoriesNotIn(ids []int) {
	allKeys := db.getAllStoryKeys()

	var keysToKeep = make([]*datastore.Key, len(ids))
	for _, id := range ids {
		keysToKeep = append(keysToKeep, db.keyForStory(id))
	}

	keysToRemove := db.findDiff(allKeys, keysToKeep)

	db.context.Debugf("Found keys to remove: %v", keysToRemove)

	if len(keysToRemove) > 0 {
		// Delete comments for story
		for _, key := range keysToRemove {
			story := new(Story)
			if err := datastore.Get(db.context, key, story); err != nil {
				db.context.Errorf("Error getting story: %v", err)
			} else {
				db.deleteComments(story.Kids)
			}
		}

		// Delete our story items
		if err := datastore.DeleteMulti(db.context, keysToRemove); err != nil {
			db.context.Errorf("Error deleting old stories: %v", err)
		}
	}
}
Example #28
0
// Loads data
func loadPage(title string, r *http.Request) (*Page, error) {
	c := appengine.NewContext(r)
	key := datastore.NewKey(c, "Pages", title, 0, nil)
	p := new(Page)
	err := datastore.Get(c, key, p)
	return p, err
}
Example #29
0
func GetCardByKey(keyStr string, c appengine.Context) (card Card, err error) {
	k := datastore.NewKey(c, "Card", keyStr, 0, nil)
	e := Card{}
	err = datastore.Get(c, k, &e)
	e.KeyName = k.StringID()
	return e, err
}
Example #30
0
func showFeed(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	url, err := url.QueryUnescape(r.URL.RawQuery)
	if err != nil {
		handleError(w, err)
		return
	}
	feedRoot := datastore.NewKey(c, "feedRoot", "feedRoot", 0, nil)
	fk := datastore.NewKey(c, "feed", url, 0, feedRoot)
	f := new(RSS)
	err = datastore.Get(c, fk, f)
	if err != nil {
		handleError(w, err)
		return
	}
	_, err = datastore.NewQuery("item").Ancestor(fk).Order("-PubDate").GetAll(c, &f.Items)
	if err != nil {
		handleError(w, err)
		return
	}
	templ, err := template.New("showFeed").Parse(feedPageRaw)
	if err != nil {
		handleError(w, err)
		return
	}
	err = templ.Execute(w, f)
	if err != nil {
		handleError(w, err)
		return
	}
}