Beispiel #1
0
def update_data(request):
    if request.method == "POST" and request.user.is_authenticated:
        openhumansmember = request.user.openhumansmember
        googlefit_member = openhumansmember.googlefit_member
        fetch_googlefit_data.delay(openhumansmember.oh_id)
        googlefit_member.last_submitted_for_update = arrow.now().format()
        googlefit_member.save()
        messages.info(request,
                      ("An update of your GoogleFit data has been started! "
                       "It can take some minutes before the first data is "
                       "available. Reload this page in a while to find your "
                       "data"))
        return redirect('/dashboard')
Beispiel #2
0
def complete_googlefit(request):

    if 'googlefit_oauth2_state' not in request.session:
        messages.warning(
            'Authorization with google did not succeed. Please try again')
        return redirect('/dashboard')

    state = request.session['googlefit_oauth2_state']
    flow = google_auth_oauthlib.flow.Flow.from_client_config(
        settings.GOOGLEFIT_CLIENT_CONFIG,
        scopes=settings.GOOGLEFIT_SCOPES,
        state=state)
    flow.redirect_uri = request.build_absolute_uri(
        reverse('complete_googlefit'))

    authorization_response = settings.OPENHUMANS_APP_BASE_URL + request.get_full_path(
    )
    flow.fetch_token(authorization_response=authorization_response)

    credentials = flow.credentials

    if hasattr(request.user.openhumansmember, 'googlefit_member'):
        googlefit_member = request.user.openhumansmember.googlefit_member
    else:
        googlefit_member = GoogleFitMember()

    googlefit_member.access_token = credentials.token
    if credentials.refresh_token:
        # Google returns a null refresh token after the 1st time
        googlefit_member.refresh_token = credentials.refresh_token
    googlefit_member.expiry_date = credentials.expiry
    googlefit_member.scope = credentials.scopes
    googlefit_member.user_id = request.user.openhumansmember.oh_id
    googlefit_member.save()

    fetch_googlefit_data.delay(request.user.openhumansmember.oh_id,
                               send_email=True)

    if googlefit_member and googlefit_member.refresh_token:
        messages.info(
            request,
            "Your GoogleFit account has been connected, and your data has been queued to be fetched from GoogleFit. You will receive an e-mail when the process has completed."
        )
        return redirect('/dashboard')

    logger.debug('Invalid code exchange. User returned to starting page.')
    messages.warning(request, (
        "Something went wrong, please try connecting your "
        "GoogleFit account again. If you have an existing connection, please go to https://myaccount.google.com/permissions to remove it and try again."
    ))
    return redirect('/dashboard')
Beispiel #3
0
 def handle(self, *args, **options):
     requests.get(settings.OPENHUMANS_APP_BASE_URL
                  )  # nudge asleep heroku instance to wake up :)
     users = OpenHumansMember.objects.all()
     for user in users:
         fetch_googlefit_data.delay(user.oh_id)