references/collections.md
3.8 KB查看排版效果| 1 | # Collection Management |
| 2 | |
| 3 | ## Reading Collections |
| 4 | |
| 5 | ```python |
| 6 | # All collections (flat list including nested) |
| 7 | all_cols = zot.everything(zot.collections()) |
| 8 | |
| 9 | # Only top-level collections |
| 10 | top_cols = zot.collections_top() |
| 11 | |
| 12 | # Specific collection |
| 13 | col = zot.collection('COLKEY') |
| 14 | |
| 15 | # Sub-collections of a collection |
| 16 | sub_cols = zot.collections_sub('COLKEY') |
| 17 | |
| 18 | # All collections under a given collection (recursive) |
| 19 | tree = zot.all_collections('COLKEY') |
| 20 | # Or all collections in the library: |
| 21 | tree = zot.all_collections() |
| 22 | ``` |
| 23 | |
| 24 | ## Collection Data Structure |
| 25 | |
| 26 | ```python |
| 27 | col = zot.collection('5TSDXJG6') |
| 28 | name = col['data']['name'] |
| 29 | key = col['data']['key'] |
| 30 | parent = col['data']['parentCollection'] # False if top-level, else parent key |
| 31 | version = col['data']['version'] |
| 32 | n_items = col['meta']['numItems'] |
| 33 | n_sub_collections = col['meta']['numCollections'] |
| 34 | ``` |
| 35 | |
| 36 | ## Creating Collections |
| 37 | |
| 38 | ```python |
| 39 | # Create a top-level collection |
| 40 | zot.create_collections([{'name': 'My New Collection'}]) |
| 41 | |
| 42 | # Create a nested collection |
| 43 | zot.create_collections([{ |
| 44 | 'name': 'Sub-Collection', |
| 45 | 'parentCollection': 'PARENTCOLKEY' |
| 46 | }]) |
| 47 | |
| 48 | # Create a parent, inspect the result, then use its real key for a child. |
| 49 | result = zot.create_collections([{'name': 'Collection B'}]) |
| 50 | if result.get('failed'): |
| 51 | raise RuntimeError(result['failed']) |
| 52 | parent_key = result['successful']['0']['key'] |
| 53 | child_result = zot.create_collections([{'name': 'Sub-B', 'parentCollection': parent_key}]) |
| 54 | if child_result.get('failed'): |
| 55 | raise RuntimeError(child_result['failed']) |
| 56 | ``` |
| 57 | |
| 58 | ## Updating Collections |
| 59 | |
| 60 | ```python |
| 61 | cols = zot.collections() |
| 62 | # Rename the first collection, if there is one |
| 63 | if cols: |
| 64 | cols[0]['data']['name'] = 'Renamed Collection' |
| 65 | zot.update_collection(cols[0]) |
| 66 | |
| 67 | # For multiple collections, use individually versioned updates. |
| 68 | # Re-fetch after a prior write to avoid reusing the old version. |
| 69 | for col in cols: |
| 70 | fresh = zot.collection(col['key']) |
| 71 | fresh['data']['name'] = col['data']['name'] |
| 72 | zot.update_collection(fresh) |
| 73 | ``` |
| 74 | |
| 75 | ## Deleting Collections |
| 76 | |
| 77 | ```python |
| 78 | # Delete a single collection |
| 79 | col = zot.collection('COLKEY') |
| 80 | zot.delete_collection(col) |
| 81 | |
| 82 | # Delete multiple collections |
| 83 | cols = zot.collections(limit=50) # review the selected keys; max 50 |
| 84 | library_version = int(zot.request.headers['Last-Modified-Version']) |
| 85 | if cols: |
| 86 | zot.delete_collection(cols, last_modified=library_version) |
| 87 | ``` |
| 88 | |
| 89 | ## Managing Items in Collections |
| 90 | |
| 91 | ```python |
| 92 | # Add an item to a collection |
| 93 | item = zot.item('ITEMKEY') |
| 94 | zot.addto_collection('COLKEY', item) |
| 95 | |
| 96 | # Remove an item from a collection (re-fetch after any earlier write) |
| 97 | item = zot.item('ITEMKEY') |
| 98 | zot.deletefrom_collection('COLKEY', item) |
| 99 | |
| 100 | # Move between collections with one version-checked request |
| 101 | item = zot.item('ITEMKEY') |
| 102 | zot.moveto_collection('OLDCOLKEY', 'NEWCOLKEY', item) |
| 103 | |
| 104 | # Get all items in a collection |
| 105 | items = zot.everything(zot.collection_items('COLKEY')) |
| 106 | |
| 107 | # Get only top-level items in a collection |
| 108 | top_items = zot.collection_items_top('COLKEY') |
| 109 | |
| 110 | # Count items in a collection |
| 111 | n = zot.num_collectionitems('COLKEY') |
| 112 | |
| 113 | # Get tags in a collection |
| 114 | tags = zot.collection_tags('COLKEY') |
| 115 | ``` |
| 116 | |
| 117 | ## Find Collection Key by Name |
| 118 | |
| 119 | ```python |
| 120 | def find_collection(zot, name): |
| 121 | for col in zot.everything(zot.collections()): |
| 122 | if col['data']['name'] == name: |
| 123 | return col['data']['key'] |
| 124 | return None |
| 125 | |
| 126 | key = find_collection(zot, 'Machine Learning Papers') |
| 127 | ``` |
| 128 | |
| 129 | Collection deletion also removes descendant collections, but preserves their items in the library. Create and batch-update responses use the same `successful`/`success`/`unchanged`/`failed` maps as items; creation accepts at most 50 collections per request. |
| 130 | |
| 131 | In Pyzotero 1.15.2, `update_collections()` routes collection dictionaries through the item-field validator and can reject `name`/`parentCollection`; the per-collection `update_collection()` example avoids that path. Its single-object precondition is the collection version; multi-delete needs the library version. |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

