from flask import Blueprint, request, current_app, jsonify
from flask_restless.helpers import to_dict
from werkzeug.utils import secure_filename
from werkzeug.exceptions import BadRequest
from gfsad.utils.s3 import upload_image
import uuid
import cStringIO
from gfsad.models.location import Image, db

upload = Blueprint('upload', __name__, url_prefix='/upload')


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in current_app.config['ALLOWED_IMG_EXTENSIONS']


@upload.route('/image', methods=['POST'])
def image_view():
    """
    This view allows users to upload photos of locations from their mobile application.
    :return: json response
    """
    # get the accompanying data
    data = request.form

    for field in ['location_id', 'lat', 'lon', 'date_acquired']:
        if field not in data:
            raise BadRequest(description='Image requires %s.' % field)

    if 'file' in request.files and request.files['file'] is not None:
        # get the file from the request object
        f = request.files['file']

        # sanitize the file name
        filename = secure_filename(f.filename)

        # check that file type is allowed NAIVE check
        if not allowed_file(filename):
            raise BadRequest('Bad File Type')

        # get file for processing and uploading
        f_io = cStringIO.StringIO()
        f.save(dst=f_io)

        # create key for file
        url = 'images/mobile/' + str(uuid.uuid4()) + '.jpg'

        # upload image to s3 bucket
        upload_image(f_io, encoded_image=False, filename=url)
    elif 'url' in data:
        url = data['url']
    else:
        raise BadRequest(description='Not enough data')

    # save to database
    image = Image(location_id=data['location_id'], lat=data['lat'], lon=data['lon'],
                  url=url,
                  date_acquired=data['date_acquired'])
    db.session.add(image)
    db.session.commit()
    return jsonify(to_dict(image)), 201
@@ -701,24 +701,52 @@
 onse%0A    %22%22%22
+%0A%0A    print request.__dict__
 %0A    # get t
@@ -878,32 +878,71 @@
 ld not in data:%0A
+            print %22missing %25s%22 %25 field%0A
             rais
@@ -1319,24 +1319,58 @@
 (filename):%0A
+            print %22bad file type%22%0A
             

increase debugging