Skip to content
Snippets Groups Projects
Unverified Commit 4fb0b111 authored by Megan Henning's avatar Megan Henning Committed by GitHub
Browse files

Merge pull request #1066 from scitran/match-timestamp

On packfile upload, use acquisition timestamp and subject code to find containers
parents 870730f4 c9addfc3
No related branches found
No related tags found
No related merge requests found
......@@ -382,7 +382,9 @@ class PackfilePlacer(Placer):
# Populated in check(), used in finalize()
self.p_id = None
self.s_label = None
self.s_code = None
self.a_label = None
self.a_time = None
self.g_id = None
self.permissions = {}
......@@ -422,6 +424,12 @@ class PackfilePlacer(Placer):
self.s_label = self.metadata['session']['label']
self.a_label = self.metadata['acquisition']['label']
# Save additional fields if provided
self.s_code = self.metadata['session'].get('subject', {}).get('code')
self.a_time = self.metadata['acquisition'].get('timestamp')
if self.a_time:
self.a_time = dateutil.parser.parse(self.a_time)
# Get project info that we need later
project = config.db['projects'].find_one({ '_id': bson.ObjectId(self.p_id)})
self.permissions = project.get('permissions', {})
......@@ -547,6 +555,10 @@ class PackfilePlacer(Placer):
'group': self.g_id
}
if self.s_code:
# If they supplied a subject code, use that in the query as well
query['subject.code'] = self.s_code
# Updates if existing
updates = {}
updates['permissions'] = self.permissions
......@@ -555,6 +567,7 @@ class PackfilePlacer(Placer):
# Extra properties on insert
insert_map = copy.deepcopy(query)
insert_map.pop('subject.code', None) # Remove query term that should not become part of the payload
insert_map['created'] = self.timestamp
insert_map.update(self.metadata['session'])
insert_map['subject'] = containerutil.add_id_to_subject(insert_map.get('subject'), bson.ObjectId(self.p_id))
......@@ -576,6 +589,11 @@ class PackfilePlacer(Placer):
'label': self.a_label,
}
if self.a_time:
# If they supplied an acquisition timestamp, use that in the query as well
query['timestamp'] = self.a_time
# Updates if existing
updates = {}
updates['permissions'] = self.permissions
......
......@@ -1132,6 +1132,66 @@ def test_packfile_upload(data_builder, file_form, as_admin, as_root, api_db):
assert acquisition.get('label') == 'test-packfile-timestamp'
# Test that acquisition timestamp is used to differenciate acquisitions and session code for sessions
# Make sure there is only one session and one acquisition with the above label to start
sessions = list(api_db.sessions.find({'label':'test-packfile-timestamp'}))
acquisitions = list(api_db.acquisitions.find({'label':'test-packfile-timestamp'}))
assert len(sessions) == 1
assert len(acquisitions) == 1
r = as_admin.post('/projects/' + project + '/packfile-start')
assert r.ok
token = r.json()['token']
r = as_admin.post('/projects/' + project + '/packfile',
params={'token': token}, files=file_form('one.csv'))
assert r.ok
metadata_json = json.dumps({
'project': {'_id': project},
'session': {
'label': 'test-packfile-timestamp',
'subject': {
'code': 'new-subject'
}
},
'acquisition': {
'label': 'test-packfile-timestamp',
'timestamp': '1999-01-01T00:00:00+00:00'
},
'packfile': {'type': 'test'}
})
r = as_admin.post('/projects/' + project + '/packfile-end',
params={'token': token, 'metadata': metadata_json})
assert r.ok
sessions = list(api_db.sessions.find({'label':'test-packfile-timestamp'}))
acquisitions = list(api_db.acquisitions.find({'label':'test-packfile-timestamp'}))
# Ensure a new session was created
assert len(sessions) == 2
# Ensure a new acquisition was created
assert len(acquisitions) == 2
# Ensure subject code exists on a session
for s in sessions:
if s.get('subject', {}).get('code') == 'new-subject':
break
else:
# We didn't fine one
assert False
# Ensure second acquisition timestamp exists on an acquisition
for a in acquisitions:
if str(a.get('timestamp')) == '1999-01-01 00:00:00':
break
else:
# We didn't fine one
assert False
# get another token (start packfile-upload)
r = as_admin.post('/projects/' + project + '/packfile-start')
assert r.ok
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment