prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Adding user image to profile and saving it results in error page
  After user uploads an image to the profile page and updates/saves the profile an error page shows up.
  
  ![screenshot from 2016-12-31 15-36-41](https://cloud.githubusercontent.com/assets/1583873/21577975/160aba80-cf6f-11e6-999a-d42a21e9561b.png)
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `app/views/users/profile.py`
  Content:
  ```
  1 from uuid import uuid4
  2 
  3 from flask import Blueprint
  4 from flask import render_template
  5 from flask import request, url_for, redirect, flash, jsonify
  6 from flask.ext import login
  7 from markupsafe import Markup
  8 
  9 from app.helpers.auth import AuthManager
  10 from app.helpers.data import DataManager, get_facebook_auth, get_instagram_auth, get_twitter_auth_url, save_to_db, get_google_auth
  11 from app.helpers.data_getter import DataGetter
  12 from app.helpers.helpers import uploaded_file
  13 from app.helpers.oauth import FbOAuth, InstagramOAuth, OAuth
  14 from app.helpers.storage import upload, UPLOAD_PATHS
  15 
  16 profile = Blueprint('profile', __name__, url_prefix='/profile')
  17 
  18 
  19 @profile.route('/')
  20 def index_view():
  21     if not AuthManager.is_verified_user():
  22         flash(Markup("Your account is unverified. "
  23                      "Please verify by clicking on the confirmation link that has been emailed to you."
  24                      '<br>Did not get the email? Please <a href="/resend_email/" class="alert-link"> '
  25                      'click here to resend the confirmation.</a>'))
  26     profile = DataGetter.get_user(login.current_user.id)
  27     return render_template('gentelella/admin/profile/index.html',
  28                            profile=profile)
  29 
  30 
  31 @profile.route('/edit/', methods=('GET', 'POST'))
  32 @profile.route('/edit/<user_id>', methods=('GET', 'POST'))
  33 def edit_view(user_id=None):
  34     admin = None
  35     if not user_id:
  36         user_id = login.current_user.id
  37     else:
  38         admin = True
  39     if request.method == 'POST':
  40         DataManager.update_user(request.form, int(user_id))
  41         if admin:
  42             return redirect(url_for('sadmin_users.details_view', user_id=user_id))
  43         return redirect(url_for('.index_view'))
  44     return redirect(url_for('.index_view'))
  45 
  46 
  47 @profile.route('/fb_connect', methods=('GET', 'POST'))
  48 def connect_facebook():
  49     facebook = get_facebook_auth()
  50     fb_auth_url, state = facebook.authorization_url(FbOAuth.get_auth_uri(), access_type='offline')
  51     return redirect(fb_auth_url)
  52 
  53 
  54 @profile.route('/tw_connect', methods=('GET', 'POST'))
  55 def connect_twitter():
  56     twitter_auth_url, __ = get_twitter_auth_url()
  57     return redirect('https://api.twitter.com/oauth/authenticate?' + twitter_auth_url)
  58 
  59 @profile.route('/instagram_connect', methods=('GET', 'POST'))
  60 def connect_instagram():
  61     instagram = get_instagram_auth()
  62     instagram_auth_url, state = instagram.authorization_url(InstagramOAuth.get_auth_uri(), access_type='offline')
  63     return redirect(instagram_auth_url)
  64 
  65 @profile.route('/<int:user_id>/editfiles/bgimage', methods=('POST', 'DELETE'))
  66 def bgimage_upload(user_id):
  67     if request.method == 'POST':
  68         background_image = request.form['bgimage']
  69         if background_image:
  70             background_file = uploaded_file(file_content=background_image)
  71             background_url = upload(
  72                 background_file,
  73                 UPLOAD_PATHS['user']['avatar'].format(
  74                     user_id=user_id
  75                 ))
  76             return jsonify({'status': 'ok', 'background_url': background_url})
  77         else:
  78             return jsonify({'status': 'no bgimage'})
  79     elif request.method == 'DELETE':
  80         profile = DataGetter.get_user(int(user_id))
  81         profile.avatar_uploaded = ''
  82         save_to_db(profile)
  83         return jsonify({'status': 'ok'})
  84 
  85 
  86 @profile.route('/create/files/bgimage', methods=('POST',))
  87 def create_event_bgimage_upload():
  88     if request.method == 'POST':
  89         background_image = request.form['bgimage']
  90         if background_image:
  91             background_file = uploaded_file(file_content=background_image)
  92             background_url = upload(
  93                 background_file,
  94                 UPLOAD_PATHS['temp']['event'].format(uuid=uuid4())
  95             )
  96             return jsonify({'status': 'ok', 'background_url': background_url})
  97         else:
  98             return jsonify({'status': 'no bgimage'})
  99
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/app/views/users/profile.py b/app/views/users/profile.py
  --- a/app/views/users/profile.py
  +++ b/app/views/users/profile.py
  @@ -11,7 +11,7 @@
   from app.helpers.data_getter import DataGetter
   from app.helpers.helpers import uploaded_file
   from app.helpers.oauth import FbOAuth, InstagramOAuth, OAuth
  -from app.helpers.storage import upload, UPLOAD_PATHS
  +from app.helpers.storage import upload, UPLOAD_PATHS, upload_local
   
   profile = Blueprint('profile', __name__, url_prefix='/profile')
   
  @@ -89,7 +89,7 @@
           background_image = request.form['bgimage']
           if background_image:
               background_file = uploaded_file(file_content=background_image)
  -            background_url = upload(
  +            background_url = upload_local(
                   background_file,
                   UPLOAD_PATHS['temp']['event'].format(uuid=uuid4())
               )
  
verification_info: {"golden_diff": "diff --git a/app/views/users/profile.py b/app/views/users/profile.py\n--- a/app/views/users/profile.py\n+++ b/app/views/users/profile.py\n@@ -11,7 +11,7 @@\n from app.helpers.data_getter import DataGetter\n from app.helpers.helpers import uploaded_file\n from app.helpers.oauth import FbOAuth, InstagramOAuth, OAuth\n-from app.helpers.storage import upload, UPLOAD_PATHS\n+from app.helpers.storage import upload, UPLOAD_PATHS, upload_local\n \n profile = Blueprint('profile', __name__, url_prefix='/profile')\n \n@@ -89,7 +89,7 @@\n         background_image = request.form['bgimage']\n         if background_image:\n             background_file = uploaded_file(file_content=background_image)\n-            background_url = upload(\n+            background_url = upload_local(\n                 background_file,\n                 UPLOAD_PATHS['temp']['event'].format(uuid=uuid4())\n             )\n", "issue": "Adding user image to profile and saving it results in error page\nAfter user uploads an image to the profile page and updates/saves the profile an error page shows up.\r\n\r\n![screenshot from 2016-12-31 15-36-41](https://cloud.githubusercontent.com/assets/1583873/21577975/160aba80-cf6f-11e6-999a-d42a21e9561b.png)\r\n\n", "before_files": [{"content": "from uuid import uuid4\n\nfrom flask import Blueprint\nfrom flask import render_template\nfrom flask import request, url_for, redirect, flash, jsonify\nfrom flask.ext import login\nfrom markupsafe import Markup\n\nfrom app.helpers.auth import AuthManager\nfrom app.helpers.data import DataManager, get_facebook_auth, get_instagram_auth, get_twitter_auth_url, save_to_db, get_google_auth\nfrom app.helpers.data_getter import DataGetter\nfrom app.helpers.helpers import uploaded_file\nfrom app.helpers.oauth import FbOAuth, InstagramOAuth, OAuth\nfrom app.helpers.storage import upload, UPLOAD_PATHS\n\nprofile = Blueprint('profile', __name__, url_prefix='/profile')\n\n\n@profile.route('/')\ndef index_view():\n    if not AuthManager.is_verified_user():\n        flash(Markup(\"Your account is unverified. \"\n                     \"Please verify by clicking on the confirmation link that has been emailed to you.\"\n                     '<br>Did not get the email? Please <a href=\"/resend_email/\" class=\"alert-link\"> '\n                     'click here to resend the confirmation.</a>'))\n    profile = DataGetter.get_user(login.current_user.id)\n    return render_template('gentelella/admin/profile/index.html',\n                           profile=profile)\n\n\n@profile.route('/edit/', methods=('GET', 'POST'))\n@profile.route('/edit/<user_id>', methods=('GET', 'POST'))\ndef edit_view(user_id=None):\n    admin = None\n    if not user_id:\n        user_id = login.current_user.id\n    else:\n        admin = True\n    if request.method == 'POST':\n        DataManager.update_user(request.form, int(user_id))\n        if admin:\n            return redirect(url_for('sadmin_users.details_view', user_id=user_id))\n        return redirect(url_for('.index_view'))\n    return redirect(url_for('.index_view'))\n\n\n@profile.route('/fb_connect', methods=('GET', 'POST'))\ndef connect_facebook():\n    facebook = get_facebook_auth()\n    fb_auth_url, state = facebook.authorization_url(FbOAuth.get_auth_uri(), access_type='offline')\n    return redirect(fb_auth_url)\n\n\n@profile.route('/tw_connect', methods=('GET', 'POST'))\ndef connect_twitter():\n    twitter_auth_url, __ = get_twitter_auth_url()\n    return redirect('https://api.twitter.com/oauth/authenticate?' + twitter_auth_url)\n\n@profile.route('/instagram_connect', methods=('GET', 'POST'))\ndef connect_instagram():\n    instagram = get_instagram_auth()\n    instagram_auth_url, state = instagram.authorization_url(InstagramOAuth.get_auth_uri(), access_type='offline')\n    return redirect(instagram_auth_url)\n\n@profile.route('/<int:user_id>/editfiles/bgimage', methods=('POST', 'DELETE'))\ndef bgimage_upload(user_id):\n    if request.method == 'POST':\n        background_image = request.form['bgimage']\n        if background_image:\n            background_file = uploaded_file(file_content=background_image)\n            background_url = upload(\n                background_file,\n                UPLOAD_PATHS['user']['avatar'].format(\n                    user_id=user_id\n                ))\n            return jsonify({'status': 'ok', 'background_url': background_url})\n        else:\n            return jsonify({'status': 'no bgimage'})\n    elif request.method == 'DELETE':\n        profile = DataGetter.get_user(int(user_id))\n        profile.avatar_uploaded = ''\n        save_to_db(profile)\n        return jsonify({'status': 'ok'})\n\n\n@profile.route('/create/files/bgimage', methods=('POST',))\ndef create_event_bgimage_upload():\n    if request.method == 'POST':\n        background_image = request.form['bgimage']\n        if background_image:\n            background_file = uploaded_file(file_content=background_image)\n            background_url = upload(\n                background_file,\n                UPLOAD_PATHS['temp']['event'].format(uuid=uuid4())\n            )\n            return jsonify({'status': 'ok', 'background_url': background_url})\n        else:\n            return jsonify({'status': 'no bgimage'})\n", "path": "app/views/users/profile.py"}], "after_files": [{"content": "from uuid import uuid4\n\nfrom flask import Blueprint\nfrom flask import render_template\nfrom flask import request, url_for, redirect, flash, jsonify\nfrom flask.ext import login\nfrom markupsafe import Markup\n\nfrom app.helpers.auth import AuthManager\nfrom app.helpers.data import DataManager, get_facebook_auth, get_instagram_auth, get_twitter_auth_url, save_to_db, get_google_auth\nfrom app.helpers.data_getter import DataGetter\nfrom app.helpers.helpers import uploaded_file\nfrom app.helpers.oauth import FbOAuth, InstagramOAuth, OAuth\nfrom app.helpers.storage import upload, UPLOAD_PATHS, upload_local\n\nprofile = Blueprint('profile', __name__, url_prefix='/profile')\n\n\n@profile.route('/')\ndef index_view():\n    if not AuthManager.is_verified_user():\n        flash(Markup(\"Your account is unverified. \"\n                     \"Please verify by clicking on the confirmation link that has been emailed to you.\"\n                     '<br>Did not get the email? Please <a href=\"/resend_email/\" class=\"alert-link\"> '\n                     'click here to resend the confirmation.</a>'))\n    profile = DataGetter.get_user(login.current_user.id)\n    return render_template('gentelella/admin/profile/index.html',\n                           profile=profile)\n\n\n@profile.route('/edit/', methods=('GET', 'POST'))\n@profile.route('/edit/<user_id>', methods=('GET', 'POST'))\ndef edit_view(user_id=None):\n    admin = None\n    if not user_id:\n        user_id = login.current_user.id\n    else:\n        admin = True\n    if request.method == 'POST':\n        DataManager.update_user(request.form, int(user_id))\n        if admin:\n            return redirect(url_for('sadmin_users.details_view', user_id=user_id))\n        return redirect(url_for('.index_view'))\n    return redirect(url_for('.index_view'))\n\n\n@profile.route('/fb_connect', methods=('GET', 'POST'))\ndef connect_facebook():\n    facebook = get_facebook_auth()\n    fb_auth_url, state = facebook.authorization_url(FbOAuth.get_auth_uri(), access_type='offline')\n    return redirect(fb_auth_url)\n\n\n@profile.route('/tw_connect', methods=('GET', 'POST'))\ndef connect_twitter():\n    twitter_auth_url, __ = get_twitter_auth_url()\n    return redirect('https://api.twitter.com/oauth/authenticate?' + twitter_auth_url)\n\n@profile.route('/instagram_connect', methods=('GET', 'POST'))\ndef connect_instagram():\n    instagram = get_instagram_auth()\n    instagram_auth_url, state = instagram.authorization_url(InstagramOAuth.get_auth_uri(), access_type='offline')\n    return redirect(instagram_auth_url)\n\n@profile.route('/<int:user_id>/editfiles/bgimage', methods=('POST', 'DELETE'))\ndef bgimage_upload(user_id):\n    if request.method == 'POST':\n        background_image = request.form['bgimage']\n        if background_image:\n            background_file = uploaded_file(file_content=background_image)\n            background_url = upload(\n                background_file,\n                UPLOAD_PATHS['user']['avatar'].format(\n                    user_id=user_id\n                ))\n            return jsonify({'status': 'ok', 'background_url': background_url})\n        else:\n            return jsonify({'status': 'no bgimage'})\n    elif request.method == 'DELETE':\n        profile = DataGetter.get_user(int(user_id))\n        profile.avatar_uploaded = ''\n        save_to_db(profile)\n        return jsonify({'status': 'ok'})\n\n\n@profile.route('/create/files/bgimage', methods=('POST',))\ndef create_event_bgimage_upload():\n    if request.method == 'POST':\n        background_image = request.form['bgimage']\n        if background_image:\n            background_file = uploaded_file(file_content=background_image)\n            background_url = upload_local(\n                background_file,\n                UPLOAD_PATHS['temp']['event'].format(uuid=uuid4())\n            )\n            return jsonify({'status': 'ok', 'background_url': background_url})\n        else:\n            return jsonify({'status': 'no bgimage'})\n", "path": "app/views/users/profile.py"}]}