No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. import pymongo
  3. collection_prefix = {
  4. 'relation': 'rel_',
  5. 'collection': 'class_'
  6. }
  7. LODEL_OPERATORS_MAP = {
  8. '=': {'name': '$eq', 'value_type': None},
  9. '<=': {'name': '$lte', 'value_type': None},
  10. '>=': {'name': '$gte', 'value_type': None},
  11. '!=': {'name': '$ne', 'value_type': None},
  12. '<': {'name': '$lt', 'value_type': None},
  13. '>': {'name': '$gt', 'value_type': None},
  14. 'in': {'name': '$in', 'value_type': list},
  15. 'not in': {'name': '$nin', 'value_type': list},
  16. 'OR': {'name': '$or', 'value_type': list},
  17. 'AND': {'name': '$and', 'value_type': list}
  18. }
  19. LODEL_SORT_OPERATORS_MAP = {
  20. 'ASC': pymongo.ASCENDING,
  21. 'DESC': pymongo.DESCENDING
  22. }
  23. ## @brief Returns a collection name given a Emclass name
  24. # @param class_name str : The class name
  25. # @return str
  26. def object_collection_name(class_name):
  27. return ("%s%s" % (collection_prefix['object'], class_name)).lower()
  28. ## @brief converts the query filters into MongoDB filters
  29. # @param query_filters list : list of query_filters as tuples or dicts
  30. # @param as_list bool : defines if the output will be a list (default: False)
  31. # @return dict|list
  32. # @todo refactor this function by adding a return_type argument (default= dict) which can be a dict or a list, then delete the convert_filter_list function
  33. def parse_query_filters(query_filters, as_list=False):
  34. parsed_filters = dict() if not as_list else list()
  35. for query_filter in query_filters:
  36. if isinstance(query_filter, tuple):
  37. if as_list:
  38. parsed_filters.append(convert_filter(query_filter))
  39. else:
  40. parsed_filters.update(convert_filter(query_filter))
  41. elif isinstance(query_filter, dict):
  42. query_item = list(query_filter.items())[0]
  43. key = LODEL_OPERATORS_MAP[query_item[0]]
  44. if as_list:
  45. parsed_filters.append({key: parse_query_filters(query_item[1], as_list=True)})
  46. else:
  47. parsed_filters.update({key: parse_query_filters(query_item[1], as_list=True)})
  48. else:
  49. # TODO Add an exception management here in case the filter is neither a tuple nor a dict
  50. pass
  51. return parsed_filters
  52. ## @brief converts a Lodel query filter into a MongoDB filter
  53. # @param filter_params tuple : (FIELD, OPERATOR, VALUE) representing the query filter to convert
  54. # @return dict : {KEY: {OPERATOR:VALUE}}
  55. # @todo Add an error management for the operator mismatch
  56. # @todo Add the checks for the type of values authorized in certain mongodb operators, such "$in" for example which takes a list
  57. def convert_filter(filter_params):
  58. key, operator, value = filter_params
  59. if operator not in ('like', 'not like'):
  60. converted_operator = LODEL_OPERATORS_MAP[operator]['name']
  61. converted_filter = {key: {converted_operator: value}}
  62. else:
  63. converted_filter = convert_like_filter(filter_params)
  64. return converted_filter
  65. ## @brief Returns a list of sorting options
  66. # @param query_filters_order list
  67. # @return list
  68. def parse_query_order(query_filters_order):
  69. ordering = list()
  70. for query_filter_order in query_filters_order:
  71. field, direction = query_filter_order
  72. ordering.append((field, LODEL_SORT_OPERATORS_MAP[direction]))
  73. return ordering
  74. ## @brief Converts "like" and "not like" filters into MongotDb filters
  75. # @param like_filter tuple
  76. # @return dict
  77. def convert_like_filter(like_filter):
  78. key, operator, value = like_filter
  79. is_starting_with = value.endswith('*')
  80. is_ending_with = value.startswith('*')
  81. if is_starting_with and not is_ending_with:
  82. regex_pattern = value.replace('*', '^')
  83. elif is_ending_with and not is_starting_with:
  84. regex_pattern = value.replace('*', '$')
  85. elif is_starting_with and is_ending_with:
  86. regex_pattern = '%s' % value
  87. else:
  88. regex_pattern = '^%s$' % value
  89. regex_condition = {'$regex': regex_pattern, '$options': 'i'}
  90. converted_filter = {key: regex_condition}
  91. if operator.startswith('not'):
  92. converted_filter = {key: {'$not': regex_condition}}
  93. return converted_filter