Skip to content

Commit

Permalink
Merge pull request #149 from diegojromerolopez/master
Browse files Browse the repository at this point in the history
Pagination when fetching actions and computing stats using TIMEZONE
  • Loading branch information
sarumont authored Aug 11, 2016
2 parents 218c5bc + 4494bce commit 6dd6716
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ dist
.pydevproject
.idea/
.tox/
.DS_Store
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
requests
requests-oauthlib
python-dateutil
pytz
18 changes: 13 additions & 5 deletions trello/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ def fetch(self):
self.description = json_obj.get('desc', '')
self.closed = json_obj['closed']
self.url = json_obj['url']
try:
self.date_last_activity = dateparser.parse(json_obj['dateLastActivity'])
except:
self.date_last_activity = None

def save(self):
pass
Expand Down Expand Up @@ -305,9 +309,13 @@ def get_members(self, filters=None):

return members

def fetch_actions(self, action_filter, action_limit=50):
json_obj = self.client.fetch_json(
'/boards/' + self.id + '/actions',
query_params={'filter': action_filter,
'limit': action_limit})
def fetch_actions(self, action_filter, action_limit=50, since=None):
query_params = {'filter': action_filter, 'limit': action_limit}

if since:
query_params["since"] = since

json_obj = self.client.fetch_json('/boards/' + self.id + '/actions', query_params=query_params)

self.actions = json_obj
return self.actions
21 changes: 15 additions & 6 deletions trello/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
from __future__ import with_statement, print_function, absolute_import
from dateutil import parser as dateparser
from datetime import datetime

from trello.organization import Organization
from trello.compat import force_str
from trello.checklist import Checklist
from trello.label import Label

import datetime
import pytz


class Card(object):
Expand Down Expand Up @@ -129,6 +132,9 @@ def from_json(cls, parent, json_obj):
card.due = json_obj.get('due', '')
card.closed = json_obj['closed']
card.url = json_obj['url']
card.pos = json_obj['pos']
card.shortUrl = json_obj['shortUrl']
card.idMembers = json_obj['idMembers']
card.member_ids = json_obj['idMembers']
card.idLabels = json_obj['idLabels']
card.idList = json_obj['idList']
Expand Down Expand Up @@ -260,7 +266,8 @@ def _list_movements(self, movement_function, filter_by_date_interval=None):

action_since = None if not filter_by_date_interval else filter_by_date_interval[0]
action_before = None if not filter_by_date_interval else filter_by_date_interval[1]
self.fetch_actions('updateCard:idList,', action_since, action_before)
if not hasattr(self, "actions") or self.actions is None:
self.fetch_actions('updateCard:idList,', action_since, action_before)

movements = []

Expand Down Expand Up @@ -309,7 +316,7 @@ def movement_as_dict_function(_source_list, _destination_list, _movement_datetim
return self._list_movements(movement_function=movement_as_dict_function, filter_by_date_interval=filter_by_date_interval)


def get_stats_by_list(self, tz, lists, list_cmp=None, done_list=None, time_unit="seconds", card_movements_filter=None):
def get_stats_by_list(self, lists, list_cmp=None, done_list=None, time_unit="seconds", card_movements_filter=None):
"""
Gets several stats about the card by each list of the board:
- time: The time that the card has been in each column in seconds (minutes or hours).
Expand All @@ -319,7 +326,6 @@ def get_stats_by_list(self, tz, lists, list_cmp=None, done_list=None, time_unit=
Returns a dict where the key is list id and value is a dict with keys
time, forward_moves and backward_moves.
:param tz: timezone to make comparison timezone-aware
:param lists: list of board lists.
:param list_cmp: function that compares two lists a,b given id_a, id_b. If b is in a forward position returns 1 else -1.
:param time_unit: default to seconds. Allow specifying time in "minutes" or "hours".
Expand All @@ -328,6 +334,8 @@ def get_stats_by_list(self, tz, lists, list_cmp=None, done_list=None, time_unit=
:return: dict of the form {list_id: {time:<time card was in that list>, forward_moves: <number>, backward_moves: <number> }}
"""

tz = pytz.timezone(Organization.TIMEZONE)

# Conversion of units
seconds_to_time_unit = lambda time: time
if time_unit == "minutes":
Expand Down Expand Up @@ -409,9 +417,10 @@ def created_date(self):
it fails. attriExp('convertToCardFromCheckItem') allows to
test for the condition.
"""
self.fetch_actions()
date_str = self.actions[0]['date']
return dateparser.parse(date_str)
if not hasattr(self, "creation_date"):
localtz = pytz.timezone(Organization.TIMEZONE)
self.creation_date = localtz.localize(datetime.datetime.fromtimestamp(int(self.id[0: 8], 16)))
return self.creation_date

# backwards compatibility alias; TODO: deprecation message
create_date = created_date
Expand Down
3 changes: 2 additions & 1 deletion trello/organization.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement, print_function, absolute_import
from trello.board import Board
from trello.compat import force_str
from trello.member import Member


class Organization(object):

TIMEZONE = None

"""
Class representing an organization
"""
Expand Down

0 comments on commit 6dd6716

Please sign in to comment.