Views, decorators, and templates

There are several views and decorators your site will use to drive Fitbit integration.

fitapp.decorators.fitbit_integration_warning(msg=None)

Adds a message to inform the user about Fitbit integration if their account is not already integrated with Fitbit.

Parameters:msg – The message text to use if the user is not integrated. If msg is a callable, it is called with the request as the only parameter. Otherwise, msg is passed in as the message text. If no message is provided, the value in FITAPP_DECORATOR_MESSAGE is used.

This decorator does not prevent the user from seeing the view if they are not integrated with Fitbit - it only adds a message to the user’s messages. If you would like to change the behavior of your view based on whether the user is integrated, you can check the user’s status by using fitapp.utils.is_integrated().

Example:

from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from fitapp.decorators import fitbit_integration_warning

@fitbit_integration_warning(msg="Integrate your account with Fitbit!")
@login_required
def my_view(request):
    return HttpResponse('Visible to authenticated users regardless' +
            'of Fitbit integration status')

In this example, the fitbit_integration_warning decorator only operates if the user is logged in. The view content is visible to all users who are logged in, regardless of Fitbit integration status.

The template(s) for any view with this decorator should display a user’s messages.

fitapp.views.login(request, *args, **kwargs)

Begins the OAuth authentication process by obtaining a Request Token from Fitbit and redirecting the user to the Fitbit site for authorization.

When the user has finished at the Fitbit site, they will be redirected to the fitapp.views.complete() view.

If ‘next’ is provided in the GET data, it is saved in the session so the fitapp.views.complete() view can redirect the user to that URL upon successful authentication.

URL name:
fitbit-login
fitapp.views.complete(request, *args, **kwargs)

After the user authorizes us, Fitbit sends a callback to this URL to complete authentication.

If there was an error, the user is redirected again to the error view.

If the authorization was successful, the credentials are stored for us to use later, and the user is redirected. If ‘next_url’ is in the request session, the user is redirected to that URL. Otherwise, they are redirected to the URL specified by the setting FITAPP_LOGIN_REDIRECT.

If FITAPP_SUBSCRIBE is set to True, add a subscription to user data at this time.

URL name:
fitbit-complete
fitapp.views.error(request, *args, **kwargs)

The user is redirected to this view if we encounter an error acquiring their Fitbit credentials. It renders the template defined in the setting FITAPP_ERROR_TEMPLATE. The default template, located at fitapp/error.html, simply informs the user of the error:

<html>
    <head>
        <title>Fitbit Authentication Error</title>
    </head>
    <body>
        <h1>Fitbit Authentication Error</h1>

        <p>We encontered an error while attempting to authenticate you
        through Fitbit.</p>
    </body>
</html>
URL name:
fitbit-error
fitapp.views.logout(request, *args, **kwargs)

Forget this user’s Fitbit credentials.

If the request has a next parameter, the user is redirected to that URL. Otherwise, they’re redirected to the URL defined in the setting FITAPP_LOGOUT_REDIRECT.

URL name:
fitbit-logout
fitapp.views.get_steps(request, *args, **kwargs)

An AJAX view that retrieves this user’s step data from Fitbit.

This view has been deprecated. Use get_data instead.

URL name:
fitbit-steps