Browse Source

MongoDB datasource debugging + field_list handling changes in leobject -> query -> datasource chain

Yann Weber 8 years ago
parent
commit
703b4adf7e

+ 1
- 3
lodel/leapi/leobject.py View File

@@ -658,9 +658,7 @@ construction and consitency when datas are not complete\n")
658 658
     #@return a list of items (lists of (fieldname, fieldvalue))
659 659
     @classmethod
660 660
     def get(cls, query_filters, field_list=None, order=None, group=None, limit=None, offset=0):
661
-        if field_list is None:
662
-            field_list = cls.fieldnames(True)
663
-        else:
661
+        if field_list is not None:
664 662
             for uid in [ uidname
665 663
                 for uidname in cls.uid_fieldname()
666 664
                 if uidname not in field_list ]:

+ 1
- 1
plugins/mongodb_datasource/confspec.py View File

@@ -4,7 +4,7 @@ from lodel.settings.validator import SettingValidator
4 4
 
5 5
 CONFSPEC = {
6 6
     'lodel2.datasource.mongodb_datasource.*':{
7
-        'read_only': (True, SettingValidator('bool')),
7
+        'read_only': (False, SettingValidator('bool')),
8 8
         'host': ('localhost', SettingValidator('host')),
9 9
         'port': (None, SettingValidator('string')),
10 10
         'db_name':('lodel', SettingValidator('string')),

+ 26
- 8
plugins/mongodb_datasource/datasource.py View File

@@ -13,7 +13,7 @@ from lodel import logger
13 13
 from lodel.leapi.leobject import CLASS_ID_FIELDNAME
14 14
 
15 15
 from . import utils
16
-from .utils import object_collection_name,\
16
+from .utils import object_collection_name, collection_name, \
17 17
     MONGODB_SORT_OPERATORS_MAP, connection_string
18 18
 
19 19
 class MongoDbDataSourceError(Exception):
@@ -133,13 +133,14 @@ class MongoDbDatasource(object):
133 133
         query_result_ordering = None
134 134
         if order is not None:
135 135
             query_result_ordering = utils.parse_query_order(order)
136
-        results_field_list = None if len(field_list) == 0 else field_list
137
-        limit = limit if limit is not None else 0
138
-
136
+        
139 137
         if group is None:
140 138
             cursor = collection.find(
141
-                filter=query_filters, projection=results_field_list,
142
-                skip=offset, limit=limit, sort=query_result_ordering)
139
+                spec = query_filters,
140
+                fields=field_list,
141
+                skip=offset,
142
+                limit=limit if limit != None else 0,
143
+                sort=query_result_ordering)
143 144
         else:
144 145
             pipeline = list()
145 146
             unwinding_list = list()
@@ -252,7 +253,8 @@ class MongoDbDatasource(object):
252 253
                         fname, op, val))
253 254
                     del(new_filters[i])
254 255
             new_filters.append(
255
-                (CLASS_ID_FIELDNAME, '=', target_child.__name__))
256
+                (CLASS_ID_FIELDNAME, '=',
257
+                    collection_name(target_child.__name__)))
256 258
             result += act(
257 259
                 target = target_child,
258 260
                 filters = new_filters,
@@ -436,17 +438,33 @@ class MongoDbDatasource(object):
436 438
     @classmethod
437 439
     def __filters2mongo(cls, filters):
438 440
         res = dict()
441
+        eq_fieldname = [] #Stores field with equal comparison OP
439 442
         for fieldname, op, value in filters:
440 443
             oop = op
441 444
             ovalue = value
442 445
             op, value = cls.__op_value_conv(op, value)
446
+            if op == '=':
447
+                eq_fieldname.append(fieldname)
448
+                if fieldname in res:
449
+                    logger.warning("Dropping previous condition. Overwritten \
450
+by an equality filter")
451
+                res[fieldname] = str(value)
452
+                continue
453
+            if fieldname in eq_fieldname:
454
+                logger.warning("Dropping condition : '%s %s %s'" % (
455
+                    fieldname, op, value))
456
+                continue
457
+
443 458
             if fieldname not in res:
444 459
                 res[fieldname] = dict()
445 460
             if op in res[fieldname]:
446 461
                 logger.warning("Dropping condition : '%s %s %s'" % (
447 462
                     fieldname, op, value))
448 463
             else:
449
-                res[fieldname][op] = value
464
+                if op not in cls.lodel2mongo_op_map:
465
+                    raise ValueError("Invalid operator : '%s'" % op)
466
+                new_op = cls.lodel2mongo_op_map[op]
467
+                res[fieldname][new_op] = value
450 468
         return res
451 469
 
452 470
 

+ 3
- 0
plugins/mongodb_datasource/utils.py View File

@@ -69,6 +69,9 @@ def connect(host, port, db_name, username, password):
69 69
 def object_collection_name(class_object):
70 70
     return class_object.__name__
71 71
 
72
+def collection_name(class_name):
73
+    return class_name
74
+
72 75
 ## @brief Determine a collection field name given a lodel2 fieldname
73 76
 # @note For the moment this method only return the argument but EVERYWHERE
74 77
 # in the datasource we should use this method to gather proper fieldnames

+ 1
- 1
tests/leapi/test_leobject.py View File

@@ -259,7 +259,7 @@ class LeObjectQueryMockTestCase(unittest.TestCase):
259 259
             mock_init.assert_called_once_with(
260 260
                 dyncode.Person,
261 261
                 query_filters = ['lodel_id = 1'],
262
-                field_list = dyncode.Person.fieldnames(True),
262
+                field_list = None,
263 263
                 order = None, group = None, limit = None, offset = 0)
264 264
 
265 265
         with patch.object(

Loading…
Cancel
Save