|
| 1 | +"""Utility functions for MongoDB query construction. |
| 2 | +
|
| 3 | +These functions were originally part of dserver-direct-mongo-plugin but are |
| 4 | +copied here to decouple the dependency-graph-plugin from that package. |
| 5 | +""" |
| 6 | + |
| 7 | +import logging |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +VALID_MONGO_QUERY_KEYS = ( |
| 13 | + "free_text", |
| 14 | + "creator_usernames", |
| 15 | + "base_uris", |
| 16 | + "uuids", |
| 17 | + "tags", |
| 18 | +) |
| 19 | + |
| 20 | +MONGO_QUERY_LIST_KEYS = ( |
| 21 | + "creator_usernames", |
| 22 | + "base_uris", |
| 23 | + "uuids", |
| 24 | + "tags", |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +def _dict_to_mongo(query_dict): |
| 29 | + """Convert a query dictionary to a MongoDB query. |
| 30 | +
|
| 31 | + :param query_dict: Dictionary with query parameters |
| 32 | + :returns: MongoDB query dictionary |
| 33 | + """ |
| 34 | + def _sanitise(query_dict): |
| 35 | + for key in list(query_dict.keys()): |
| 36 | + if key not in VALID_MONGO_QUERY_KEYS: |
| 37 | + del query_dict[key] |
| 38 | + for lk in MONGO_QUERY_LIST_KEYS: |
| 39 | + if lk in query_dict: |
| 40 | + if len(query_dict[lk]) == 0: |
| 41 | + del query_dict[lk] |
| 42 | + |
| 43 | + def _deal_with_possible_or_statment(a_list, key): |
| 44 | + if len(a_list) == 1: |
| 45 | + return {key: a_list[0]} |
| 46 | + else: |
| 47 | + return {"$or": [{key: v} for v in a_list]} |
| 48 | + |
| 49 | + def _deal_with_possible_and_statement(a_list, key): |
| 50 | + if len(a_list) == 1: |
| 51 | + return {key: a_list[0]} |
| 52 | + else: |
| 53 | + return {key: {"$all": a_list}} |
| 54 | + |
| 55 | + _sanitise(query_dict) |
| 56 | + |
| 57 | + sub_queries = [] |
| 58 | + if "free_text" in query_dict: |
| 59 | + sub_queries.append({"$text": {"$search": query_dict["free_text"]}}) |
| 60 | + if "creator_usernames" in query_dict: |
| 61 | + sub_queries.append( |
| 62 | + _deal_with_possible_or_statment( |
| 63 | + query_dict["creator_usernames"], "creator_username" |
| 64 | + ) |
| 65 | + ) |
| 66 | + if "base_uris" in query_dict: |
| 67 | + sub_queries.append( |
| 68 | + _deal_with_possible_or_statment(query_dict["base_uris"], "base_uri") |
| 69 | + ) |
| 70 | + if "uuids" in query_dict: |
| 71 | + sub_queries.append(_deal_with_possible_or_statment(query_dict["uuids"], "uuid")) |
| 72 | + if "tags" in query_dict: |
| 73 | + sub_queries.append( |
| 74 | + _deal_with_possible_and_statement(query_dict["tags"], "tags") |
| 75 | + ) |
| 76 | + |
| 77 | + if len(sub_queries) == 0: |
| 78 | + return {} |
| 79 | + elif len(sub_queries) == 1: |
| 80 | + return sub_queries[0] |
| 81 | + else: |
| 82 | + return {"$and": [q for q in sub_queries]} |
| 83 | + |
| 84 | + |
| 85 | +def _dict_to_mongo_query(query_dict): |
| 86 | + """Construct mongo query, allowing embedding of a raw mongo query. |
| 87 | +
|
| 88 | + Converts a query dictionary to a MongoDB query format. If the query_dict |
| 89 | + contains a 'query' key with a dict value, that raw MongoDB query is |
| 90 | + merged with the constructed query. |
| 91 | +
|
| 92 | + :param query_dict: Dictionary with query parameters. May contain: |
| 93 | + - free_text: Text search string |
| 94 | + - creator_usernames: List of creator usernames |
| 95 | + - base_uris: List of base URIs |
| 96 | + - uuids: List of UUIDs |
| 97 | + - tags: List of tags |
| 98 | + - query: Raw MongoDB query dict (optional) |
| 99 | + :returns: MongoDB query dictionary |
| 100 | + """ |
| 101 | + if "query" in query_dict and isinstance(query_dict["query"], dict): |
| 102 | + raw_mongo = query_dict["query"] |
| 103 | + del query_dict["query"] |
| 104 | + else: |
| 105 | + raw_mongo = {} |
| 106 | + |
| 107 | + mongo_query = _dict_to_mongo(query_dict) |
| 108 | + |
| 109 | + if len(raw_mongo) > 0 and len(mongo_query) == 0: |
| 110 | + mongo_query = raw_mongo |
| 111 | + elif len(raw_mongo) > 0 and len(mongo_query) == 1 and "$and" in mongo_query: |
| 112 | + mongo_query["$and"].append(raw_mongo) |
| 113 | + elif len(raw_mongo) > 0: |
| 114 | + mongo_query = {"$and": [mongo_query, raw_mongo]} |
| 115 | + |
| 116 | + logger.debug("Constructed mongo query: {}".format(mongo_query)) |
| 117 | + return mongo_query |
0 commit comments