Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Query parameters:
- `force`: 1 to allow deletion of bundles that have descendants or that
appear across multiple worksheets, or 0 to throw an error if any of the
specified bundles have multiple references. Default is 0.
- `recursive`: 1 to remove all bundles downstream too, or 0 otherwise.
Default is 0.
- `data-only`: 1 to only remove contents of the bundle(s) from the bundle
store and leave the bundle metadata intact, or 0 to remove both the
bundle contents and the bundle metadata. Default is 0.
- `dry-run`: 1 to just return list of bundles that would be deleted with
the given parameters without actually deleting them, or 0 to perform
the deletion. Default is 0.
"""
uuids = get_resource_ids(request.json, 'bundles')
force = query_get_bool('force', default=False)
recursive = query_get_bool('recursive', default=False)
data_only = query_get_bool('data-only', default=False)
dry_run = query_get_bool('dry-run', default=False)
deleted_uuids = delete_bundles(
uuids, force=force, recursive=recursive, data_only=data_only, dry_run=dry_run
)
# Return list of deleted ids as meta
return json_api_meta({}, {'ids': deleted_uuids})
- `force`: 1 to allow deletion of bundles that have descendants or that
appear across multiple worksheets, or 0 to throw an error if any of the
specified bundles have multiple references. Default is 0.
- `recursive`: 1 to remove all bundles downstream too, or 0 otherwise.
Default is 0.
- `data-only`: 1 to only remove contents of the bundle(s) from the bundle
store and leave the bundle metadata intact, or 0 to remove both the
bundle contents and the bundle metadata. Default is 0.
- `dry-run`: 1 to just return list of bundles that would be deleted with
the given parameters without actually deleting them, or 0 to perform
the deletion. Default is 0.
"""
uuids = get_resource_ids(request.json, 'bundles')
force = query_get_bool('force', default=False)
recursive = query_get_bool('recursive', default=False)
data_only = query_get_bool('data-only', default=False)
dry_run = query_get_bool('dry-run', default=False)
deleted_uuids = delete_bundles(
uuids, force=force, recursive=recursive, data_only=data_only, dry_run=dry_run
)
# Return list of deleted ids as meta
return json_api_meta({}, {'ids': deleted_uuids})
Query parameters:
- `force`: 1 to allow deletion of bundles that have descendants or that
appear across multiple worksheets, or 0 to throw an error if any of the
specified bundles have multiple references. Default is 0.
- `recursive`: 1 to remove all bundles downstream too, or 0 otherwise.
Default is 0.
- `data-only`: 1 to only remove contents of the bundle(s) from the bundle
store and leave the bundle metadata intact, or 0 to remove both the
bundle contents and the bundle metadata. Default is 0.
- `dry-run`: 1 to just return list of bundles that would be deleted with
the given parameters without actually deleting them, or 0 to perform
the deletion. Default is 0.
"""
uuids = get_resource_ids(request.json, 'bundles')
force = query_get_bool('force', default=False)
recursive = query_get_bool('recursive', default=False)
data_only = query_get_bool('data-only', default=False)
dry_run = query_get_bool('dry-run', default=False)
deleted_uuids = delete_bundles(
uuids, force=force, recursive=recursive, data_only=data_only, dry_run=dry_run
)
# Return list of deleted ids as meta
return json_api_meta({}, {'ids': deleted_uuids})
# Store the data.
try:
if request.query.urls:
sources = query_get_list('urls')
else:
filename = request.query.get('filename', default='contents')
sources = [(filename, request['wsgi.input'])]
local.upload_manager.upload_to_bundle_store(
bundle,
sources=sources,
follow_symlinks=False,
exclude_patterns=None,
remove_sources=False,
git=query_get_bool('git', default=False),
unpack=query_get_bool('unpack', default=True),
simplify_archives=query_get_bool('simplify', default=True),
) # See UploadManager for full explanation of 'simplify'
bundle_location = local.bundle_store.get_bundle_location(uuid)
local.model.update_disk_metadata(bundle, bundle_location, enforce_disk_quota=True)
except UsageError as err:
# This is a user error (most likely disk quota overuser) so raise a client HTTP error
if local.upload_manager.has_contents(bundle):
local.upload_manager.cleanup_existing_contents(bundle)
msg = "Upload failed: %s" % err
local.model.update_bundle(
bundle, {'state': State.FAILED, 'metadata': {'failure_message': msg}}
)
abort(http.client.BAD_REQUEST, msg)
worksheet = local.model.get_worksheet(worksheet_uuid, fetch_items=False)
check_worksheet_has_all_permission(local.model, request.user, worksheet)
worksheet_util.check_worksheet_not_frozen(worksheet)
request.user.check_quota(need_time=True, need_disk=True)
created_uuids = []
for bundle in bundles:
# Prep bundle info for saving into database
# Unfortunately cannot use the `construct` methods because they don't
# provide a uniform interface for constructing bundles for all types
# Hopefully this can all be unified after REST migration is complete
bundle_uuid = bundle.setdefault('uuid', spec_util.generate_uuid())
created_uuids.append(bundle_uuid)
bundle_class = get_bundle_subclass(bundle['bundle_type'])
bundle['owner_id'] = request.user.user_id
if issubclass(bundle_class, UploadedBundle) or query_get_bool('wait_for_upload', False):
bundle['state'] = State.UPLOADING
else:
bundle['state'] = State.CREATED
bundle['is_anonymous'] = worksheet.is_anonymous # inherit worksheet anonymity
bundle.setdefault('metadata', {})['created'] = int(time.time())
for dep in bundle.setdefault('dependencies', []):
dep['child_uuid'] = bundle_uuid
# Create bundle object
bundle = bundle_class(bundle, strict=False)
# Save bundle into model
local.model.save_bundle(bundle)
# Inherit worksheet permissions
group_permissions = local.model.get_group_worksheet_permissions(
state should not change on failure. Default is 0.
- `finalize_on_success`: (optional) 1 if bundle state should be set
to 'state_on_success' when the upload finishes successfully. Default is
True
- `state_on_success`: (optional) Update the bundle state to this state if
the upload completes successfully. Must be either 'ready' or 'failed'.
Default is 'ready'.
"""
check_bundles_have_all_permission(local.model, request.user, [uuid])
bundle = local.model.get_bundle(uuid)
if bundle.state in State.FINAL_STATES:
abort(http.client.FORBIDDEN, 'Contents cannot be modified, bundle already finalized.')
# Get and validate query parameters
finalize_on_failure = query_get_bool('finalize_on_failure', default=False)
finalize_on_success = query_get_bool('finalize_on_success', default=True)
final_state = request.query.get('state_on_success', default=State.READY)
if finalize_on_success and final_state not in State.FINAL_STATES:
abort(
http.client.BAD_REQUEST,
'state_on_success must be one of %s' % '|'.join(State.FINAL_STATES),
)
# If this bundle already has data, remove it.
if local.upload_manager.has_contents(bundle):
local.upload_manager.cleanup_existing_contents(bundle)
# Store the data.
try:
if request.query.urls:
sources = query_get_list('urls')
else:
bundles_dict = get_bundle_infos(
bundle_uuids,
get_children='children' in include_set,
get_permissions='group_permissions' in include_set,
get_host_worksheets='host_worksheets' in include_set,
ignore_not_found=False,
)
# Create list of bundles in original order
bundles = [bundles_dict[uuid] for uuid in bundle_uuids]
# Build response document
document = BundleSchema(many=True).dump(bundles).data
# Shim in display metadata used by the front-end application
if query_get_bool('include_display_metadata', default=False):
for bundle, data in izip(bundles, document['data']):
bundle_class = get_bundle_subclass(bundle['bundle_type'])
json_api_meta(
data,
{
'editable_metadata_keys': worksheet_util.get_editable_metadata_fields(
bundle_class
),
'metadata_type': worksheet_util.get_metadata_types(bundle_class),
},
)
if 'owner' in include_set:
owner_ids = set(b['owner_id'] for b in bundles if b['owner_id'] is not None)
json_api_include(document, UserSchema(), local.model.get_users(owner_ids))
to 'failed' in the case of a failure during upload, or 0 if the bundle
state should not change on failure. Default is 0.
- `finalize_on_success`: (optional) 1 if bundle state should be set
to 'state_on_success' when the upload finishes successfully. Default is
True
- `state_on_success`: (optional) Update the bundle state to this state if
the upload completes successfully. Must be either 'ready' or 'failed'.
Default is 'ready'.
"""
check_bundles_have_all_permission(local.model, request.user, [uuid])
bundle = local.model.get_bundle(uuid)
if bundle.state in State.FINAL_STATES:
abort(http.client.FORBIDDEN, 'Contents cannot be modified, bundle already finalized.')
# Get and validate query parameters
finalize_on_failure = query_get_bool('finalize_on_failure', default=False)
finalize_on_success = query_get_bool('finalize_on_success', default=True)
final_state = request.query.get('state_on_success', default=State.READY)
if finalize_on_success and final_state not in State.FINAL_STATES:
abort(
http.client.BAD_REQUEST,
'state_on_success must be one of %s' % '|'.join(State.FINAL_STATES),
)
# If this bundle already has data, remove it.
if local.upload_manager.has_contents(bundle):
local.upload_manager.cleanup_existing_contents(bundle)
# Store the data.
try:
if request.query.urls:
sources = query_get_list('urls')
def wrapper(*args, **kwargs):
try:
result = callback(*args, **kwargs)
# If response is JSON, add server version to meta
if isinstance(result, dict):
json_api_meta(result, {'version': CODALAB_VERSION})
return result
except ValidationError as err:
format_errors = query_get_bool('format_errors', default=False)
if format_errors:
msg = err.messages
else:
msg = '\n'.join([e['detail'] for e in err.messages['errors']])
abort(httplib.BAD_REQUEST, msg)