|
1 |
| -from neam.python.app import app |
| 1 | +import os |
| 2 | + |
| 3 | +from flask import render_template, request, send_from_directory, jsonify, url_for |
| 4 | +from neam.python.app import app, celery, neam_annotate |
| 5 | + |
2 | 6 |
|
3 | 7 | @app.route('/')
|
4 | 8 | @app.route('/index')
|
5 | 9 | def index():
|
6 |
| - return 'Hello, World!' |
| 10 | + """ Routes the user to the index page """ |
| 11 | + return render_template('index.html', title='NEAM Annotate') |
| 12 | + |
| 13 | + |
| 14 | +@app.route('/annotate', methods=['POST']) |
| 15 | +def annotate(): |
| 16 | + """ |
| 17 | + Annotates a document |
| 18 | +
|
| 19 | + TODO: Add validation |
| 20 | +
|
| 21 | + :return: An HTTP response, where the Location key corresponds to the URI to check on |
| 22 | + the annotation process |
| 23 | + """ |
| 24 | + # Grab the data from the request |
| 25 | + email = request.form['email'] |
| 26 | + f = request.files['file'] |
| 27 | + |
| 28 | + # Save the file so the worker can find it |
| 29 | + f.save(os.path.join(app.config['UPLOAD_FOLDER'], f.filename)) |
| 30 | + |
| 31 | + # Fire off a worker to annotate the file |
| 32 | + t = neam_annotate.delay(f.filename, email) |
| 33 | + |
| 34 | + return jsonify({}), 202, {'Location': url_for('taskstatus', task_id=t.id)} |
| 35 | + |
| 36 | + |
| 37 | +@app.route('/status/<task_id>') |
| 38 | +def taskstatus(task_id): |
| 39 | + """ |
| 40 | + Checks on the status of a worker |
| 41 | +
|
| 42 | + :param task_id: The ID of the worker |
| 43 | + :type task_id: str |
| 44 | + :return: The status of the worker |
| 45 | + """ |
| 46 | + # Find the task |
| 47 | + # TODO: handle invalid queries |
| 48 | + task = my_task.AsyncResult(task_id) |
| 49 | + |
| 50 | + # Set the values on a response object |
| 51 | + response = { 'state': task.state } |
| 52 | + if task.state == 'PENDING': |
| 53 | + response['status'] = 'Pending' |
| 54 | + elif task.state != 'FAILURE': |
| 55 | + response['status'] = task.info.get('status', '') |
| 56 | + if 'result' in task.info: |
| 57 | + response['result'] = task.info['result'] |
| 58 | + else: |
| 59 | + response['status'] = str(task.info) |
| 60 | + |
| 61 | + return jsonify(response) |
| 62 | + |
| 63 | + |
| 64 | +@app.route('/download/<filename>') |
| 65 | +def download(filename): |
| 66 | + """ |
| 67 | + Downloads a file from the server |
| 68 | +
|
| 69 | + TODO: validate the input |
| 70 | +
|
| 71 | + :param filename: The name of the file to download |
| 72 | + :return: The requested file |
| 73 | + """ |
| 74 | + return send_from_directory(os.path.join(app.config['UPLOAD_FOLDER']), filename, as_attachment=True) |
7 | 75 |
|
0 commit comments