Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge commit 'upstream/master'

  • Loading branch information...
commit 21dcb5f99b2806347f1e91d1079f03e496159407 2 parents f8350ca + f0c3d6d
@bne authored
Showing with 65 additions and 11 deletions.
  1. +10 −10 harvest-notifier.py
  2. +55 −1 harvest.py
View
20 harvest-notifier.py
@@ -62,16 +62,16 @@ def __init__(self):
def task_cb(self, widget, event, data = None):
if self.timer_running:
- # TODO: Needs to stop the current timer, and start the new one
-
- pass
- else:
+
+ self.menu.remove(self.menu.children()[0])
- # button showing what timer we're running
- timer = gtk.MenuItem(data['project']['name'] + " - " + data['task']['name'])
- timer.connect('activate', self.timer_click_cb, self.menu, data)
- self.menu.prepend(timer)
- self.timer_running = True
+ self.harvest.timer_toggle(data['project'], data['task'])
+
+ # button showing what timer we're running
+ timer = gtk.MenuItem(data['project']['name'] + " - " + data['task']['name'])
+ timer.connect('activate', self.timer_click_cb, self.menu, data)
+ self.menu.prepend(timer)
+ self.timer_running = True
def timer_click_cb(self, widget, event, data = None):
""" Event triggered if a timer is running and someone clicks on the timer """
@@ -80,7 +80,7 @@ def timer_click_cb(self, widget, event, data = None):
# we shouldn't get here, but just in case
return
- # TODO: actually stop the timer
+ self.harvest.timer_toggle(data['project'], data['task'])
# remove the timer button
self.menu.remove(widget)
View
56 harvest.py
@@ -1,6 +1,8 @@
import urllib2
+import urllib
import json
from base64 import b64encode
+from datetime import datetime
class Harvest(object):
@@ -19,6 +21,13 @@ def __init__(self, uri, username, password):
'User-Agent':'harvest.py',
}
+ self.xml_headers = {
+ 'Authorization':'Basic '+b64encode('%s:%s' % (self.username, self.password)),
+ 'Accept':'application/xml',
+ 'Content-Type':'application/xml',
+ 'User-Agent':'harvest.py',
+ }
+
def _request(self, url):
""" Make a request to the harvest web service """
@@ -28,6 +37,19 @@ def _request(self, url):
j = r.read()
return json.loads(j)
+ def _post(self, url, data):
+ """ Send data to the harvest web service """
+ request = urllib2.Request(url = self.uri + url, data = data, headers = self.headers)
+
+ r = urllib2.urlopen(request)
+ j = r.read()
+ return json.loads(j)
+
+ def get_day_entries(self):
+ """ Get all the information from the daily api """
+ data = self._request('/daily')
+ return data['day_entries']
+
def get_project_list(self):
""" Get a list of projects from harvest """
data = self._request('/daily')
@@ -45,4 +67,36 @@ def get_project_in_categories(self):
else:
ret_data[project['client']] = [project]
- return ret_data
+ return ret_data
+
+ def timer_toggle(self, project, task):
+
+ # first check if we have a daily entry for this project and task
+ data = self.get_day_entries()
+
+ entry_id = None
+
+ for entry in data:
+ if entry['project'] == project['name'] and entry['task'] == task['name']:
+ entry_id = entry['id']
+
+ if entry_id:
+ url = "/daily/timer/" + str(entry_id)
+
+ return self._request(url)
+ else:
+
+ # if we get here, we don't have a day entry for this project yet
+ # create one, then use that
+
+ data = {}
+ data['notes'] = "Timer started by harvest-notifier"
+ data['hours'] = " "
+ data['project_id'] = project['id']
+ data['task_id'] = task['id']
+ data['spent_at'] = datetime.now().strftime("%a, %d %b %Y")
+
+ data = json.dumps(data)
+
+ return self._post('/daily/add', data)
+
Please sign in to comment.
Something went wrong with that request. Please try again.