urschrei / pyzotero

Pyzotero: a Python client for the Zotero API
https://pyzotero.readthedocs.org
Other
908 stars 99 forks source link

Unable to add multiple creators #108

Closed michaelbriordan closed 4 years ago

michaelbriordan commented 4 years ago

General

Platform: MacOS X Python version: 3.7.5 Pyzotero version: latest release

Problem description

Unable to create JSON arrays (creators) and objects (collections) with new items.

template['creators'][0]['creatorType'] = "blah"

works

template['creators'][1]['creatorType'] = "blah"

does not

Seems to also apply to objects, thus

template['collections'][0] = "blah"

returns

list assignment index out of range

Adding additional creatos raises no error but does not produce additional creators. Attempting to assign a collection returns:

IndexError: list assignment index out of range

If possible, paste the full traceback in a code block below, and fill in the summary

urschrei commented 4 years ago

Could you add a little more context about what you are trying to do? What API call resulted in template? What are you hoping to pass it to?

michaelbriordan commented 4 years ago

I am trying to use the Zotero.create_items method

exceptions are thrown when I try to assign a value to arrays or objects

  1. Arrays
    template = zot.item_template(‘manuscript’) # I’m using ‘manuscript’ and ‘letter’ only with this script
         …  
    template['collections'][0] = "UBJW58IP"
        …
         resp = zot.create_items([template])

Raises exception:

IndexError: list assignment index out of range

If I remove this code, or pass over the exception, the item is created but no collection assigned.

  1. Objects
    template = zot.item_template(‘letter’)
    …
           template['creators'][1]['creatorType'] = 'recipient' 
               template['creators'][1]['firstName'] = “Some Other Person”
                template['creators'][1]['firstName'] = “Some Other Person”
        ...
               resp = zot.create_items([template])

raises an Index Exception, but the first object, ie. template['creators'][0]['creatorType'] does not raise any exception. If I remove this code, or pass over the exception, the item is created, without the second creator.

urschrei commented 4 years ago

I can't reproduce this:

template = zot.item_template("manuscript")
c = zot.collections()
template["collections"].append(c[0]['key'])
resp = zot.create_items([template])

Creates a new item as expected.

As for the second problem, unless I'm misunderstanding you, you need to append a dict containing creator data to the creators list first. You can't just specify a new key for a dict that doesn't exist yet: creators is a list with a single dict entry, so the 1 index doesn't work.

template = zot.item_template("letter")
creators_dict = template['creators'][0]
template['creators'][0]['firstName'] = "foo"
creators_dict['firstName'] = "bar"
template['creators'].append(creators_dict)
resp = zot.create_items([template])

In the absence of a reproducible example demonstrating an error, I'm going to close this issue. Feel free to reopen if something isn't working.