1414from __future__ import unicode_literals
1515
1616import requests
17-
1817from datetime import date , datetime
19- from django .utils .dateparse import parse_datetime , parse_date
2018
2119
2220class Tempo (object ):
@@ -38,12 +36,8 @@ def _resolve_date(self, value):
3836 return value .date ()
3937 if isinstance (value , date ):
4038 return value
41- parsed = parse_date (value )
42- if not parsed :
43- parsed = parse_datetime (value )
44- if not parsed :
45- raise ValueError ()
46- return parsed .date ()
39+
40+ parsed = date .fromisoformat (value )
4741 return parsed
4842
4943 def _list (self , url , ** params ):
@@ -69,60 +63,68 @@ def _list(self, url, **params):
6963
7064 def get_accounts (self ):
7165 """
72- Retrieve existing accounts.
66+ Retrieve existing accounts as a dictionary .
7367 """
7468 return { x ["key" ]: x for x in self ._list ("/accounts" ) }
7569
7670 def get_account_categories (self ):
7771 """
78- Retrieve existing account categories.
72+ Retrieve existing account categories as a dictionary .
7973 """
8074 return { x ["key" ]: x for x in self ._list ("/account-categories" ) }
8175
8276 def get_account_category_types (self ):
8377 """
84- Retrieve all periods for a given date range
78+ Retrieve all periods for a given date range as a list.
8579 """
86- return list ( self ._list ("/account-category-types" ) )
80+ return self ._list ("/account-category-types" )
8781
8882 def get_periods (self , date_from , date_to ):
8983 """
90- Retrieve periods.
84+ Retrieve periods as a list .
9185 """
9286 params = {
9387 "from" : self ._resolve_date (date_from ).isoformat (),
9488 "to" : self ._resolve_date (date_to ).isoformat ()
9589 }
9690
97- return list ( self ._list ("/periods" , ** params ) )
91+ return self ._list ("/periods" , ** params )
9892
9993 def get_work_attributes (self ):
10094 """
101- Returns work attributeslogs inside ```date_from``` and ```date_to```,
95+ Returns worklog attributes inside ```date_from``` and ```date_to```,
10296 for particular ```user```, adding work attributes if ```add_work_attributes```.
10397 """
10498 return { x ["key" ]: x for x in self ._list ("/work-attributes" ) }
10599
106- def _process_worklogs (self , worklogs ):
100+ def add_worklog_attributes (self , worklogs ):
101+ """
102+ Return worklogs with added worklog attributes, destroys worklogs.
103+ """
104+
105+ if not self .work_attributes :
106+ self .work_attributes = self .get_work_attributes ()
107+
107108 for worklog in worklogs :
108109 attributes = (worklog .get ("attributes" ) or {}).get ("values" ) or {}
109110 resolved_attributes = {}
110111
111112 for attribute in attributes :
112- key = attribute ["key" ]
113- name = self .work_attributes .get (key , {}).get ("name" , key )
114- resolved_attributes [name ] = attribute ["value" ]
113+ key = attribute .get ("key" )
114+ if key :
115+ name = self .work_attributes .get (key , {}).get ("name" , key )
116+ resolved_attributes [name ] = attribute ["value" ]
115117
116118 worklog ["attributes" ] = resolved_attributes
119+
117120 yield worklog
118121
119122 def get_all_worklogs (self , date_from , date_to ):
120123 date_from = self ._resolve_date (date_from ).isoformat ()
121124 date_to = self ._resolve_date (date_to ).isoformat ()
122125 url = f"/worklogs"
123126 params = { "from" : date_from , "to" : date_to , "limit" : self .MAX_RESULTS }
124- l = self ._list (url , ** params )
125- return self ._process_worklogs (l )
127+ return self ._list (url , ** params )
126128
127129 def get_user_worklogs (self , date_from , date_to , userid ):
128130 """
@@ -134,8 +136,7 @@ def get_user_worklogs(self, date_from, date_to, userid):
134136 date_to = self ._resolve_date (date_to ).isoformat ()
135137 url = f"/worklogs/user/{ userid } "
136138 params = { "from" : date_from , "to" : date_to , "limit" : self .MAX_RESULTS }
137- l = self ._list (url , ** params )
138- return self ._process_worklogs (l )
139+ return self ._list (url , ** params )
139140
140141 def get_team_worklogs (self , date_from , date_to , teamid ):
141142 """
@@ -147,8 +148,7 @@ def get_team_worklogs(self, date_from, date_to, teamid):
147148 date_to = self ._resolve_date (date_to ).isoformat ()
148149 url = f"/worklogs/team/{ teamid } "
149150 params = { "from" : date_from , "to" : date_to , "limit" : self .MAX_RESULTS }
150- l = self ._list (url , ** params )
151- return self ._process_worklogs (l )
151+ return self ._list (url , ** params )
152152
153153 def get_user_schedule (self , date_from , date_to , user = None ):
154154 """
@@ -161,4 +161,4 @@ def get_user_schedule(self, date_from, date_to, user=None):
161161 if user is not None :
162162 url += "/{}" .format (user )
163163 params = { "from" : date_from , "to" : date_to , "limit" : self .MAX_RESULTS }
164- return list ( self ._list (url , ** params ) )
164+ return self ._list (url , ** params )
0 commit comments