示例#1
0
文件: stop.py 项目: gogogo/gogogo-hk
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)
示例#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")

    cache_key = "gogogo_cluster_block_%s" % (prefix)
    cache = memcache.get(cache_key)

    if cache == None:           
        query = Cluster.all(keys_only=True).filter("geohash >=" , prefix ).filter("geohash <=" , prefix + 'zzzzzz')
        
        result = []
        for key in query:
            entity = getCachedEntityOr404(Cluster,key = key)
            del entity['instance']
            del entity['geohash']
            entity['radius'] = "%0.3f" % entity['radius']
            result.append(entity)
        
        cache = {}
        cache['result'] = result
        
        memcache.add(cache_key, cache, _default_cache_time)
        
    result = cache['result']
    
    return ApiResponse(data=result)
示例#3
0
def list(request):
    """
    List all agency
    """
    
    lang = MLStringProperty.get_current_lang(request)
    cache_key = "gogogo_db_agency_list_%d" % lang
    cache = memcache.get(cache_key)

    if cache == None:
        query = Agency.all(keys_only=True)
	
        result = []
        for key in query:
            entity = getCachedEntityOr404(Agency,key = key)
            result.append(trEntity(entity,request))

        cache = {}
        cache['result'] = result
        memcache.add(cache_key, cache, _default_cache_time)
        
    agency_list = cache['result']
           
    t = loader.get_template('gogogo/db/agency/list.html')
    c = RequestContext(
        request,
    {
        'page_title': _("Agency List"),
        'agency_list' : agency_list
    })
            
    return HttpResponse(t.render(c))
示例#4
0
文件: stop.py 项目: gogogo/gogogo-hk
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)
示例#5
0
文件: trip.py 项目: gogogo/gogogo-hk
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
示例#6
0
文件: stop.py 项目: gogogo/gogogo-hk
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))
示例#7
0
def stop(request, stop_id):
    """
	Browse stop information
	"""
    entity = getCachedEntityOr404(Stop, id_or_name=stop_id)

    entity = trEntity(entity, request)
    pathbar = Pathbar(stop=entity)

    parent = None
    if "parent_station" in entity and entity["parent_station"] != None:
        parent = getCachedEntityOr404(Stop, id_or_name=entity["parent_station"].id_or_name())
        parent = trEntity(parent, request)

    t = loader.get_template("gogogo/transit/stop.html")
    c = RequestContext(
        request,
        {"page_title": entity["name"], "pathbar": pathbar, "parent": parent, "stop_kind": "stop", "stop": entity},
    )

    return HttpResponse(t.render(c))
示例#8
0
def search(request,lat0,lng0,lat1,lng1):
	"""
	Cluster searching
	Deprecated

	"""
	
	lat0 = float(lat0)
	lng0 = float(lng0)
	lat1 = float(lat1)
	lng1 = float(lng1)
	
	if lat0 < lat1:
		minlat = lat0
		maxlat = lat1
	else:
		minlat = lat1
		maxlat = lat0
	
	if lng0 < lng1:
		minlng = lng0
		maxlng = lng1
	else:
		minlng = lng1
		maxlng = lng0

	#TODO: Check the distance. Prevent to dump the database that will spend too much bandwidth
	hash0 = str(Geohash( (minlng,minlat) ))
	hash1 = str(Geohash( (maxlng,maxlat) ))
	
	cache_key = "gogogo_cluster_search_%s_%s" % (hash0,hash1)
	cache = memcache.get(cache_key)
	
	if cache == None:
		query = Cluster.all(keys_only=True).filter("geohash >=" , hash0).filter("geohash <=" , hash1)
		
		result = []
		for key in query:
			entity = getCachedEntityOr404(Cluster,id_or_name = key.id_or_name())
			del entity['instance']
			del entity['geohash']
			entity['radius'] = "%0.3f" % entity['radius']
			result.append(entity)
		
		cache = {}
		cache['result'] = result
		
		memcache.add(cache_key, cache, _default_cache_time)
		
	result = cache['result']
	
	return ApiResponse(data=result)
示例#9
0
def get(request):
    if "id" not in request.GET:
        return ApiResponse(error="ID missing")
        
    id = request.GET['id']
        
    try:
        entity = getCachedEntityOr404(Cluster,id_or_name = id)

        del entity['instance']
        return ApiResponse(data=entity)
    except Http404:
        return ApiResponse(error="Cluster not found")
示例#10
0
文件: stop.py 项目: gogogo/gogogo-hk
def get(request):
    if "id" not in request.GET:
        return ApiResponse(error="ID missing")
        
    id = request.GET['id']
        
    try:
        entity = getCachedEntityOr404(Stop,id_or_name = id)
        entity = trEntity(entity,request)
        del entity['instance']
        del entity['geohash']
        return ApiResponse(data=entity)
    except Http404:
        return ApiResponse(error="Stop not found")
示例#11
0
文件: shape.py 项目: gogogo/gogogo-hk
def browse(request,id):
	"""
	Browse the information of a shape
	"""
	entity = getCachedEntityOr404(Shape,id_or_name = id)
	
	t = loader.get_template('gogogo/db/shape/browse.html')
	c = RequestContext(
		request,
	{
        'page_title': entity['id'] ,
        'shape' : entity
    })
    		
	return HttpResponse(t.render(c))	
示例#12
0
def browse(request,id):
    """
    Browse the information of an agency
    """

    agency = getCachedEntityOr404(Agency,id_or_name=id)

    t = loader.get_template('gogogo/db/agency/browse.html')
    c = RequestContext(
        request,
    {
        'page_title': agency['name'] ,
        'agency' : agency
    })
            
    return HttpResponse(t.render(c))
示例#13
0
def browse(request, id):
    """
    Browse a changelog
    """

    entity = getCachedEntityOr404(Changelog, id_or_name=id)
    # entity['id'] = int(id)

    d = simplejson.loads(entity["changes"])

    entity["old_rev"] = d[0]["old"]
    entity["new_rev"] = d[0]["new"]

    return render_to_response(
        request,
        "gogogo/db/changelog/browse.html",
        {"changelog": entity, "reference_link": entity["instance"].reference.get_absolute_url()},
    )