Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ __pycache__
*$py.class
staticfiles/*
.vscode/*
.python-version
140 changes: 64 additions & 76 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion datauploader/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ def fetch_fitbit_data(fitbit_member_id, access_token):

def get_existing_fitbit(oh_access_token, fitbit_urls):
print("entered get_existing_fitbit")
member = api.exchange_oauth2_member(oh_access_token)
member = api.exchange_oauth2_member(
oh_access_token, all_files=True)
for dfile in member['data']:
if 'Fitbit' in dfile['metadata']['tags']:
print("got inside fitbit if")
Expand Down
6 changes: 4 additions & 2 deletions main/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import arrow
from datetime import timedelta


def get_fitbit_file(oh_member):
try:
oh_access_token = oh_member.get_access_token(
client_id=settings.OPENHUMANS_CLIENT_ID,
client_secret=settings.OPENHUMANS_CLIENT_SECRET)
user_object = api.exchange_oauth2_member(oh_access_token)
user_object = api.exchange_oauth2_member(
oh_access_token, all_files=True)
for dfile in user_object['data']:
if 'Fitbit' in dfile['metadata']['tags']:
return dfile['download_url']
Expand All @@ -21,4 +23,4 @@ def get_fitbit_file(oh_member):
def check_update(fitbit_member):
if fitbit_member.last_submitted < (arrow.now() - timedelta(hours=1)):
return True
return False
return False
22 changes: 21 additions & 1 deletion main/templates/main/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ <h1>Open&nbsp;Humans&nbsp;/ Fitbit&nbsp;API Connection</h1>
</div>
</div>

<div>
<p>
<a href="https://www.fitbit.com">Fitbit</a> is a family of fitness products that help you stay motivated and improve your health by tracking your activity, exercise, food, weight and sleep.
This website can be used to retrieve and store your account data from Fitbit in your Open Humans account.
</p>


<p>
Check out <a href="{{ oh_proj_page }}">this project's activity page</a>
to see how notebooks, projects, and members are using & sharing this data.
Expand All @@ -44,4 +46,22 @@ <h1>Open&nbsp;Humans&nbsp;/ Fitbit&nbsp;API Connection</h1>
and always available to you, and you choose when to share with others.
</p>

{% endblock main %}
</ol>
{% if client_id %}
<a
class="btn btn-primary btn-lg"
href="https://www.openhumans.org/direct-sharing/projects/oauth2/authorize/?client_id={{ client_id }}&response_type=code&redirect_uri={{app_base}}/complete/oh"
{% else %}
class="btn btn-default disabled"
{% endif %}
>
Start with Open Humans authorization
</a>
{% if not client_id %}
<p class="text-muted"><i>
This button is disabled because client_id hasn't been configured.
</i></p>
{% endif %}
</div>

{% endblock %}
10 changes: 4 additions & 6 deletions main/views.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import logging
import requests
import os
import base64
import json
import arrow

from django.contrib import messages
from django.contrib.auth import login, logout
from django.shortcuts import render, redirect
from django.conf import settings
from datauploader.tasks import fetch_fitbit_data
from urllib.parse import parse_qs
from open_humans.models import OpenHumansMember
from .models import FitbitMember
from .helpers import get_fitbit_file, check_update

from ohapi import api

# Set up logging.
logger = logging.getLogger(__name__)
Expand All @@ -32,6 +29,7 @@ def index(request):
return redirect('/dashboard')

context = {'client_id': settings.OPENHUMANS_CLIENT_ID,
'app_base': settings.OPENHUMANS_APP_BASE_URL,
'oh_proj_page': settings.OH_ACTIVITY_PAGE}

return render(request, 'main/index.html', context=context)
Expand Down Expand Up @@ -62,7 +60,7 @@ def dashboard(request):
fitbit_member = ''
download_file = ''
auth_url = 'https://www.fitbit.com/oauth2/authorize?response_type=code&client_id='+settings.FITBIT_CLIENT_ID+'&scope=activity%20nutrition%20heartrate%20location%20nutrition%20profile%20settings%20sleep%20social%20weight'

context = {
'oh_member': request.user.oh_member,
'fitbit_member': fitbit_member,
Expand Down Expand Up @@ -191,7 +189,7 @@ def complete(request):
context['auth_url'] = auth_url
return render(request, 'main/fitbit.html',
context=context)

return redirect("/dashboard")

logger.debug('Invalid code exchange. User returned to starting page.')
Expand Down