Skip to content

Commit

Permalink
Eliminate unnecessary fetch calls for search results
Browse files Browse the repository at this point in the history
This patch eliminates the need to perform additional fetch calls for Board
and Organization results by altering the fields returned for results.
For Member results this patch drops fields that are unused in the
Member.from_json call like avatarHash and confirmed.
  • Loading branch information
Robert C Jennings committed Jan 16, 2017
1 parent 3d12518 commit e023fde
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions trello/trelloclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ def search(self, query, partial_match=False, models=[],
if card_ids:
query_params['idCards'] = card_ids

# Request result fields required to instantiate class objects
query_params['board_fields'] = ['name,url,desc,closed']
query_params['member_fields'] = ['fullName,initials,username']
query_params['organization_fields'] = ['name,url,desc']

json_obj = self.fetch_json('/search', query_params=query_params)
if not json_obj:
return []
Expand All @@ -311,15 +316,18 @@ def search(self, query, partial_match=False, models=[],
for board_json in json_obj.get('boards', []):
# Cache board objects
if board_json['id'] not in board_cache:
board_cache[board_json['id']] = Board(self, board_json['id'])
board_cache[board_json['id']].fetch()
board_cache[board_json['id']] = Board.from_json(
self, json_obj=board_json)
results.append(board_cache[board_json['id']])

for card_json in json_obj.get('cards', []):
# Cache board objects
if card_json['idBoard'] not in board_cache:
board_cache[card_json['idBoard']] = Board(
self, card_json['idBoard'])
# Fetch the board attributes as the Board object created
# from the card initially result lacks a URL and name.
# This Board will be stored in Card.parent
board_cache[card_json['idBoard']].fetch()
results.append(Card.from_json(board_cache[card_json['idBoard']],
card_json))
Expand All @@ -328,8 +336,7 @@ def search(self, query, partial_match=False, models=[],
results.append(Member.from_json(self, member_json))

for org_json in json_obj.get('organizations', []):
org = Organization(self, org_json['id'])
org.fetch()
org = Organization.from_json(self, org_json)
results.append(org)

return results

0 comments on commit e023fde

Please sign in to comment.