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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. ' like ': {'name': '$text', 'value_type': str},
  17. ' not like ': {'name': '', 'value_type': str}, # TODO Add the operator
  18. 'OR': {'name': '$or', 'value_type': list},
  19. 'AND': {'name': '$and', 'value_type': list}
  20. }
  21. LODEL_SORT_OPERATORS_MAP = {
  22. 'ASC': pymongo.ASCENDING,
  23. 'DESC': pymongo.DESCENDING
  24. }
  25. ## @brief Returns a collection name given a Emclass name
  26. # @param class_name str : The class name
  27. # @return str
  28. def object_collection_name(class_name):
  29. return ("%s%s" % (collection_prefix['object'], class_name)).lower()
  30. ## @brief converts the query filters into MongoDB filters
  31. # @param query_filters list
  32. # @return dict
  33. # @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
  34. def parse_query_filters(query_filters):
  35. filters_dict = dict()
  36. for query_filter in query_filters:
  37. if isinstance(query_filter, tuple):
  38. filters_dict.update(convert_filter(query_filter))
  39. elif isinstance(query_filter, dict):
  40. query_item = list(query_filter.items())[0]
  41. key = LODEL_OPERATORS_MAP[query_item[0]]
  42. filters_dict.update({key: convert_filter_list(query_item[1])})
  43. else:
  44. # TODO Add an exception management here in case the filter is neither a tuple nor a dict
  45. pass
  46. return filters_dict
  47. ## @brief converts a query filters list into MongoDB filters list
  48. # It is used mainly in case of an "AND" or an "OR"
  49. # @param filters_list list
  50. # @return list
  51. def convert_filter_list(filters_list):
  52. converted_filters_list = list()
  53. for filter_list_item in filters_list:
  54. if isinstance(filter_list_item, tuple):
  55. converted_filters_list.append(convert_filter(filter_list_item))
  56. elif isinstance(filter_list_item, dict):
  57. query_item = list(filter_list_item.items())[0]
  58. key = LODEL_OPERATORS_MAP[query_item[0]]['name']
  59. converted_filters_list.append({key: convert_filter_list(query_item[1])})
  60. return converted_filters_list
  61. ## @brief converts a Lodel query filter into a MongoDB filter
  62. # @param filter tuple : (FIELD, OPERATOR, VALUE) representing the query filter to convert
  63. # @return dict : {KEY: {OPERATOR:VALUE}}
  64. # @todo Add an error management for the operator mismatch
  65. # @todo Add the checks for the type of values authorized in certain mongodb operators, such "$in" for example which takes a list
  66. def convert_filter(filter):
  67. key, operator, value = filter
  68. converted_operator = LODEL_OPERATORS_MAP[operator]['name']
  69. converted_filter = {key: {converted_operator: value}}
  70. return converted_filter
  71. ## @brief Returns a list of sorting options
  72. # @param query_filters_order list
  73. # @return list
  74. def parse_query_order(query_filters_order):
  75. ordering = list()
  76. for query_filter_order in query_filters_order:
  77. field, direction = query_filter_order
  78. ordering.append((field, LODEL_SORT_OPERATORS_MAP[direction]))
  79. return ordering