def get(self): key = self.request.get("key") client = memcache.Client() chave_memcache = "linha_json_" + key linha_obj = client.get(chave_memcache) if linha_obj is None: linha = Linha.get(db.Key(key)) linha_obj = { "key" : str(linha.key()), "nome" : linha.nome, "url" : linha.url, "hashes" : linha.hashes()} client.add(chave_memcache, linha_obj) self.response.out.write("gerou cache de %s " % chave_memcache) else: self.response.out.write("ja tinha cache de %s " % chave_memcache) chave_memcache = "pontos_json_" + key; pontos_json = client.get(chave_memcache) if pontos_json is None: linha = Linha.get(db.Key(key)) pontos = Ponto.all().filter("linha = ", linha).order("ordem") pontos_json = simplejson.dumps([(ponto.lat, ponto.lng) for ponto in pontos]) client.add(chave_memcache, pontos_json) self.response.out.write("gerou cache de %s " % chave_memcache) else: self.response.out.write("ja tinha cache de %s " % chave_memcache)
def get(self): """Popula os objetos Hash para os pontos de uma linha""" self.response.headers['Content-Type'] = 'text/plain' key = self.request.get("key") linha = Linha.get(db.Key(key)) self.response.out.write('Linha: %s\n' % linha.nome) # Percorre os pontos dessa linha, processando os hashes dos segmentos nearhashAnterior = "" for ponto in linha.pontos: if ponto.nearhash: if ponto.nearhash == nearhashAnterior: self.response.out.write('hash repetido, pulando %s\n' % ponto.nearhash) continue nearhashAnterior = ponto.nearhash # if len(ponto.nearhash) != 6: # self.response.out.write('hash curto, pulando %s\n' % ponto.nearhash) # Se ja existe objeto para o hash, pega ele, senao cria um novo hashLista = Hash.all().filter("hash =", ponto.nearhash).fetch(1) if hashLista: self.response.out.write('Hash: %s ja existia \n' % ponto.nearhash) hash = hashLista[0] else: self.response.out.write('Hash: %s criado \n' % ponto.nearhash) hash = Hash(hash=ponto.nearhash) # Se a linha ainda nao esta associada ao objeto-hash, associa if str(linha.key()) not in hash.linhas: self.response.out.write('Linha adicionada a lista\n') hash.linhas.append(str(linha.key())) hash.put()
def _linha_obj(self, key): """Monta info da linha no formato da resposta, usando o cache se possÃvel""" client = memcache.Client() chave_memcache = "linha_json_" + key linha_obj = client.get(chave_memcache) if linha_obj is None: linha = Linha.get(db.Key(key)) linha_obj = { "key" : str(linha.key()), "nome" : linha.nome, "url" : linha.url, "hashes" : linha.hashes()} client.add(chave_memcache, linha_obj) return linha_obj
def get(self): self.response.headers['Content-Type'] = 'application/json' key = self.request.get("key") chave_memcache = "pontos_json_" + key; client = memcache.Client() pontos_json = client.get(chave_memcache) if pontos_json is None: linha = Linha.get(db.Key(key)) pontos = Ponto.all().filter("linha = ", linha).order("ordem") pontos_json = simplejson.dumps([(ponto.lat, ponto.lng) for ponto in pontos]) client.add(chave_memcache, pontos_json) callback = self.request.get("callback"); if callback: pontos_json = callback + "(" + pontos_json + ");" self.response.out.write(pontos_json)