Skip to content

Commit

Permalink
adapt card checklists handling to new Trello payload
Browse files Browse the repository at this point in the history
There is no checkItemStats section anymore, but the state of each item is directly in its section.

fixes #357
  • Loading branch information
Amine Chadly authored and sarumont committed Jan 26, 2023
1 parent 9817e97 commit bef7c03
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 17 deletions.
3 changes: 1 addition & 2 deletions trello/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,7 @@ def get_checklists(self, cards='all'):
query_params={'cards': cards})
json_obj = sorted(json_obj, key=lambda checklist: checklist['pos'])
for cl in json_obj:
checklists.append(Checklist(self.client, cl.get('checkItemStates', []), cl,
trello_card=cl.get('idCard')))
checklists.append(Checklist(self.client, cl, trello_card=cl.get('idCard')))
return checklists

def add_list(self, name, pos=None):
Expand Down
8 changes: 2 additions & 6 deletions trello/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ def fetch(self, eager=True):
self.due = json_obj.get('due', '')
else:
self.due = ''
self.checked = json_obj['checkItemStates']
self.dateLastActivity = dateparser.parse(json_obj['dateLastActivity'])

self._customFields = self.fetch_custom_fields(json_obj=json_obj)
Expand Down Expand Up @@ -246,17 +245,14 @@ def fetch_checklists(self):
if self.countCheckLists == 0:
return []

if not hasattr(self, "checked") or self.checked is None:
self.fetch(eager=False)

checklists = []
json_obj = self.client.fetch_json(
'/cards/' + self.id + '/checklists', )
# Thanks /~https://github.com/HuffAndPuff for noticing checklist
# were not sorted
json_obj = sorted(json_obj, key=lambda checklist: checklist['pos'])
for cl in json_obj:
checklists.append(Checklist(self.client, self.checked, cl,
checklists.append(Checklist(self.client, cl,
trello_card=self.id))
return checklists

Expand Down Expand Up @@ -760,7 +756,7 @@ def add_checklist(self, title, items, itemstates=None):
http_method='POST',
post_args={'name': title}, )

cl = Checklist(self.client, [], json_obj, trello_card=self.id)
cl = Checklist(self.client, json_obj, trello_card=self.id)
for i, name in enumerate(items):
try:
checked = itemstates[i]
Expand Down
11 changes: 2 additions & 9 deletions trello/checklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ class Checklist(TrelloBase):
Class representing a Trello checklist.
"""

def __init__(self, client, checked, obj, trello_card=None):
def __init__(self, client, obj, trello_card=None):
"""
:client: Trello client object
:checked: List of completed checklist items on the card
:obj: List of checklists on the card, sorted by position
:trello_card: ID of the Trello card
"""
Expand All @@ -24,13 +23,7 @@ def __init__(self, client, checked, obj, trello_card=None):
self.name = obj['name']
self.items = sorted(obj['checkItems'], key=lambda items: items.get('pos'))
for i in self.items:
i['checked'] = False
# checked might be None if there were no completed items
if not checked:
continue
for cis in checked:
if cis['idCheckItem'] == i['id'] and cis['state'] == 'complete':
i['checked'] = True
i['checked'] = i['state'] == "complete"

def add_checklist_item(self, name, checked=False):
"""Add a checklist item to this checklist
Expand Down

0 comments on commit bef7c03

Please sign in to comment.