fix things related to conditions

This commit is contained in:
Romain Bignon 2010-11-01 10:37:07 +01:00
commit 572d5c1061
2 changed files with 15 additions and 10 deletions

View file

@ -107,7 +107,8 @@ class IFormatter(object):
def format_dict(self, item):
"""
Format an dict to be human-readable. The dict is already simplified if user provides selected fields.
Format a dict to be human-readable. The dict is already simplified
if user provides selected fields.
Called by format().
This method has to be overridden in child classes.
@ -121,7 +122,7 @@ class IFormatter(object):
print string.encode('utf-8')
def to_dict(self, obj, condition=None, selected_fields=None):
def iter_select_and_decorate(d):
def iter_select(d):
if selected_fields is None or '*' in selected_fields:
fields = d.iterkeys()
else:
@ -133,12 +134,16 @@ class IFormatter(object):
except KeyError:
raise FieldNotFound(obj, key)
yield key, value
def iter_decorate(d):
for key, value in d:
if key == 'id' and obj.backend is not None:
value = self.build_id(value, obj.backend)
yield key, value
fields_iterator = obj.iter_fields()
d = OrderedDict(fields_iterator)
d = OrderedDict(iter_decorate(fields_iterator))
if condition is not None and not condition.is_valid(d):
return None
return OrderedDict([(k, v) for k, v in iter_select_and_decorate(d)])
return OrderedDict([(k, v) for k, v in iter_select(d)])

View file

@ -71,16 +71,16 @@ class ResultsCondition(object):
condition_str = None
def __init__(self, condition_str):
condition_str = condition_str.replace('OR', 'or') \
.replace('AND', 'and') \
.replace('NOT', 'not')
condition_str = condition_str.replace(' OR ', ' or ') \
.replace(' AND ', ' and ') \
.replace(' NOT ', ' not ')
or_list = []
for _or in condition_str.split('or'):
for _or in condition_str.split(' or '):
and_dict = {}
for _and in _or.split('and'):
for _and in _or.split(' and '):
if '!=' in _and:
k, v = _and.split('!=')
k += '!'
k = k.strip() + '!'
elif '=' in _and:
k, v = _and.split('=')
else: