Example #1
0
def markerwin(request,id):
	"""
	Generate marker window
	"""
	
	cache_key = "gogogo__stop_markerwin_%s" % id #Prefix of memecache key
	
	cache = memcache.get(cache_key)

	if cache == None:

		stop = getCachedEntityOr404(Stop,key_name=id)

		cache = {}
		cache['stop'] = stop
		
		trip_list = []	
	
		
		if  stop['parent_station'] == None:
			station_key = stop['instance'].key()
		else:
			#station_key = db.Key.from_path(Stop.kind(),stop['parent_station'])
			#logging.info(station_key)
			#parent_station = getCachedEntityOr404(Stop,key = station_key)
			station_key = stop['instance'].parent_station.key()
			cache['parent_station'] = createEntity(stop['instance'].parent_station)
		
		q = Trip.all().filter("stop_list = " , station_key)
		for row in q:
			trip = createEntity(row)
			
			trip['route_id'] = trip['instance'].route.key().id_or_name()
			
			trip['agency_id'] = trip['instance'].route.agency.key().id_or_name()
			trip_list.append(trip)
						
		cache['trip_list'] = trip_list
				
		memcache.add(cache_key, cache, _default_cache_time)
		
	stop = trEntity(cache['stop'],request)
	trip_list = [trEntity(trip,request) for trip in cache['trip_list']   ]
	
	parent_station = None
	if 'parent_station' in cache:
		parent_station = trEntity(cache['parent_station'],request)
		
	t = loader.get_template('gogogo/api/stop-markerwin.html')
	c = Context(
	{
        'stop': stop,
        'parent_station' : parent_station,
        'trip_list' : trip_list
	})
    		
	return HttpResponse(t.render(c))
Example #2
0
def block(request):
    if "prefix" not in request.GET:
        return ApiResponse(error="Prefix missing")
        
    prefix = request.GET['prefix']

    if len(prefix) <= 4:
        return ApiResponse(error="The query range is too large")

    lang = MLStringProperty.get_current_lang(request)

    cache_key = "gogogo_stop_block_%d_%s" % (lang,prefix)
    cache = memcache.get(cache_key)

    if cache == None:           
        query = Stop.all(keys_only=True).filter("geohash >=" , prefix ).filter("geohash <=" , prefix + 'zzzzzz')
        
        result = []
        for key in query:
            entity = getCachedEntityOr404(Stop,key = key)
            entity = trEntity(entity,request)
            del entity['instance']
            del entity['geohash']           
            result.append(entity)
        
        cache = {}
        cache['result'] = result
        
        memcache.add(cache_key, cache, _default_cache_time)
        
    result = cache['result']
    
    return ApiResponse(data=result)
Example #3
0
def mget(request):
    """
    Get multiple entities in an ajax call
    """
    if "ids" not in request.GET:
        return ApiResponse(error="ids")
        
    ids = request.GET['ids']

    items = ids.split(",")

    result = []

    for id in items:    
        try:
            entity = getCachedEntityOr404(Stop,id_or_name = id)
            entity = trEntity(entity,request)
            del entity['geohash']
            del entity['instance']
            result.append(entity)

        except Http404:
            pass

    return ApiResponse(data=result)
Example #4
0
def _get(id,request):
    """
    Get a trip entity
    """
    entity = getCachedEntityOr404(Trip,id_or_name = id)
    entity = trEntity(entity,request)
    del entity['instance']
        
    if entity["route"]:
        if isinstance(entity["route"],db.Key):
            route = getCachedEntityOr404(Route,key = entity["route"])
        else:
            route = getCachedEntityOr404(Route,id_or_name = entity["route"])
        route = trEntity(route,request)
        entity['color'] = route["color"]
        entity['name'] = route["long_name"]
        
    return entity
Example #5
0
def get(request):
    if "id" not in request.GET:
        return ApiResponse(error="ID missing")
        
    id = request.GET['id']
        
    try:
        entity = getCachedEntityOr404(Agency,id_or_name = id)
        entity = trEntity(entity,request)
        del entity['instance']

        return ApiResponse(data=entity)
    except Http404:
        return ApiResponse(error="Agency not found")
Example #6
0
def search(request,lat0,lng0,lat1,lng1):
    """
        Search stop (api/stop/search)
        Deprecated
        
    """
    lat0 = float(lat0)
    lng0 = float(lng0)
    lat1 = float(lat1)
    lng1 = float(lng1)

    sw = LatLng(lat0,lng0)
    ne = LatLng(lat1,lng1)
    bounds = LatLngBounds(sw,ne)
        
    #TODO: Check the distance. Prevent to dump the database that will spend too much bandwidth
    hash0 = str(Geohash( (sw.lng,sw.lat) ))
    hash1 = str(Geohash( (ne.lng,ne.lat) ))

    lang = MLStringProperty.get_current_lang(request)

    cache_key = "gogogo_stop_search_%d_%s_%s" % (lang,hash0,hash1)

    cache = memcache.get(cache_key)

    if cache == None:
        result = []

        query = Stop.all().filter("geohash >=" , hash0).filter("geohash <=" , hash1)
        
        for stop in query:
            pt = latlngFromGeoPt(stop.latlng)
            if bounds.containsLatLng(pt):
                entity = createEntity(stop)
                entity = trEntity(entity,request)
                del entity['instance']
                del entity['geohash']
                result.append(entity)
        
        cache = {}
        cache['result'] = result	
        memcache.add(cache_key, cache, _default_cache_time)

    result = cache['result']

    return ApiResponse(data=result)