|
| 1 | +# This script works to create instances (consisting of top_containers) from a separate CSV file. The CSV file should have two columns, indicator and barcode. |
| 2 | +# The directory where this file is stored must match the directory in the filePath variable, below. |
| 3 | +# The script will prompt you first for the exact name of the CSV file, and then for the exact resource or accession to attach the containers to. |
| 4 | + |
| 5 | +import json |
| 6 | +import requests |
| 7 | +import secrets |
| 8 | +import csv |
| 9 | + |
| 10 | +filePath = '[Enter File Path]' |
| 11 | + |
| 12 | +targetFile = raw_input('Enter file name: ') |
| 13 | +targetRecord = raw_input('Enter record type and id (e.g. \'accessions/2049\'): ') |
| 14 | + |
| 15 | +baseURL = secrets.baseURL |
| 16 | +user = secrets.user |
| 17 | +password = secrets.password |
| 18 | + |
| 19 | +auth = requests.post(baseURL + '/users/'+user+'/login?password='+password).json() |
| 20 | +session = auth["session"] |
| 21 | +headers = {'X-ArchivesSpace-Session':session, 'Content_Type':'application/json'} |
| 22 | + |
| 23 | +csv = csv.DictReader(open(filePath+targetFile)) |
| 24 | + |
| 25 | +containerList = [] |
| 26 | +for row in csv: |
| 27 | + containerRecord = {} |
| 28 | + containerRecord['barcode'] = row['barcode'] |
| 29 | + containerRecord['indicator'] = row['indicator'] |
| 30 | + containerRecord = json.dumps(containerRecord) |
| 31 | + post = requests.post(baseURL + '/repositories/3/top_containers', headers=headers, data=containerRecord).json() |
| 32 | + print post |
| 33 | + containerList.append(post['uri'].encode('utf-8')) |
| 34 | + |
| 35 | +asRecord = requests.get(baseURL+'/repositories/3/'+targetRecord, headers=headers).json() |
| 36 | +instanceArray = asRecord['instances'] |
| 37 | + |
| 38 | +for i in range (0, len (containerList)): |
| 39 | + top_container = {} |
| 40 | + top_container['ref'] = containerList[i] |
| 41 | + sub_container = {} |
| 42 | + sub_container['top_container'] = top_container |
| 43 | + instance = {} |
| 44 | + instance['sub_container'] = sub_container |
| 45 | + instance['instance_type'] = 'mixed_materials' |
| 46 | + instanceArray.append(instance) |
| 47 | + |
| 48 | +asRecord['instances'] = instanceArray |
| 49 | +asRecord = json.dumps(asRecord) |
| 50 | +post = requests.post(baseURL+'/repositories/3/'+targetRecord, headers=headers, data=asRecord).json() |
| 51 | +print post |
0 commit comments