Last active
August 29, 2015 14:10
-
-
Save lightstrike/75a3ea3721efb0ea186e to your computer and use it in GitHub Desktop.
Determine if a user is tracking time in Harvest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # http://docs.python-requests.org/en/latest/ | |
| import requests | |
| from datetime import datetime, timedelta | |
| domain_name = 'domain' | |
| username = 'username' | |
| password = 'password' | |
| # lookup past 3 days along with today in case someone left a timer running | |
| today = datetime.now() | |
| past_date = today - timedelta(days=3) | |
| # format time to expected format by Harvest API URL | |
| from_date = past_date.strftime('Y%Y%m%d') | |
| to_date = today.strftime('Y%Y%m%d') | |
| def get_harvest_json(url): | |
| """ | |
| Returns JSON from a Harvest API URL | |
| """ | |
| return requests.get(url, | |
| auth=(username, password), | |
| headers={'content-type': 'application/json', 'accept': 'application/json'} | |
| ).json() | |
| # Get a list of all users, then determine if each is tracking time | |
| people_url = 'https://{domain}.harvestapp.com/people'.format( | |
| domain=domain_name) | |
| people_json = get_harvest_json(people_url) | |
| # filter people json to only include active users | |
| people_json = [person for person in people_json if person['user']['is_active']] | |
| for person in people_json: | |
| person_name = '{first} {last}'.format( | |
| first=person['user']['first_name'], | |
| last=person['user']['last_name'] | |
| ) | |
| entry_url = 'https://{domain}.harvestapp.com/people/{user_id}/entries?from={from_date}&to={to_date}'.format( | |
| domain=domain_name, | |
| user_id=person['user']['id'], | |
| from_date=from_date, | |
| to_date=to_date | |
| ) | |
| entry_json = get_harvest_json(entry_url) | |
| # The only indication the Harvest API provides that a timer is currently running on an entry | |
| # is the presence of an 'hours_with_timer' item - determine if any entries have this item | |
| tracking_time = any(['hours_with_timer' in e['day_entry'] for e in entry_json]) | |
| if tracking_time: | |
| print '{name} IS tracking time'.format(name=person_name) | |
| else: | |
| print '{name} is NOT tracking time'.format(name=person_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment