import os.path

from flask import request, jsonify, redirect, url_for

from IATISimpleTester import app, db
from IATISimpleTester.models import SuppliedData


@app.route('/upload', methods=['GET', 'POST'])
def upload():
    resp = {}
    source_url = request.args.get('source_url')
    file = request.files.get('file')
    raw_text = request.args.get('paste')
    form_name = None

    if source_url:
        form_name = 'url_form'
    elif raw_text:
        form_name = 'text_form'
    elif file:
        form_name = 'upload_form'

    if form_name:
        data = SuppliedData(source_url, file, raw_text, form_name)
        db.session.add(data)
        db.session.commit()
        resp['success'] = True
        resp['data'] = {
            'id': data.id,
            'original_file': data.original_file,
        }

    if request.args.get('output') == 'json':
        return jsonify(resp)

    return redirect(url_for('explore', uuid=data.id))

@@ -26,16 +26,23 @@
 k import
+ abort,
  request
@@ -70,16 +70,16 @@
 url_for%0A
-
 %0Afrom IA
@@ -224,22 +224,8 @@
 ():%0A
-    resp = %7B%7D%0A
     
@@ -526,16 +526,20 @@
 %0A    if 
+not 
 form_nam
@@ -541,24 +541,47 @@
 m_name:%0A    
+    return abort(404)%0A%0A
     data = S
@@ -631,20 +631,16 @@
 m_name)%0A
-    
     db.s
@@ -660,20 +660,16 @@
 ta)%0A    
-  
-  
 db.sessi
@@ -679,16 +679,80 @@
 commit()
+%0A%0A    if request.args.get('output') == 'json':%0A        resp = %7B%7D
 %0A       
@@ -876,16 +876,16 @@
 l_file,%0A
+
         
@@ -889,54 +889,8 @@
    %7D
-%0A%0A    if request.args.get('output') == 'json':
 %0A   

Tidy up upload response a bit