Example #1
0
def clear_auth(request):
    """
    Clears the credentials for the requesting user
    """
    if request.user:
        profile, _ = Profile.get_or_create(user=request.user)
        profile.clear_credentials()
    return HttpResponseRedirect("/")
Example #2
0
def clear_auth(request):
    """
    Clears the credentials for the requesting user
    """
    if request.user:
        profile, _ = Profile.get_or_create(user=request.user)
        profile.clear_credentials()
    return HttpResponseRedirect("/")
Example #3
0
def google_auth(request):
    """
    Handles Google oauth flow. For details, visit
    https://developers.google.com/api-client-library/python/guide/aaa_oauth#OAuth2WebServerFlow
    https://developers.google.com/api-client-library/python/guide/django
    """

    flow = None
    default_flow = OAuth2WebServerFlow(
        client_id=settings.GOOGLE_CALENDAR_API_CLIENT_ID,
        client_secret=settings.GOOGLE_CALENDAR_API_CLIENT_SECRET,
        scope=['https://www.googleapis.com/auth/calendar', 'profile', 'email'],
        redirect_uri=settings.BASE_URL + '/auth/google')
    default_flow.params['access_type'] = 'offline'
    default_flow.params['include_granted_scopes'] = 'true'
    default_flow.params['prompt'] = 'consent'

    # Try to retrieve an existing flow, or create one if it doesn't exist
    gflow = GoogleFlow.objects.filter(id=request.user).last()

    if gflow and gflow.flow.params.get('prompt') != 'consent':
        # Delete any flows that don't have the prompt parameter set, since that will
        # prevent the credentials from getting a refresh token
        gflow.delete()
        gflow = None

    if not gflow:
        # print "Could not retrieve existing flow"
        gflow = GoogleFlow(id=request.user, flow=default_flow)
        gflow.save()
    flow = gflow.flow

    code = request.GET.get('code', None)
    error = request.GET.get('error', None)

    if error:
        # TODO eventually make this prettier, like redirect to some landing page
        return HttpResponseBadRequest(
            "Authentication failed. Reason: {}".format(error))
    elif code:
        credential = flow.step2_exchange(code)
        # Save the credentials
        storage = Storage(GoogleCredentials, 'user', request.user,
                          'credential')
        storage.put(credential)
        profile, _ = Profile.get_or_create(user=request.user)
        profile.authed = True
        profile.save()

        # TODO improve the latency over here
        request.user.googlecredentials.import_calendars()

        return HttpResponseRedirect("/")
    else:
        auth_uri = flow.step1_get_authorize_url()
        return HttpResponseRedirect(auth_uri)
Example #4
0
def google_auth(request):
    """
    Handles Google oauth flow. For details, visit
    https://developers.google.com/api-client-library/python/guide/aaa_oauth#OAuth2WebServerFlow
    https://developers.google.com/api-client-library/python/guide/django
    """

    flow = None
    default_flow = OAuth2WebServerFlow(client_id=settings.GOOGLE_CALENDAR_API_CLIENT_ID,
                                       client_secret=settings.GOOGLE_CALENDAR_API_CLIENT_SECRET,
                                       scope=['https://www.googleapis.com/auth/calendar','profile','email'],
                                       redirect_uri=settings.BASE_URL + '/auth/google')
    default_flow.params['access_type'] = 'offline'
    default_flow.params['include_granted_scopes'] = 'true'

    # Try to retrieve an existing flow, or create one if it doesn't exist
    gflow = GoogleFlow.objects.filter(id=request.user).last()
    if not gflow:
        gflow = GoogleFlow(id=request.user,
                          flow=default_flow)
        gflow.save()
    flow = gflow.flow

    code = request.GET.get('code', None)
    error = request.GET.get('error', None)

    if error:
        # TODO eventually make this prettier, like redirect to some landing page
        return HttpResponseBadRequest("Authentication failed. Reason: {}".format(error))
    elif code:
        credential = flow.step2_exchange(code)
        # Save the credentials
        storage = Storage(GoogleCredentials, 'user', request.user, 'credential')
        storage.put(credential)
        profile, _ = Profile.get_or_create(user=request.user)
        profile.authed = True
        profile.save()

        # TODO improve the latency over here
        request.user.googlecredentials.import_calendars()

        return HttpResponseRedirect("/")
    else:
        auth_uri = flow.step1_get_authorize_url()
        return HttpResponseRedirect(auth_uri)
Example #5
0
def sync(request, format=None):
    if not request.user:
        return Response("Not logged in")

    profile = Profile.get_or_create(request.user)[0]

    if request.query_params.get('sync_all'):
        profile.create_calendars(only_primary=False)
        calendars = GCalendar.objects.filter(user=request.user)
    else:
        profile.create_calendars(only_primary=True)
        calendars = [profile.main_calendar]

    for calendar in calendars:
        if request.query_params.get('full_sync'):
            calendar.sync(full_sync=True)
        else:
            calendar.sync()

    return HttpResponseRedirect("/sync?no_sync=true")
Example #6
0
def sync(request, format=None):

    if not request.user:
        return Response("Not logged in")

    profile = Profile.get_or_create(request.user)[0]

    if request.query_params.get('sync_all'):
        profile.create_calendars(only_primary=False)
        calendars = GCalendar.objects.filter(user=request.user)
    else:
        profile.create_calendars(only_primary=True)
        calendars = [profile.main_calendar]

    for calendar in calendars:
        if request.query_params.get('full_sync'):
            calendar.sync(full_sync=True)
        else:
            calendar.sync()

    return HttpResponseRedirect("/")