diff --git a/api/placer.py b/api/placer.py index 107aacee4db1dba28222e737845f4e924e1cace6..0c642d9fc90edb5a304a4a428eb320eaf64aeb94 100644 --- a/api/placer.py +++ b/api/placer.py @@ -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 diff --git a/tests/integration_tests/python/test_uploads.py b/tests/integration_tests/python/test_uploads.py index 120115c713ac870c50af13c8a7f33dc7c27cc9b5..e17a9dbfb1161cb77e5c3d4357a5308c249b0bf8 100644 --- a/tests/integration_tests/python/test_uploads.py +++ b/tests/integration_tests/python/test_uploads.py @@ -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