"""Image Model. Model representing image upload status and data. """ from flask import g import requests from images import config from images import response from images.constants import errors from images.constants import workstation as workstation_constants def prepare_for_bulk_upload(filename, php_session_cookie_value): """Make a post request to the multiple releases endpoint in Workstation. Makes a request to Workstation to prepare for uploading files through the bulkuploader. Workstation will add the asset to the ingestion queue. The returned data is intended to be returned for use in the request to the bulkuploader by client APIs. Args: filename (string): Filename of the file to be passed to the bulkuploader. php_session_cookie_value (string): The php session cookie value to be included in the request. Returns: Response: A response object containing the returned data from Workstation. """ workstation_request = requests.post( config.WORKSTATION_URL + workstation_constants.GETDESTINATIONLIST_PATH, params=format_bulk_upload_url_param(filename), cookies={ workstation_constants.PHP_SESSION_COOKIE_KEY: php_session_cookie_value}) workstation_validation_response = validate_workstation_response( workstation_request, filename) if not workstation_validation_response: return workstation_validation_response workstation_image_data = workstation_request.json().get(filename) or {} workstation_response_data = { 'upload_filename': workstation_image_data.get('upload_filename'), 'import_asset_id': workstation_image_data.get('import_asset_id'), 'vendor_id': workstation_image_data.get('vendor_id'), 'token': workstation_image_data.get('token'), 'id': filename, 'original_filename': filename, 'asset_code': workstation_image_data.get('audio_asset_code')[0]} return response.Response(message=workstation_response_data) def log_and_create_fatal_response(message): """Log and create fatal response. Logs error message and returns fatal responses with message. """ g.ows.log.error(message) return response.create_fatal_response(message=message) def validate_workstation_response(workstation_response, filename): """Validate that Workstation responds with bulk_upload data. In cases where we do not recieve a 200, have a meta_not_found attribute, or an unexpected non JSON response from Workstation, this function will return fatal response object. Args: workstation_response (Response): A response object from the Requests library. Represents the Workstation response. filename (string): Filename of the file to be passed to the bulkuploader. Returns: Response: A successful response object or a fatal response with error message on validation failure. """ if workstation_response.status_code != 200: message = '{0} {1}'.format( errors.UNEXPECTED_STATUS_CODE_MESSAGE, workstation_response.status_code) return log_and_create_fatal_response(message) try: workstation_json_response = workstation_response.json() workstation_image_data = workstation_json_response.get(filename) or {} except: message = errors.UNEXPECTED_RESPONSE_MESSAGE return log_and_create_fatal_response(message) upload_filename = workstation_image_data.get('upload_filename') if upload_filename == workstation_constants.META_NOT_FOUND_VALUE: message = errors.META_NOT_FOUND_MESSAGE return log_and_create_fatal_response(message) return response.Response() def format_bulk_upload_url_param(filename): """Format url params for Workstation getdestinationlist endpoint. Arguments: filename (string): Filename of the file to be passed to the bulkuploader. Returns: Dict: The param and value to be used by the requests library for use as a URL query param along with the release id flag. """ upload_string = '[{{"filename":"{0}","folder":"","id":"{0}"}}]'.format( filename) return { 'bulk_upload': upload_string, 'use_release_id': '1'}