diff --git a/sugarcrm/sugarmodule.py b/sugarcrm/sugarmodule.py index 5d4ea95..15f70a8 100644 --- a/sugarcrm/sugarmodule.py +++ b/sugarcrm/sugarmodule.py @@ -1,4 +1,3 @@ - import itertools from sugarentry import SugarEntry @@ -35,7 +34,7 @@ def __init__(self, connection, name): self._relationships = (result['link_fields'] or {}).copy() - def _search(self, query_str, start = 0, total = 20, fields = []): + def _search(self, query_str, start = 0, total = 20, fields = [], _exclude_ids = []): """Return a list of SugarEntry objects that match the query. Keyword arguments: @@ -43,37 +42,35 @@ def _search(self, query_str, start = 0, total = 20, fields = []): start -- Record offset to start from total -- Maximum number of results to return fields -- If set, return only the specified fields + _exclude_ids - IDs of records that must be excluded from the result """ if 'id' not in fields: fields.append('id') entry_list = [] - count = 0 - offset = 0 - while count < total: - result = self._connection.get_entry_list(self._name, - query_str, '', start + offset, fields, - total - count, 0) - if result['result_count'] == 0: - break + doubles_count = 0 + result = self._connection.get_entry_list(self._name, query_str, '', start, fields, total, 0) + + for i in range(result['result_count']): + + new_entry = SugarEntry(self) + + nvl = result['entry_list'][i]['name_value_list'] + for attribute in nvl: + new_entry._fields[attribute] = nvl[attribute]['value'] + + # SugarCRM seems broken, because it retrieves several copies + # of the same contact for every opportunity related with + # it. Check to make sure we don't return duplicate entries. + if new_entry['id'] not in _exclude_ids: + entry_list.append(new_entry) + _exclude_ids.append(new_entry._fields['id']) else: - offset += result['result_count'] - for i in range(result['result_count']): - - new_entry = SugarEntry(self) - - nvl = result['entry_list'][i]['name_value_list'] - for attribute in nvl: - new_entry._fields[attribute] = nvl[attribute]['value'] - - # SugarCRM seems broken, because it retrieves several copies - # of the same contact for every opportunity related with - # it. Check to make sure we don't return duplicate entries. - if new_entry['id'] not in [entry['id'] - for entry in entry_list]: - entry_list.append(new_entry) - count += 1 + doubles_count += 1 + + if doubles_count > 0: + entry_list += self._search(query_str, start + result['result_count'], doubles_count, fields, _exclude_ids) return entry_list