from flask import (
    Blueprint,
    render_template,
    redirect,
    flash,
    url_for,
    current_app,
    request
)

from flask.ext.security import login_required
from flask.ext.login import current_user

from application.profile.forms import EmailForm, UpdateDetailsForm

profile = Blueprint('profile', __name__, template_folder='templates')


@profile.route('/profile')
@login_required
def view_profile():
    return render_template('profile/profile.html')


@profile.route('/profile/add-email', methods=['GET', 'POST'])
@login_required
def add_email():
    form = EmailForm()
    user = current_user
    if form.validate_on_submit():
        email = form.email.data.strip()
        if email not in user.other_email and email != user.email:
            user.other_email.append(email)
            user.save()
            message = "Sucessfully added email %s" % email
        else:
            message = "Already have email: %s" % email
        flash(message)
        return redirect(url_for('profile.view_profile'))
    else:
        return render_template('profile/add-email.html', form=form)


@profile.route('/profile/remove-email')
@login_required
def remove_email():
    email = request.args.get('email')
    user = current_user
    if email:
        email = email.strip()
        user.other_email.remove(email)
        user.save()
        message = "Removed email: %s" % email
        flash(message)
    return redirect(url_for('profile.view_profile'))

@profile.route('/profile/update-details')
@login_required
def update_details():
  form = UpdateDetailsForm()
  return render_template('profile/update-details.html', form=form)

@@ -1504,16 +1504,41 @@
 details'
+, methods=%5B'GET', 'POST'%5D
 )%0A@login
@@ -1598,16 +1598,501 @@
 sForm()%0A
+  user = current_user%0A  if form.validate_on_submit():%0A      if form.grade.data:%0A          user.grade = form.grade.data.strip()%0A      if form.profession.data:%0A          user.profession = form.profession.data.strip()%0A      if form.grade.data or form.profession.data:%0A          user.save()%0A          message = %22Successfully updated details%22%0A      else:%0A          message = %22No new details submitted%22%0A      flash(message)%0A      return redirect(url_for('profile.view_profile'))%0A  else:%0A    
   return

update details form works