Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/gameloop/py-trello
Browse files Browse the repository at this point in the history
Conflicts:
	trello/__init__.py
  • Loading branch information
sarumont committed Aug 7, 2014
2 parents 3d433d1 + ad0a888 commit aa23339
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 31 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name = "py-trello",
version = "0.2.2",
version = "0.2.3",

description = 'Python wrapper around the Trello API',
long_description = open('README.rst').read(),
Expand All @@ -24,7 +24,7 @@
'Programming Language :: Python 3',
'Programming Language :: Python 3.3',
],
install_requires = ["requests", "requests-oauthlib >= 0.4.1",],
install_requires = ["requests", "requests-oauthlib >= 0.4.1", "dateutil"],
packages = find_packages(),
include_package_data = True,
)
Expand Down
58 changes: 54 additions & 4 deletions test/test_trello.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from trello import TrelloClient
import unittest
import os
Expand Down Expand Up @@ -61,7 +62,7 @@ def test30_list_attrs(self):
self.assertIsNotNone(l.closed, msg="closed not provided")
break # only need to test one board's lists

def test40_list_cards(self):
def test50_list_cards(self):
boards = self._trello.list_boards()
for b in boards:
for l in b.all_lists():
Expand All @@ -75,8 +76,24 @@ def test40_list_cards(self):
break
pass

def test51_fetch_cards(self):
"""
Tests fetching all attributes for all cards
"""
boards = self._trello.list_boards()
for b in boards:
for l in b.all_lists():
for c in l.list_cards():
c.fetch()

self.assertIsInstance(c.date_last_activity, datetime, msg='date not provided')
self.assertTrue(len(c.board_id) > 0, msg='board id not provided')
break
break
pass


def test50_add_card(self):
def test40_add_card(self):
boards = self._trello.list_boards()
board_id = None
for b in boards:
Expand All @@ -101,7 +118,7 @@ def test50_add_card(self):
if not card:
self.fail("No card created")

def test51_add_card(self):
def test41_add_card(self):
boards = self._trello.list_boards()
board_id = None
for b in boards:
Expand All @@ -128,20 +145,53 @@ def test51_add_card(self):
if not card:
self.fail("No card created")

def test42_add_card_with_comments(self):
boards = self._trello.list_boards()
board_id = None
for b in boards:
if b.name != os.environ['TRELLO_TEST_BOARD_NAME']:
continue

for l in b.open_lists():
try:
name = "Card with comments"
comment = "Hello World!"
card = l.add_card(name)
card.comment(comment)
card.fetch(True)
except Exception as e:
print(str(e))
self.fail("Caught Exception adding card")

self.assertIsNotNone(card, msg="card is None")
self.assertIsNotNone(card.id, msg="id not provided")
self.assertEquals(card.name, name)
self.assertEquals(card.description, '')
self.assertIsNotNone(card.closed, msg="closed not provided")
self.assertIsNotNone(card.url, msg="url not provided")
self.assertEquals(len(card.comments), 1)
self.assertEquals(card.comments[0]['data']['text'], comment)
break
break
if not card:
self.fail("No card created")


def test52_get_cards(self):
boards = [board for board in self._trello.list_boards() if board.name == os.environ['TRELLO_TEST_BOARD_NAME']]
self.assertEquals(len(boards), 1, msg="Test board not found")

board = boards[0]
cards = board.get_cards()
self.assertEqual(len(cards), 2, msg="Unexpected number of cards in testboard")
self.assertEqual(len(cards), 3, msg="Unexpected number of cards in testboard")

for card in cards:
if card.name == 'Testing from Python':
self.assertEqual(card.description, 'Description goes here')
elif card.name == 'Testing from Python - no desc':
self.assertEqual(card.description, '')
elif card.name == 'Card with comments':
self.assertEqual(card.description, '')
else:
self.fail(msg='Unexpected card found')

Expand Down
71 changes: 46 additions & 25 deletions trello/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime
import json
from dateutil import parser as dateparser
import requests
from requests_oauthlib import OAuth1

Expand Down Expand Up @@ -310,20 +311,7 @@ def get_cards(self, filters=None):
query_params=filters
)

cards = list()
for card_json in json_obj:
card = Card(self, card_json['id'],
name=card_json['name'])

for card_key, card_val in card_json.items():
if card_key in ['id', 'name']:
continue

setattr(card, card_key, card_val)

cards.append(card)

return cards
return list([Card.from_json(json, self) for json in json_obj])

def all_members(self):
"""Returns all members on this board"""
Expand Down Expand Up @@ -487,10 +475,32 @@ def board_id(self):
def description(self):
return self.desc

@property
def date_last_activity(self) -> datetime:
return self.dateLastActivity

@description.setter
def description(self, value):
self.desc = value

@property
def comments(self):
"""
Lazily loads and returns the comments
"""
if self._comments is None:
self._comments = self.fetch_comments()
return self._comments

@property
def checklists(self):
"""
Lazily loads and returns the checklists
"""
if self._checklists is None:
self._checklists = self.fetch_checklists()
return self._checklists

def __init__(self, trello_list, card_id, name=''):
"""
:trello_list: reference to the parent list
Expand Down Expand Up @@ -523,11 +533,15 @@ def from_json(cls, trello_list, json_obj):
def __repr__(self):
return '<Card %s>' % self.name

def fetch(self):
"""Fetch all attributes for this card"""
def fetch(self, eager=True):
"""
Fetch all attributes for this card
:param eager: If eager is true comments and checklists will be fetched immediately, otherwise on demand
"""
json_obj = self.client.fetch_json(
'/cards/' + self.id,
query_params={'badges': False})
self.id = json_obj['id']
self.name = json_obj['name'].encode('utf-8')
self.desc = json_obj.get('desc', '')
self.closed = json_obj['closed']
Expand All @@ -541,20 +555,27 @@ def fetch(self):
# For consistency, due date is in YYYY-MM-DD format
self.due = json_obj.get('due', '')[:10]
self.checked = json_obj['checkItemStates']
self.dateLastActivity = dateparser.parse(json_obj['dateLastActivity'])

self.checklists = []
if self.badges['checkItems'] > 0:
json_obj = self.client.fetch_json(
'/cards/' + self.id + '/checklists', )
for cl in json_obj:
self.checklists.append(Checklist(self.client, self.checked, cl,
trello_card=self.id))
self._checklists = self.fetch_checklists() if eager else None
self._comments = self.fetch_comments() if eager else None

self.comments = []
def fetch_comments(self):
comments = []
if self.badges['comments'] > 0:
self.comments = self.client.fetch_json(
comments = self.client.fetch_json(
'/cards/' + self.id + '/actions',
query_params={'filter': 'commentCard'})
return comments

def fetch_checklists(self):
checklists = []
if self.badges['checkItems'] > 0:
json_obj = self.client.fetch_json(
'/cards/' + self.id + '/checklists', )
for cl in json_obj:
checklists.append(Checklist(self.client, self.checked, cl, trello_card=self.id))
return checklists

def fetch_actions(self, action_filter='createCard'):
"""
Expand Down

0 comments on commit aa23339

Please sign in to comment.