コード例 #1
0
ファイル: views.py プロジェクト: billwaddyjr/pythoncontacts
def update(request, contact_id):
    try:
        # Initialize a DisplayContact object from the posted form data
        updated_contact = DisplayContact()
        updated_contact.given_name = request.POST['first_name']
        updated_contact.last_name = request.POST['last_name']
        updated_contact.mobile_phone = request.POST['mobile_phone']
        updated_contact.email1_address = request.POST['email1_address']
        updated_contact.email1_name = request.POST['email1_name']
        updated_contact.email2_address = request.POST['email2_address']
        updated_contact.email2_name = request.POST['email2_name']
        updated_contact.email3_address = request.POST['email3_address']
        updated_contact.email3_name = request.POST['email3_name']
        
    except (KeyError):
        # If the form data is missing or incomplete, display an error.
        return render(request, 'contacts/error.html',
                {
                    'error_message': 'No contact data included in POST.',
                }
            )
    else:
        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:
            result = contacts.o365service.update_contact(connection_info.outlook_api_endpoint,
                                                         connection_info.access_token,
                                                         contact_id,
                                                         updated_contact.get_json(True))
            
            # Per MSDN, success should be a 200 status
            if (result == 200):
                return HttpResponseRedirect(reverse('contacts:index'))
            else:
                return render(request, 'contacts/error.html',
                    {
                        'error_message': 'Unable to update contact: {0} HTTP status returned.'.format(result),
                    }
                )
コード例 #2
0
def update(request, contact_id):
    try:
        # Initialize a DisplayContact object from the posted form data
        updated_contact = DisplayContact()
        updated_contact.given_name = request.POST['first_name']
        updated_contact.last_name = request.POST['last_name']
        updated_contact.mobile_phone = request.POST['mobile_phone']
        updated_contact.email1_address = request.POST['email1_address']
        updated_contact.email1_name = request.POST['email1_name']
        updated_contact.email2_address = request.POST['email2_address']
        updated_contact.email2_name = request.POST['email2_name']
        updated_contact.email3_address = request.POST['email3_address']
        updated_contact.email3_name = request.POST['email3_name']

    except (KeyError):
        # If the form data is missing or incomplete, display an error.
        return render(request, 'contacts/error.html', {
            'error_message': 'No contact data included in POST.',
        })
    else:
        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:
            result = contacts.o365service.update_contact(
                connection_info.outlook_api_endpoint,
                connection_info.access_token, contact_id,
                updated_contact.get_json(True))

            # Per MSDN, success should be a 200 status
            if (result == 200):
                return HttpResponseRedirect(reverse('contacts:index'))
            else:
                return render(
                    request, 'contacts/error.html', {
                        'error_message':
                        'Unable to update contact: {0} HTTP status returned.'.
                        format(result),
                    })