def put_query(identifier): """Save a query to a prefix tree. Arguments: identifier (str): Part of the key that will be associated with queries. Returns: json'ified True. """ query = request.form.get('query', '') mtree = mongotree.MongoTree(identifier=identifier) mtree.upsert(query.split()) return json.dumps(True)
def delete_query(identifier): """Remove a query (and all subqueries) from the tree. Arguments: identifier (str): Part of the key that will be associated with queries. Returns: json'ified True. """ query = request.form.get('query', '') mtree = mongotree.MongoTree(identifier=identifier) node = mtree.get_node_by_path(query) mtree.remove(node) return json.dumps(True)
def get_all_queries(identifier): """Get all queries. Arguments: identifier (str): Part of the key that will be associated with queries. Returns: list of str representing all queries in the system. """ mtree = mongotree.MongoTree(identifier=identifier) nodes = [] [nodes.extend(mtree.get_leaf_nodes(root)) for root in mtree.get_roots()] if not nodes: return json.dumps( ['No autocompletes are available for identifier %s.' % identifier]) paths = [' '.join(node['path'].split(mtree.SEPARATOR)) for node in nodes] return json.dumps(paths)
def setUp(self): """Initialization.""" self.db_name = 'mongotree_test' self.identifier = 'mongotest' self.tree = mongotree.MongoTree(db_name=self.db_name, identifier=self.identifier)
def options_for_suggestions(identifier): """Get suggestions based on the current query. Arguments: identifier (str): Part of the key that will be associated with queries. Returns: a list containing autocompletes; queries that could possibly autocomplete the current set of tokens. """ print 'OPTIONS_FOR_SUGGESTIONS' try: n_results = json.loads(request.args.get('n')) except: n_results = 10 try: mark = json.loads(request.args.get('mark')) except: mark = False n_results = request.args.get('n', -1) next_token_only = request.args.get('next_token_only', False) mtree = mongotree.MongoTree(identifier=identifier) query_ = request.args.get('query', '') query = query_.split() if not query_: path = '' token = '' elif query_[-1] == ' ': path = query token = '' elif len(query) == 1: path = '' token = query[0] else: path = query[:-1] if len(query) > 1: token = query[-1] else: token = '' print 'path "%s"' % path print 'token "%s"' % token node = mtree.get_node_by_path(path) foo = [] if n_results: n = 0 """ TODO implement children only implement all nodes implement leaves only mark nodes as end nodes when saved """ if node or (not path and token): if not path and token: children = mtree.get_roots() else: children = mtree.get_children(node['path']) for child in children: if n_results: n += 1 if n == n_results: break label = child['label'] if not label.startswith(token): continue #if next_token_only: # foo.append(label) #else: path = child['path'].split(mtree.SEPARATOR) foo.append(' '.join(path)) if mark: mark_l = request.args.get('markL', '__MARKL__') mark_r = request.args.get('markR', '__MARKR__') print 'PATHS', foo return json.dumps(foo)