示例#1
0
def edit(request, contact_id):        
    try:
        # Get the user's connection info
        connection_info = Office365Connection.objects.get(username = request.user)
        
    except ObjectDoesNotExist:
        # If there is no connection object for the user, they haven't connected their
        # Office 365 account yet. The page will ask them to connect.
        return render(request, 'contacts/index.html', None)
        
    else:
        if (connection_info.access_token is None or 
                connection_info.access_token == ''):
            return render(request, 'contacts/index.html', None)
            
    contact_json = contacts.o365service.get_contact_by_id(connection_info.outlook_api_endpoint,
                                                          connection_info.access_token,
                                                          contact_id, contact_properties)
                                                          
    if (not contact_json is None):
        # Load the contact into a DisplayContact object
        display_contact = DisplayContact()
        display_contact.load_json(contact_json)
        
        # Render a details form
        return render(request, 'contacts/details.html', { 'contact': display_contact })
    else:
        return render(request, 'contacts/error.html',
            {
                'error_message': 'Unable to get contact with ID: {0}'.format(contact_id),
            }
        )
示例#2
0
def edit(request, contact_id):
    try:
        # Get the user's connection info
        connection_info = Office365Connection.objects.get(
            username=request.user)

    except ObjectDoesNotExist:
        # If there is no connection object for the user, they haven't connected their
        # Office 365 account yet. The page will ask them to connect.
        return render(request, 'contacts/index.html', None)

    else:
        if (connection_info.access_token is None
                or connection_info.access_token == ''):
            return render(request, 'contacts/index.html', None)

    contact_json = contacts.o365service.get_contact_by_id(
        connection_info.outlook_api_endpoint, connection_info.access_token,
        contact_id, contact_properties)

    if (not contact_json is None):
        # Load the contact into a DisplayContact object
        display_contact = DisplayContact()
        display_contact.load_json(contact_json)

        # Render a details form
        return render(request, 'contacts/details.html',
                      {'contact': display_contact})
    else:
        return render(
            request, 'contacts/error.html', {
                'error_message':
                'Unable to get contact with ID: {0}'.format(contact_id),
            })
示例#3
0
def index(request):
    try:
        # Get the user's connection info
        connection_info = Office365Connection.objects.get(
            username=request.user)

    except ObjectDoesNotExist:
        # If there is no connection object for the user, they haven't connected their
        # Office 365 account yet. The page will ask them to connect.
        return render(request, 'contacts/index.html', None)

    else:
        # If we don't have an access token, request one
        # NOTE: This doesn't check if the token is expired. We could, but
        # we'll just lazily assume it is good. If we try to use it and we
        # get an error, then we can refresh.
        if (connection_info.access_token is None
                or connection_info.access_token == ''):
            # Use the refresh token to request a token for the Contacts API
            access_token = contacts.o365service.get_access_token_from_refresh_token(
                connection_info.refresh_token,
                connection_info.outlook_resource_id)

            # Save the access token
            connection_info.access_token = access_token['access_token']
            connection_info.save()

        user_contacts = contacts.o365service.get_contacts(
            connection_info.outlook_api_endpoint, connection_info.access_token,
            contact_properties)

        if (user_contacts is None):
            # Use the refresh token to request a token for the Contacts API
            access_token = contacts.o365service.get_access_token_from_refresh_token(
                connection_info.refresh_token,
                connection_info.outlook_resource_id)

            # Save the access token
            connection_info.access_token = access_token['access_token']
            connection_info.save()

            user_contacts = contacts.o365service.get_contacts(
                connection_info.outlook_api_endpoint,
                connection_info.access_token, contact_properties)
        contact_list = list()

        for user_contact in user_contacts['value']:
            display_contact = DisplayContact()
            display_contact.load_json(user_contact)
            contact_list.append(display_contact)

        # For now just return the token and the user's email, the page will display it.
        context = {
            'user_email': connection_info.user_email,
            'user_contacts': contact_list
        }
        return render(request, 'contacts/index.html', context)
示例#4
0
def index(request):
    try:
        # Get the user's connection info
        connection_info = Office365Connection.objects.get(username = request.user)
        
    except ObjectDoesNotExist:
        # If there is no connection object for the user, they haven't connected their
        # Office 365 account yet. The page will ask them to connect.
        return render(request, 'contacts/index.html', None)
    
    else:
        # If we don't have an access token, request one
        # NOTE: This doesn't check if the token is expired. We could, but 
        # we'll just lazily assume it is good. If we try to use it and we
        # get an error, then we can refresh.
        if (connection_info.access_token is None or 
            connection_info.access_token == ''):
            # Use the refresh token to request a token for the Contacts API
            access_token = contacts.o365service.get_access_token_from_refresh_token(connection_info.refresh_token, 
                                                                                    connection_info.outlook_resource_id)
            
            # Save the access token
            connection_info.access_token = access_token['access_token']
            connection_info.save()
        
        user_contacts = contacts.o365service.get_contacts(connection_info.outlook_api_endpoint,
                                                          connection_info.access_token, contact_properties)
                                                          
        if (user_contacts is None):
            # Use the refresh token to request a token for the Contacts API
            access_token = contacts.o365service.get_access_token_from_refresh_token(connection_info.refresh_token, 
                                                                                    connection_info.outlook_resource_id)
            
            # Save the access token
            connection_info.access_token = access_token['access_token']
            connection_info.save()
            
            user_contacts = contacts.o365service.get_contacts(connection_info.outlook_api_endpoint,
                                                              connection_info.access_token, contact_properties)
        contact_list = list()
        
        for user_contact in user_contacts['value']:
            display_contact = DisplayContact()
            display_contact.load_json(user_contact)
            contact_list.append(display_contact)
        
        # For now just return the token and the user's email, the page will display it.
        context = { 'user_email': connection_info.user_email,
                    'user_contacts': contact_list }
        return render(request, 'contacts/index.html', context)