Browse Source

Added the order options parsing function

Roland Haroutiounian 9 years ago
parent
commit
801f2f0679
2 changed files with 18 additions and 5 deletions
  1. 13
    3
      lodel/datasource/mongodb/datasource.py
  2. 5
    2
      lodel/datasource/mongodb/utils.py

+ 13
- 3
lodel/datasource/mongodb/datasource.py View File

@@ -56,17 +56,26 @@ class MongoDbDataSource(object):
56 56
         return (connection_args['login'], urllib.quote_plus(connection_args['password']), connection_args['host'],
57 57
                 connection_args['port'], connection_args['dbname'])
58 58
 
59
-
60 59
     ## @brief returns a selection of documents from the datasource
61 60
     # @param target_cls Emclass
62 61
     # @param field_list list
62
+    # @param filters list : List of filters
63
+    # @param rel_filters list : List of relational filters
64
+    # @param order list : List of column to order. ex: order = [('title', 'ASC'),]
65
+    # @param group list : List of tupple representing the column to group together. ex: group = [('title', 'ASC'),]
66
+    # @param limit int : Number of records to be returned
67
+    # @param offset int: used with limit to choose the start record
68
+    # @param instanciate bool : If true, the records are returned as instances, else they are returned as dict
69
+    # @return list
70
+    # @todo Implement the grouping
71
+    # @todo Implement the relations
63 72
     def select(self, target_cls, field_list, filters, rel_filters=None, order=None, group=None, limit=None, offset=0,
64 73
                instanciate=True):
65 74
         collection_name = utils.object_collection_name(target_cls.__class__)
66 75
         collection = self.database[collection_name]
67 76
         query_filters = utils.parse_query_filters(filters)
68 77
         query_result_ordering = utils.parse_query_order(order) if order is not None else None
69
-        results_field_list = None if len(field_list) == 0 else field_list  # TODO On peut peut-être utiliser None dans les arguments au lieu d'une liste vide
78
+        results_field_list = None if len(field_list) == 0 else field_list
70 79
         limit = limit if limit is not None else 0
71 80
         cursor = collection.find(
72 81
             filter=query_filters,
@@ -95,8 +104,9 @@ class MongoDbDataSource(object):
95 104
         result = collection.delete_many(uid)
96 105
         return result.deleted_count
97 106
 
107
+    ## @brief updates one or a list of records
108
+    # @todo to be implemented
98 109
     def update(self, target_cls, uid, **datas):
99
-
100 110
         pass
101 111
 
102 112
     ## @brief Inserts a record in a given collection

+ 5
- 2
lodel/datasource/mongodb/utils.py View File

@@ -7,7 +7,6 @@ collection_prefix = {
7 7
     'collection': 'class_'
8 8
 }
9 9
 
10
-# TODO Ajouter ici les conversions vers les opérateurs MongoDB correspondants
11 10
 LODEL_OPERATORS_MAP = {
12 11
     '=': {'name': '$eq', 'value_type': None},
13 12
     '<=': {'name': '$lte', 'value_type': None},
@@ -70,6 +69,7 @@ def convert_filter_list(filters_list):
70 69
             converted_filters_list.append({key: convert_filter_list(query_item[1])})
71 70
     return converted_filters_list
72 71
 
72
+
73 73
 ## @brief converts a Lodel query filter into a MongoDB filter
74 74
 # @param filter tuple : (FIELD, OPERATOR, VALUE) representing the query filter to convert
75 75
 # @return dict : {KEY: {OPERATOR:VALUE}}
@@ -81,10 +81,13 @@ def convert_filter(filter):
81 81
     converted_filter = {key: {converted_operator: value}}
82 82
     return converted_filter
83 83
 
84
+
84 85
 ## @brief Returns a list of sorting options
85 86
 # @param query_filters_order list
86 87
 # @return list
87
-# @todo To be implemented
88 88
 def parse_query_order(query_filters_order):
89 89
     ordering = list()
90
+    for query_filter_order in query_filters_order:
91
+        field, direction = query_filter_order
92
+        ordering.append((field, LODEL_SORT_OPERATORS_MAP[direction]))
90 93
     return ordering

Loading…
Cancel
Save