-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpostContainerLinksToRecords.py
57 lines (50 loc) · 1.79 KB
/
postContainerLinksToRecords.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
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')
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(targetFile))
asRecord = requests.get(baseURL + '/repositories/' + repository + '/'
+ targetRecord, headers=headers).json()
print(baseURL + '/repositories/' + repository + '/' + targetRecord)
f = open(targetRecord + 'asRecordBackup.json', 'w')
json.dump(asRecord, f)
instanceArray = asRecord['instances']
for row in csv:
uri = row['uri']
print(uri)
top_container = {}
top_container['ref'] = uri
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
f2 = open(targetRecord + 'asRecordModified.json', 'w')
json.dump(asRecord, f2)
asRecord = json.dumps(asRecord)
post = requests.post(baseURL + '/repositories/' + repository + '/'
+ targetRecord, headers=headers, data=asRecord).json()
print(post)