def user_edit(user_id):
    t = env.get_template('home.html')
    user = get_user(app, User())

    parsed = parse_qs(app.current_request.raw_body.decode())
    username = parsed.get('username')[0]

    try:
        client = boto3.client('cognito-idp')
        response = client.admin_update_user_attributes(
            UserPoolId=conf['COGNITO_POOL_ID'],
            Username=user.id,
            UserAttributes=[
                {
                    'Name': 'name',
                    'Value': username
                },
            ])
    except Exception as e:
        return Response(body={'update': 'failed'},
                        status_code=200,
                        headers={"Content-Type": "application/json"})

    return Response(body={'update': 'success'},
                    status_code=200,
                    headers={"Content-Type": "application/json"})
def photo_delete(photo_id):
    """
    Delete specific file (with thumbnail) and delete DB record.
    :param photo_id: target photo id
    :return: Json data remove:fail or success
    """
    user = get_user(app, User())

    app.log.debug("Photo delete request: %s", photo_id)
    try:
        photo = Photo.get(user.id, int(photo_id))
        photo.delete()

        util.delete_s3(app, photo.filename, user)

        # flash('Successfully removed!')
        app.log.debug('Successfully removed!')

    except Exception as e:
        app.log.error(e)
        # flash('Error occurred! Please try again.')
        return Response(body={'remove': 'fail'},
                        status_code=200,
                        headers={"Content-Type": "application/json"})

    return Response(body={'remove': 'success'},
                    status_code=200,
                    headers={"Content-Type": "application/json"})
def upload():
    # flash('Your file upload have been completed successfully!')

    user = get_user(app, User())
    form = util.get_parts(app)

    taken_date = datetime.strptime(form['taken_date'][0].decode('utf-8'),
                                   "%Y:%m:%d %H:%M:%S")
    photo = Photo(user.id, util.current_milli_time())
    photo.tags = form['tags'][0].decode('utf-8')
    photo.desc = form['desc'][0].decode('utf-8')
    photo.filename_orig = form['filename_origin'][0].decode('utf-8')
    photo.filename = form['filename_s3_key'][0].decode('utf-8')
    photo.filesize = int(form['file_size'][0].decode('utf-8'))
    photo.geotag_lat = form['lat'][0].decode('utf-8')
    photo.geotag_lng = form['lng'][0].decode('utf-8')
    photo.upload_date = util.the_time_now()
    photo.taken_date = taken_date
    photo.make = form['make'][0].decode('utf-8')
    photo.model = form['model'][0].decode('utf-8')
    photo.width = form['width'][0].decode('utf-8')
    photo.height = form['height'][0].decode('utf-8')
    photo.city = form['city'][0].decode('utf-8')
    photo.nation = form['nation'][0].decode('utf-8')
    photo.address = form['address'][0].decode('utf-8')
    photo.save()

    return Response(status_code=301,
                    headers={'Location': '/api/home'},
                    body='')
def upload_form():
    user = get_user(app, User())
    t = env.get_template('upload.html')
    body = t.render(current_user=user,
                    gmaps_key=conf['GMAPS_KEY'],
                    s3_static_url=S3_STATIC_URL)
    response = Response(body=body,
                        status_code=200,
                        headers={"Content-Type": "text/html"})
    return response
def user_edit_form(user_id):
    t = env.get_template('signup.html')
    user = get_user(app, User())

    body = t.render(user=user,
                    password_reset=util.get_password_reset_url(),
                    s3_static_url=S3_STATIC_URL)

    return Response(body=body,
                    status_code=200,
                    headers={"Content-Type": "text/html"})
def home():
    t = env.get_template('home.html')
    user = get_user(app, User())

    result = Photo.query(user.id)
    body = t.render(current_user=user,
                    photos=result,
                    presigned_url=util.presigned_url,
                    s3_static_url=S3_STATIC_URL)

    return Response(body=body,
                    status_code=200,
                    headers={"Content-Type": "text/html"})
def map_view():
    t = env.get_template('map.html')
    user = get_user(app, User())

    photo_list = Photo.query(user.id)
    body = t.render(current_user=user,
                    photos=photo_list,
                    gmaps_key=conf['GMAPS_KEY'],
                    presigned_url=util.presigned_url,
                    s3_static_url=S3_STATIC_URL)

    return Response(body=body,
                    status_code=200,
                    headers={"Content-Type": "text/html"})
def upload_to_s3(uuid, file_name):
    s3_client = boto3.client('s3')

    ext = (file_name.rsplit('.', 1)[1]).lower()
    filename = "{0}.{1}".format(uuid, ext)
    user = get_user(app, User())

    body = app.current_request.raw_body

    try:
        size = util.save_s3_chalice(body, filename, user.email, app)

        return Response(body='upload successful',
                        headers={'Content-Type': 'text/plain'},
                        status_code=200)

    except Exception as e:
        app.log.error('error occurred during upload %s' % e)
        return Response(body='upload failed',
                        headers={'Content-Type': 'text/plain'},
                        status_code=400)
def search():
    """
    Search function for description and tags in Photo table.
    :return: List of photo object which retrieved DB.
    """
    t = env.get_template('home.html')
    user = get_user(app, User())

    parsed = parse_qs(app.current_request.raw_body.decode())
    keyword = parsed.get('search')[0]

    photo_pages = Photo.query(
        user.id,
        Photo.tags.contains(keyword) | Photo.desc.contains(keyword))

    body = t.render(current_user=user,
                    photos=photo_pages,
                    presigned_url=util.presigned_url,
                    msg="Search results for '{0}'.. ".format(keyword),
                    s3_static_url=S3_STATIC_URL)

    return Response(body=body,
                    status_code=200,
                    headers={"Content-Type": "text/html"})