def rate(request): """ Adds the rating to all edges in a route, and saves it both in the structure and in the database. Query args: tag -- the tag of the route you want to rate rating -- a float between 0 and 5 """ tag = request.GET.get('tag') rating = float(request.GET.get('rating')) path = from_string(GRAPH, tag) add_rating_list(GRAPH, [(i, j) for i, j in zip(path, path[1:])], rating) return HttpResponse('')
def parse(request): """ Convert a tag into another type. Query args: tag -- route to be converted. """ tag = request.GET.get('tag') path = from_string(GRAPH, tag) resp = respond_path(request.GET, path, [path[0]]) if resp is None: return HttpResponseNotFound() return HttpResponse(into_json(resp))
def import_json(request): """ Processes a json structure that contains route and applies the score in the MongoDB database. The structure should have the following structure: { 'route_id (e.g. 1)' : { 'length': int, 'score': int, 'name': string, 'tags': arrays[string] } } The data segment should contain the structure (can be done through e.g. curl "<domain>/route/import" --data "@filename") """ print request.body jsonobj = json.loads(request.body) for key in jsonobj: tags = jsonobj[key]['tags'] score = float(jsonobj[key]['score']) for tag in tags: path = from_string(GRAPH, tag) add_rating_list(GRAPH, [(i, j) for i, j in zip(path, path[1:])], score) return HttpResponse('<html><body>Imported ' + file_name + '</body></html>')
def go_home(request): """ Responds with a route leading the user back to his starting point. Query args: tag -- the route that the user used to run. lon, lat -- position of the user. distance -- The preferred distance to the starting point. """ # Get path from request tag tag = request.GET.get('tag') path = from_string(GRAPH, tag) # Find nearest node (start, end) = get_edge_tuple(request, request.GET.get('lat'), request.GET.get('lon')) # Get the preferred distance to run from this point to starting position (0 means straight home) dist_arg = float(request.GET.get('distance')) if dist_arg == 0: dist = distance(serialize_node(GRAPH, end), serialize_node(GRAPH, path[0])) else: dist = dist_arg print dist # Get index of current location and close rod to return if end not in path and start in path: (end, start) = (start, end) elif not (end in path or start in path): for node in serialize_node(GRAPH, end).connections + serialize_node( GRAPH, start).connections: if node.node in path: end = node.node break ind = path.index(end) # Nodes from starting to current position pois_path = path[ind:0:-1] dist = dist - path_length(GRAPH, pois_path) d = {k: v for k, v in request.GET.dict().items()} d['min_length'] = str(dist) d['max_length'] = str(dist + 1.0) config = routing_config.from_dict(DEFAULT_ROUTING_CONFIG, d) # Generate new random rod from starting position nodes = generate_rod(GRAPH, path[0], config) # Create new rod that will be used for the poisoning (starting from current position and contains starting pos) pois_path.extend(nodes) print "tag =", into_string(GRAPH, nodes) # Close the rod on the starting position routes = close_rod(GRAPH, end, pois_path, config, nodes) # Will result in the shortest route returned if dist_arg == 0: routes = sorted(routes, key=len) else: shuffle(routes) print path_length(GRAPH, routes[0]) # Return the new route, i.e. the completed part + new part selected_route = path[0:ind] + routes[0][::-1] resp = respond_path(request.GET, selected_route, [selected_route[0]]) if resp is None: return HttpResponseNotFound() return HttpResponse(into_json(resp))
def test_encode_decode(self): rod = generate_rod(GRAPH, 17263, from_dict(DEFAULT_ROUTING_CONFIG, {})) string = into_string(GRAPH, rod) self.assertEqual(from_string(GRAPH, string), rod)