Skip to content

Commit 4fb0b11

Browse files
authored
Merge pull request #1066 from scitran/match-timestamp
On packfile upload, use acquisition timestamp and subject code to find containers
2 parents 870730f + c9addfc commit 4fb0b11

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

api/placer.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,9 @@ def __init__(self, container_type, container, id_, metadata, timestamp, origin,
382382
# Populated in check(), used in finalize()
383383
self.p_id = None
384384
self.s_label = None
385+
self.s_code = None
385386
self.a_label = None
387+
self.a_time = None
386388
self.g_id = None
387389

388390
self.permissions = {}
@@ -422,6 +424,12 @@ def check(self):
422424
self.s_label = self.metadata['session']['label']
423425
self.a_label = self.metadata['acquisition']['label']
424426

427+
# Save additional fields if provided
428+
self.s_code = self.metadata['session'].get('subject', {}).get('code')
429+
self.a_time = self.metadata['acquisition'].get('timestamp')
430+
if self.a_time:
431+
self.a_time = dateutil.parser.parse(self.a_time)
432+
425433
# Get project info that we need later
426434
project = config.db['projects'].find_one({ '_id': bson.ObjectId(self.p_id)})
427435
self.permissions = project.get('permissions', {})
@@ -547,6 +555,10 @@ def finalize(self):
547555
'group': self.g_id
548556
}
549557

558+
if self.s_code:
559+
# If they supplied a subject code, use that in the query as well
560+
query['subject.code'] = self.s_code
561+
550562
# Updates if existing
551563
updates = {}
552564
updates['permissions'] = self.permissions
@@ -555,6 +567,7 @@ def finalize(self):
555567

556568
# Extra properties on insert
557569
insert_map = copy.deepcopy(query)
570+
insert_map.pop('subject.code', None) # Remove query term that should not become part of the payload
558571
insert_map['created'] = self.timestamp
559572
insert_map.update(self.metadata['session'])
560573
insert_map['subject'] = containerutil.add_id_to_subject(insert_map.get('subject'), bson.ObjectId(self.p_id))
@@ -576,6 +589,11 @@ def finalize(self):
576589
'label': self.a_label,
577590
}
578591

592+
if self.a_time:
593+
# If they supplied an acquisition timestamp, use that in the query as well
594+
query['timestamp'] = self.a_time
595+
596+
579597
# Updates if existing
580598
updates = {}
581599
updates['permissions'] = self.permissions

tests/integration_tests/python/test_uploads.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,66 @@ def test_packfile_upload(data_builder, file_form, as_admin, as_root, api_db):
11321132
assert acquisition.get('label') == 'test-packfile-timestamp'
11331133

11341134

1135+
# Test that acquisition timestamp is used to differenciate acquisitions and session code for sessions
1136+
1137+
# Make sure there is only one session and one acquisition with the above label to start
1138+
sessions = list(api_db.sessions.find({'label':'test-packfile-timestamp'}))
1139+
acquisitions = list(api_db.acquisitions.find({'label':'test-packfile-timestamp'}))
1140+
assert len(sessions) == 1
1141+
assert len(acquisitions) == 1
1142+
1143+
1144+
r = as_admin.post('/projects/' + project + '/packfile-start')
1145+
assert r.ok
1146+
token = r.json()['token']
1147+
r = as_admin.post('/projects/' + project + '/packfile',
1148+
params={'token': token}, files=file_form('one.csv'))
1149+
assert r.ok
1150+
1151+
metadata_json = json.dumps({
1152+
'project': {'_id': project},
1153+
'session': {
1154+
'label': 'test-packfile-timestamp',
1155+
'subject': {
1156+
'code': 'new-subject'
1157+
}
1158+
},
1159+
'acquisition': {
1160+
'label': 'test-packfile-timestamp',
1161+
'timestamp': '1999-01-01T00:00:00+00:00'
1162+
},
1163+
'packfile': {'type': 'test'}
1164+
})
1165+
1166+
r = as_admin.post('/projects/' + project + '/packfile-end',
1167+
params={'token': token, 'metadata': metadata_json})
1168+
assert r.ok
1169+
1170+
sessions = list(api_db.sessions.find({'label':'test-packfile-timestamp'}))
1171+
acquisitions = list(api_db.acquisitions.find({'label':'test-packfile-timestamp'}))
1172+
1173+
# Ensure a new session was created
1174+
assert len(sessions) == 2
1175+
1176+
# Ensure a new acquisition was created
1177+
assert len(acquisitions) == 2
1178+
1179+
# Ensure subject code exists on a session
1180+
for s in sessions:
1181+
if s.get('subject', {}).get('code') == 'new-subject':
1182+
break
1183+
else:
1184+
# We didn't fine one
1185+
assert False
1186+
1187+
# Ensure second acquisition timestamp exists on an acquisition
1188+
for a in acquisitions:
1189+
if str(a.get('timestamp')) == '1999-01-01 00:00:00':
1190+
break
1191+
else:
1192+
# We didn't fine one
1193+
assert False
1194+
11351195
# get another token (start packfile-upload)
11361196
r = as_admin.post('/projects/' + project + '/packfile-start')
11371197
assert r.ok

0 commit comments

Comments
 (0)