import json from flask import abort from flask import jsonify from flask import request from bulkperformancerights.app import application from bulkperformancerights.logic import async from bulkperformancerights.logic import fileupload from bulkperformancerights.logic import export_status from bulkperformancerights.logic import job from bulkperformancerights.logic import user @application.route('/upload//', methods=['POST']) def file_post(user_type, user_id): """Post a file to the service Args: user_type (str): 'vendor' or 'subaccount', see logic/user.py user_id (int): indentifier of user for which to get job status email (str): user's email to be used for notification Return: str: json representation of created job Todo(OrCharles): Move as much logic back to the logic layer as feasible Complete http://jira.theorchard.com/browse/LNC-677 """ if not request.headers.get('WORKSTATION-TRUSTED'): return json.dumps({'error': 'Missing WORKSTATION-TRUSTED header'}), 400 file = request.files.get('file') if not file: abort('You must provide a file') if not fileupload.allowed_file(file.filename): return json.dumps({'error': 'File must be of type .xlsx'}), 400 user_dict = {'type': user_type, 'id': user_id} user_s3_path = user.prefix_for_user(user_dict) xlsx_user_s3_path = user.xlsx_to_json_prefix_for_user(user_dict) s3_put_result = fileupload.put_to_s3(file, user_s3_path) # hold onto s3_put_result, we'll pass this to the xlsx-json svc if file: assert s3_put_result, 'upload to s3 failed' email = request.form['email'] if not email: abort('You must provide an email') created_job = job.make_job(file.filename, user_type, user_id) async.convert_validate_update.delay(created_job, user_s3_path, xlsx_user_s3_path, email) response = jsonify(job.decorate_job(created_job.dict())) response.status_code = 200 return response @application.route('/status//', methods=['GET']) @application.route('/status///', methods=['GET']) def get_job_status(user_type, user_id, job_id=None): """Retrieve status of bulkperformancerights job(s) Args: user_type (str): 'vendor' or 'subaccount', see logic/user.py user_id (int): indentifier of user for which to get job status job_id (int): optional arg, targeting a job_id will return info for that job, else a list will be returned Return: str: json representing either single job or a list of jobs """ return json.dumps(job.get_job_status(user_type, user_id, job_id)), 200 @application.route('/export//', methods=['POST']) def export_tracks(user_type, user_id): """Request performance rights track export for user Args: user_type (str): 'vendor' or 'subaccount', see logic/user.py user_id (int): indentifier of user for which to queue export job Return: str: json representation of created export job Todo(OrCharles): MR-24: accept email address to notify user of completion """ if not request.headers.get('WORKSTATION-TRUSTED'): return json.dumps({'error': 'Missing WORKSTATION-TRUSTED header'}), 400 email = request.form.get('email') export_state = export_status.create(user_type, user_id) async.export_tracks.delay(export_state, email) response = jsonify(export_state.dict()) response.status_code = 200 return response @application.route('/export//', methods=['GET']) @application.route('/export///', methods=['GET']) def export_jobs_list(user_type, user_id, job_id=None): """Retrieve export job statuses Args: user_type (str): 'vendor' or 'subaccount', see logic/user.py user_id (int): id of user for which to get export job statuses job_id (int): optional arg, targeting a job_id will return info for that job, else a list will be returned Return: str: json representing a list of job statuses """ return json.dumps(export_status.get(user_type, user_id, job_id)), 200 @application.route('/hello', methods=['GET']) def hello(): response = jsonify({"hello": "hi"}) response.status_code = 200 return response