Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions api/placer.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,9 @@ def __init__(self, container_type, container, id_, metadata, timestamp, origin,
# 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 = {}
Expand Down Expand Up @@ -422,6 +424,12 @@ def check(self):
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', {})
Expand Down Expand Up @@ -547,6 +555,10 @@ def finalize(self):
'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
Expand All @@ -555,6 +567,7 @@ def finalize(self):

# 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))
Expand All @@ -576,6 +589,11 @@ def finalize(self):
'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
Expand Down
60 changes: 60 additions & 0 deletions tests/integration_tests/python/test_uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down