def post_upload(context, request): properties = context.upgrade_properties() if properties['status'] not in ('uploading', 'upload failed'): raise HTTPForbidden( 'status must be "uploading" to issue new credentials') accession_or_external = properties.get( 'accession') or properties['external_accession'] external = context.propsheets.get('external', None) if external is None: # Handle objects initially posted as another state. bucket = request.registry.settings['file_upload_bucket'] uuid = context.uuid mapping = context.schema['file_format_file_extension'] file_extension = mapping[properties['file_format']] date = properties['date_created'].split('T')[0].replace('-', '/') key = '{date}/{uuid}/{accession_or_external}{file_extension}'.format( accession_or_external=accession_or_external, date=date, file_extension=file_extension, uuid=uuid, **properties) elif external.get('service') == 's3': bucket = external['bucket'] key = external['key'] else: raise ValueError(external.get('service')) name = 'up{time:.6f}-{accession_or_external}'.format( accession_or_external=accession_or_external, time=time.time(), **properties)[:32] # max 32 chars profile_name = request.registry.settings.get('file_upload_profile_name') creds = external_creds(bucket, key, name, profile_name) new_properties = None if properties['status'] == 'upload failed': new_properties = properties.copy() new_properties['status'] = 'uploading' registry = request.registry registry.notify(BeforeModified(context, request)) context.update(new_properties, {'external': creds}) registry.notify(AfterModified(context, request)) rendered = request.embed('/%s/@@object' % context.uuid, as_user=True) result = { 'status': 'success', '@type': ['result'], '@graph': [rendered], } return result
def _update_status(self, new_status, current_status, properties, schema, request, item_id, update): # Don't update if update parameter not true. if not update: return # Don't actually patch if the same. if new_status == current_status: return properties['status'] = new_status # Some release specific functionality. if new_status == 'released': # This won't be reassigned if you rerelease something. if 'date_released' in schema[ 'properties'] and 'date_released' not in properties: properties['date_released'] = str(datetime.now().date()) request.registry.notify(BeforeModified(self, request)) self.update(properties) request.registry.notify(AfterModified(self, request)) request._set_status_changed_paths.add( (item_id, current_status, new_status))
def post_upload(context, request): properties = context.upgrade_properties() if properties['status'] not in ('uploading', 'upload failed'): raise HTTPForbidden( 'status must be "uploading" to issue new credentials') accession_or_external = properties.get( 'accession') or properties['external_accession'] file_upload_bucket = request.registry.settings['file_upload_bucket'] external = context.propsheets.get('external', None) registry = request.registry if external is None: # Handle objects initially posted as another state. bucket = file_upload_bucket uuid = context.uuid mapping = context.schema['file_format_file_extension'] file_extension = mapping[properties['file_format']] date = properties['date_created'].split('T')[0].replace('-', '/') key = '{date}/{uuid}/{accession_or_external}{file_extension}'.format( accession_or_external=accession_or_external, date=date, file_extension=file_extension, uuid=uuid, **properties) elif external.get('service') == 's3': bucket = external['bucket'] # Must reset file to point to file_upload_bucket (keep AWS public dataset in sync). if bucket != file_upload_bucket: registry.notify(BeforeModified(context, request)) context._set_external_sheet({'bucket': file_upload_bucket}) registry.notify(AfterModified(context, request)) bucket = file_upload_bucket key = external['key'] else: raise HTTPNotFound(detail='External service {} not expected'.format( external.get('service'))) name = 'up{time:.6f}-{accession_or_external}'.format( accession_or_external=accession_or_external, time=time.time(), **properties)[:32] # max 32 chars profile_name = request.registry.settings.get('file_upload_profile_name') upload_creds = UploadCredentials(bucket, key, name, profile_name=profile_name) s3_transfer_allow = request.registry.settings.get( 'external_aws_s3_transfer_allow', 'false') creds = upload_creds.external_creds( s3_transfer_allow=asbool(s3_transfer_allow), s3_transfer_buckets=request.registry.settings.get( 'external_aws_s3_transfer_buckets'), ) new_properties = None if properties['status'] == 'upload failed': new_properties = properties.copy() new_properties['status'] = 'uploading' registry.notify(BeforeModified(context, request)) context.update(new_properties, {'external': creds}) registry.notify(AfterModified(context, request)) rendered = request.embed('/%s/@@object' % context.uuid, as_user=True) result = { 'status': 'success', '@type': ['result'], '@graph': [rendered], } return result