Skip to content

Commit

Permalink
Merge pull request #130 from nureineide/master
Browse files Browse the repository at this point in the history
Implement more checklist operations
  • Loading branch information
sarumont committed Mar 2, 2016
2 parents 9518127 + 1f75070 commit 1325e31
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
23 changes: 23 additions & 0 deletions test/test_checklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ def test_checklist_rename(self):
self.assertEqual(len(card.checklists), 1)
self.assertEqual(card.checklists[0].name, new_name)

def test_delete_checklist_item(self):
name = "Testing checklist item delete"
card = self._list.add_card(name, "Description goes here")

name = 'Checklist'
checklist = self._add_checklist(card, name, ['item1', 'item2'])
checklist.delete_checklist_item('item2')

checklists = card.fetch_checklists()
self.assertEqual(len(checklists[0].items), 1)
self.assertEqual(checklists[0].items[0]['name'], 'item1')

def test_clear_checklist(self):
name = "Testing checklist clear"
card = self._list.add_card(name, "Description goes here")

name = 'Checklist'
checklist = self._add_checklist(card, name, ['item1', 'item2', 'item3'])
checklist.clear()

checklists = card.fetch_checklists()
self.assertEqual(len(checklists[0].items), 0)


def suite():
# tests = ['test01_list_boards', 'test10_board_attrs', 'test20_add_card']
Expand Down
46 changes: 35 additions & 11 deletions trello/checklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,36 @@ def add_checklist_item(self, name, checked=False):
self.items.append(json_obj)
return json_obj

def delete_checklist_item(self, name):
"""Delete an item on this checklist
:name: name of the checklist item to delete
"""
ix = self._get_item_id(name)
if ix is None:
return

self.client.fetch_json(
'/checklists/'+ self.id +
'/checkItems/'+ self.items[ix]['id'],
http_method='DELETE')
del self.items[ix]

def clear(self):
"""Clear checklist by removing all checklist items"""
# iterate over names as list is modified while iterating and this breaks
# for-loops behaviour
for name in [item['name'] for item in self.items]:
self.delete_checklist_item(name)

def set_checklist_item(self, name, checked):
"""Set the state of an item on this checklist
:name: name of the checklist item
:checked: True if item state should be checked, False otherwise
"""

# Locate the id of the checklist item
try:
[ix] = [i for i in range(len(self.items)) if
self.items[i]['name'] == name]
except ValueError:
ix = self._get_item_id(name)
if ix is None:
return

json_obj = self.client.fetch_json(
Expand Down Expand Up @@ -80,11 +98,8 @@ def rename_checklist_item(self, name, new_name):
:name: name of the checklist item
:new_name: new name of item
"""

# Locate the id of the checklist item
try:
[ix] = [i for i in range(len(self.items)) if self.items[i]['name'] == name]
except ValueError:
ix = self._get_item_id(name)
if ix is None:
return

json_obj = self.client.fetch_json(
Expand All @@ -103,5 +118,14 @@ def delete(self):
'/checklists/%s' % self.id,
http_method='DELETE')

def _get_item_id(self, name):
"""Locate the id of the checklist item"""
try:
[ix] = [i for i in range(len(self.items)) if
self.items[i]['name'] == name]
return ix
except ValueError:
return None

def __repr__(self):
return '<Checklist %s>' % self.id

0 comments on commit 1325e31

Please sign in to comment.