-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrateLocations.py
208 lines (200 loc) · 8.34 KB
/
migrateLocations.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from openpyxl import load_workbook
from archives_tools import uaLocations
from archives_tools import aspace as AS
import os
__location__ = os.path.dirname(os.path.realpath(__file__))
collectionListFile = os.path.join(__location__, "collectionList.xlsx")
collectionWorkbook = load_workbook(filename = collectionListFile, read_only=True)
collectionList = collectionWorkbook.get_sheet_by_name('collectionList')
loginData = AS.getLogin()
session = AS.getSession(loginData)
repo = "2"
rowIndex = 0
collections = []
noLocation = []
for row in collectionList.rows:
rowIndex = rowIndex + 1
if rowIndex > 49:
print ("***********************************************************************************************************")
ID = row[0].value
print (str(rowIndex) + ". " + ID)
if not row[15].value is None:
locationCode = row[15].value
if not ";" in str(locationCode):
#single location
coordinateList = []
coordinates = uaLocations.location2ASpace(str(locationCode).strip())
if coordinates[1] is True:
for coordSet in coordinates[0]:
coordinateList.append(coordSet)
else:
coordinateList.append(coordinates[0])
collectionObject = AS.getResourceID(session, repo, ID, loginData)
#take this out before production migration
if collectionObject is None:
pass
else:
hasChildren = False
tree = AS.getTree(session, collectionObject, loginData)
for child in tree.children:
if not "website" in child.title.lower() and not "web archives" in child.title.lower():
hasChildren = True
if hasChildren == False:
boxObject = AS.makeContainer(session, repo, "Collection", "X", loginData)
for location in coordinateList:
locationURI = AS.findLocation(session, location["Title"], loginData)
locFound = False
if len(boxObject.container_locations) > 0:
for listedLoc in boxObject.container_locations:
if listedLoc["ref"] == locationURI:
locFound = True
if locFound == False:
boxUpdates = True
locationObject = AS.getLocation(session, locationURI, loginData)
if len(coordinateList) > 1:
locationObject.temporary = "collection_level"
post0 = AS.postLocation(session, locationObject, loginData)
if post0 != 200:
print ("location:" + str(post0))
boxObject = AS.addToLocation(boxObject, locationURI, location["Note"], "previous", "2999-01-01")
else:
boxObject = AS.addToLocation(boxObject, locationURI)
post1 = AS.postContainer(session, repo, boxObject, loginData)
if post1 != 200:
print (post1)
collectionObject = AS.addToContainer(session, collectionObject, boxObject.uri, None, None, loginData)
post2 = AS.postResource(session, repo, collectionObject, loginData)
if post2 != 200:
print (post2)
else:
uriList = []
def findBoxes(tree, uriList):
for child in tree.children:
if len(child.containers) < 1:
findBoxes(child, uriList)
else:
aoObject = AS.getArchObj(session, child.record_uri, loginData)
for box in aoObject.instances:
if "sub_container" in box.keys():
boxURI = box.sub_container.top_container.ref
if not boxURI in uriList:
uriList.append(boxURI)
return uriList
uriList = findBoxes(tree, uriList)
for boxURI in uriList:
boxObject = AS.getContainer(session, boxURI, loginData)
for location in coordinateList:
locationURI = AS.findLocation(session, location["Title"], loginData)
locFound = False
if len(boxObject.container_locations) > 0:
for listedLoc in boxObject.container_locations:
if listedLoc["ref"] == locationURI:
locFound = True
if locFound == False:
boxUpdates = True
locationObject = AS.getLocation(session, locationURI, loginData)
if len(coordinateList) > 1:
locationObject.temporary = "collection_level"
post0 = AS.postLocation(session, locationObject, loginData)
if post0 != 200:
print ("location:" + str(post0))
boxObject = AS.addToLocation(boxObject, locationURI, location["Note"], "previous", "2999-01-01")
else:
boxObject = AS.addToLocation(boxObject, locationURI)
post1 = AS.postContainer(session, repo, boxObject, loginData)
if post1 != 200:
print ("container: " + str(post1))
else:
#multiple locations
coordinateList = []
for place in str(locationCode).split(";"):
print place
print "\n"
coordinates = uaLocations.location2ASpace(place.strip())
if coordinates[1] is True:
for coordSet in coordinates[0]:
coordinateList.append(coordSet)
else:
coordinateList.append(coordinates[0])
print coordinateList
print "\n\n"
collectionObject = AS.getResourceID(session, repo, ID, loginData)
#take this out before production migration
if collectionObject is None:
pass
else:
hasChildren = False
tree = AS.getTree(session, collectionObject, loginData)
for child in tree.children:
if not "website" in child.title.lower() and not "web archives" in child.title.lower():
hasChildren = True
if hasChildren == False:
boxObject = AS.makeContainer(session, repo, "Collection", "X", loginData)
for location in coordinateList:
locationURI = AS.findLocation(session, location["Title"], loginData)
locFound = False
if len(boxObject.container_locations) > 0:
for listedLoc in boxObject.container_locations:
if listedLoc["ref"] == locationURI:
locFound = True
if locFound == False:
boxUpdates = True
locationObject = AS.getLocation(session, locationURI, loginData)
if len(coordinateList) > 1:
locationObject.temporary = "collection_level"
post0 = AS.postLocation(session, locationObject, loginData)
if post0 != 200:
print ("location:" + str(post0))
boxObject = AS.addToLocation(boxObject, locationURI, location["Note"], "previous", "2999-01-01")
else:
boxObject = AS.addToLocation(boxObject, locationURI)
post1 = AS.postContainer(session, repo, boxObject, loginData)
if post1 != 200:
print (post1)
collectionObject = AS.addToContainer(session, collectionObject, boxObject.uri, None, None, loginData)
post2 = AS.postResource(session, repo, collectionObject, loginData)
if post2 != 200:
print (post2)
else:
uriList = []
def findBoxes(tree, uriList):
for child in tree.children:
if len(child.containers) < 1:
findBoxes(child, uriList)
else:
aoObject = AS.getArchObj(session, child.record_uri, loginData)
for box in aoObject.instances:
if "sub_container" in box.keys():
boxURI = box.sub_container.top_container.ref
if not boxURI in uriList:
uriList.append(boxURI)
return uriList
uriList = findBoxes(tree, uriList)
boxUpdates = False
for boxURI in uriList:
boxObject = AS.getContainer(session, boxURI, loginData)
for location in coordinateList:
locationURI = AS.findLocation(session, location["Title"], loginData)
locFound = False
if len(boxObject.container_locations) > 0:
for listedLoc in boxObject.container_locations:
if listedLoc["ref"] == locationURI:
locFound = True
if locFound == False:
boxUpdates = True
locationObject = AS.getLocation(session, locationURI, loginData)
if len(coordinateList) > 1:
locationObject.temporary = "collection_level"
post0 = AS.postLocation(session, locationObject, loginData)
if post0 != 200:
print ("location:" + str(post0))
boxObject = AS.addToLocation(boxObject, locationURI, location["Note"], "previous", "2999-01-01")
else:
boxObject = AS.addToLocation(boxObject, locationURI)
if boxUpdates == True:
boxName = boxObject.display_string
post1 = AS.postContainer(session, repo, boxObject, loginData)
print " -->posted " + str(boxName)
if post1 != 200:
print ("container:" + str(post1))
collectionObject = AS.addToContainer(session, collectionObject, boxObject.uri, None, None, loginData)