-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpostContainersFromCSV.py
72 lines (62 loc) · 2.45 KB
/
postContainersFromCSV.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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.
# The directory where this file is stored must match the directory in the
# filePath variable, below.
# 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.
import json
import requests
import csv
secretsVersion = input('To edit production server, enter the name of the \
secrets file: ')
if secretsVersion != '':
try:
secrets = __import__(secretsVersion)
print('Editing Production')
except ImportError:
secrets = __import__('secrets')
print('Editing Development')
else:
print('Editing Development')
filePath = '[Enter File Path]'
targetFile = input('Enter file name: ')
targetRecord = input('Enter record type and id (e.g. \'accessions/2049\'): ')
baseURL = secrets.baseURL
user = secrets.user
password = secrets.password
repository = secrets.repository
auth = requests.post(baseURL + '/users/' + user + '/login?password='
+ password).json()
session = auth['session']
headers = {'X-ArchivesSpace-Session': session,
'Content_Type': 'application/json'}
csv = csv.DictReader(open(filePath + targetFile))
containerList = []
for row in csv:
containerRecord = {}
containerRecord['barcode'] = row['barcode']
containerRecord['indicator'] = row['indicator']
containerRecord = json.dumps(containerRecord)
post = requests.post(baseURL + '/repositories/' + repository
+ '/top_containers', headers=headers,
data=containerRecord).json()
print(post)
containerList.append(post['uri'])
asRecord = requests.get(baseURL + '/repositories/' + repository + '/'
+ targetRecord, headers=headers).json()
instanceArray = asRecord['instances']
for i in range(0, len(containerList)):
top_container = {}
top_container['ref'] = containerList[i]
sub_container = {}
sub_container['top_container'] = top_container
instance = {}
instance['sub_container'] = sub_container
instance['instance_type'] = 'mixed_materials'
instanceArray.append(instance)
asRecord['instances'] = instanceArray
asRecord = json.dumps(asRecord)
post = requests.post(baseURL + '/repositories/' + repository + '/'
+ targetRecord, headers=headers, data=asRecord).json()
print(post)