def create_pdf():
    ## Normally you would have a PDF on the local disk, in which case you would
    ## do something like this:
    #pdf = BibItem.encode_pdffile('/path/to/my/cool.pdf', 'Testdata')
    ## or:
    #pdf = BibItem.encode_pdf('my.pdf', open('/path/to/my/cool.pdf').read())

    ## But for this example, we just use a simple string
    pdf = BibItem.encode_pdf('my.pdf', 'Testdata')
    return pdf

# Create a bibitem
folder = BibFolder(decode_output=True, **auth)
create_response = folder.create_item(exampleitemid, portal_type,
                                     attributes={'title': 'Example',
                                                 'publication_year': '2012'})
print 'Created {title}'.format(**create_response['attributes'])
print '   View it here: {url}'.format(**create_response)
#pprint(create_response) # Pretty-print the entire response



# Get the created bibitem
itemid = create_response['attributes']['id']
exampleitem = BibItem(itemid, decode_output=True, **auth)


# Pretend to update (no changes are stored in the database, but the response is just like it would be on a regular update)
update_attributes = {'title': 'Updated example item using the Python REST client library'}
pretend_update_response = exampleitem.update(portal_type,
                                             attributes=update_attributes,
                                             pretend=True)
print 'Updated using pretend=True. This does not change the database, as you can see by doing a GET-request:'
get_response = exampleitem.get()
print '    Title stored in the database: ', get_response['attributes']['title']
#pprint(pretend_update_response) # Pretty-print the entire response


# Show the user a DIFF between the pretend_update_response and get_response, and ask them to confirm the changes
print 'Are you sure you want to make the following changes to {id}:'.format(**get_response['attributes'])

def portal_state_updatedmessage(update_response):
    print 'Updated', update_response['attributes']['id']
    print 'The current portal_state is "{portal_state}", and the next possible states are: '.format(**update_response)
    pprint(update_response['portal_state_transitions'])


## Setup our login credentials.
auth = dict(username='******', password='******')
folder = BibFolder(decode_output=True, **auth)
exampleitemid = auth['username'] + '-pubexample'


## Create the item (it will be private by default)
pdf = BibItem.encode_pdf('my.pdf', 'Testdata')
create_response = folder.create_item(exampleitemid, 'ArticleReference',
                                     attributes={'title': 'Example',
                                                 'publication_year': '2012',
                                                 'authors': [{'firstnames': 'Hans Petter',
                                                              'lastname': 'Langtangen',
                                                              'username': '******'},
                                                             {'username': '******',
                                                              'firstnames': 'Carsten',
                                                              'lastname': 'Griwodz'}],
                                                 'simula_pdf_file': pdf})
print 'Created {title}'.format(**create_response['attributes'])
print '   View it here: {url}'.format(**create_response)
print 'The current portal_state is "{portal_state}", and the next possible states are: '.format(**create_response)
pprint(create_response['portal_state_transitions'])
                                                 'simula_pdf_file': create_pdf()})
print 'Created {title}'.format(**create_response['attributes'])
print '   View it here: {url}'.format(**create_response)
#pprint(create_response) # Pretty-print the entire response



###############################################
# Work with the item API
# - Get all attributes of a single item, including the PDF data
# - Update attributes of an item
# - Delete single item via the web-browser
###############################################

itemid = create_response['attributes']['id']
exampleitem = BibItem(itemid, decode_output=True, **auth)

get_response = exampleitem.get(include_filefields=True)
print 'Got:', get_response['url']
print '   url:', get_response['url']
print '   portal_type:', get_response['portal_type']
print '   title:', get_response['attributes']['title']
print '   simula_pdf_file:', get_response['attributes']['simula_pdf_file']
print '   simula_pdf_file decoded:', BibItem.decode_pdf(get_response['attributes']['simula_pdf_file'])
#pprint(get_response) # Pretty-print the entire response


portal_type = get_response['portal_type']
update_response = exampleitem.update(portal_type,
                                     attributes={'title': 'Updated example item using the Python REST client library'})
print 'Updated', update_response['attributes']['id']