1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2025-10-31 19:49:02 +01:00

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

This commit is contained in:
Yann 2016-06-30 14:15:50 +02:00
commit 703b4adf7e
5 changed files with 32 additions and 13 deletions

View file

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

View file

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

View file

@ -13,7 +13,7 @@ from lodel import logger
from lodel.leapi.leobject import CLASS_ID_FIELDNAME from lodel.leapi.leobject import CLASS_ID_FIELDNAME
from . import utils from . import utils
from .utils import object_collection_name,\ from .utils import object_collection_name, collection_name, \
MONGODB_SORT_OPERATORS_MAP, connection_string MONGODB_SORT_OPERATORS_MAP, connection_string
class MongoDbDataSourceError(Exception): class MongoDbDataSourceError(Exception):
@ -133,13 +133,14 @@ class MongoDbDatasource(object):
query_result_ordering = None query_result_ordering = None
if order is not None: if order is not None:
query_result_ordering = utils.parse_query_order(order) query_result_ordering = utils.parse_query_order(order)
results_field_list = None if len(field_list) == 0 else field_list
limit = limit if limit is not None else 0
if group is None: if group is None:
cursor = collection.find( cursor = collection.find(
filter=query_filters, projection=results_field_list, spec = query_filters,
skip=offset, limit=limit, sort=query_result_ordering) fields=field_list,
skip=offset,
limit=limit if limit != None else 0,
sort=query_result_ordering)
else: else:
pipeline = list() pipeline = list()
unwinding_list = list() unwinding_list = list()
@ -252,7 +253,8 @@ class MongoDbDatasource(object):
fname, op, val)) fname, op, val))
del(new_filters[i]) del(new_filters[i])
new_filters.append( new_filters.append(
(CLASS_ID_FIELDNAME, '=', target_child.__name__)) (CLASS_ID_FIELDNAME, '=',
collection_name(target_child.__name__)))
result += act( result += act(
target = target_child, target = target_child,
filters = new_filters, filters = new_filters,
@ -436,17 +438,33 @@ class MongoDbDatasource(object):
@classmethod @classmethod
def __filters2mongo(cls, filters): def __filters2mongo(cls, filters):
res = dict() res = dict()
eq_fieldname = [] #Stores field with equal comparison OP
for fieldname, op, value in filters: for fieldname, op, value in filters:
oop = op oop = op
ovalue = value ovalue = value
op, value = cls.__op_value_conv(op, value) op, value = cls.__op_value_conv(op, value)
if op == '=':
eq_fieldname.append(fieldname)
if fieldname in res:
logger.warning("Dropping previous condition. Overwritten \
by an equality filter")
res[fieldname] = str(value)
continue
if fieldname in eq_fieldname:
logger.warning("Dropping condition : '%s %s %s'" % (
fieldname, op, value))
continue
if fieldname not in res: if fieldname not in res:
res[fieldname] = dict() res[fieldname] = dict()
if op in res[fieldname]: if op in res[fieldname]:
logger.warning("Dropping condition : '%s %s %s'" % ( logger.warning("Dropping condition : '%s %s %s'" % (
fieldname, op, value)) fieldname, op, value))
else: else:
res[fieldname][op] = value if op not in cls.lodel2mongo_op_map:
raise ValueError("Invalid operator : '%s'" % op)
new_op = cls.lodel2mongo_op_map[op]
res[fieldname][new_op] = value
return res return res

View file

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

View file

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