Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perfomance fix #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 23 additions & 26 deletions sugarcrm/sugarmodule.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import itertools

from sugarentry import SugarEntry
Expand Down Expand Up @@ -35,45 +34,43 @@ 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:
query_str -- SQL query to be passed to the API
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

Expand Down