def edit_profile():
    """Update profile info."""
    if g.user is None:
        abort(401)
    form = dict(name=g.user.name, email=g.user.email)
    if request.method == 'POST':
        if 'delete' in request.form:
            db_session.delete(g.user)
            db_session.commit()
            session[session['token_key']] = None
            flash(u'Profile successfully deleted.')
            return redirect(url_for('docs.index'))
        form['name'] = request.form['name']
        form['email'] = request.form['email']
        if not form['name']:
            flash(u'Error: you have to provide a name.')
        elif '@' not in form['email']:
            flash(u'Error: you have to enter a valid email address.')
        else:
            support.update_username(g.user.name, form['name'])
            try:
                g.user.name = form['name']
                g.user.email = form['email']
                db_session.commit()
            except IntegrityError:
                flash(u'Error: user name or email address already taken.')
            else:
                flash(u'Profile successfully updated.')
                return redirect(url_for('.edit_profile'))
    return render_template('edit_profile.html', form=form)
def edit_profile():
    """Update profile info."""
    if g.user is None:
        abort(401)
    form = dict(name=g.user.name, email=g.user.email)
    if request.method == 'POST':
        if 'delete' in request.form:
            db_session.delete(g.user)
            db_session.commit()
            session[session['token_key']] = None
            flash(u'Profile successfully deleted.')
            return redirect(url_for('docs.index'))
        form['name'] = request.form['name']
        form['email'] = request.form['email']
        if not form['name']:
            flash(u'Error: you have to provide a name.')
        elif '@' not in form['email']:
            flash(u'Error: you have to enter a valid email address.')
        else:
            support.update_username(g.user.name, form['name'])
            try:
                g.user.name = form['name']
                g.user.email = form['email']
                db_session.commit()
            except IntegrityError:
                flash(u'Error: user name or email address already taken.')
            else:
                flash(u'Profile successfully updated.')
                return redirect(url_for('.edit_profile'))
    return render_template('edit_profile.html', form=form)
def create_profile():
    """If this is the user's first login, the create_or_login function
    will redirect here so that the user can set up his profile.
    """
    if g.user is not None or 'token_key' not in session or session[
            'token_key'] not in session:
        return redirect(url_for('docs.index'))
    if request.method == 'POST':
        name = request.form['name']
        email = request.form['email']
        if not name:
            flash(u'Error: you have to provide a name.')
        elif '@' not in email:
            flash(u'Error: you have to enter a valid email address.')
        else:
            db_session.query(User).filter(User.name == name)
            try:
                db_session.add(User(name, email, session['user_id']))
                db_session.commit()
            except IntegrityError:
                flash(u'Error: user name or email address already taken.')
            else:
                flash(u'Profile successfully created.')
                return redirect(oid.get_next_url())
    return render_template('create_profile.html', next_url=oid.get_next_url())
def create_profile():
    """If this is the user's first login, the create_or_login function
    will redirect here so that the user can set up his profile.
    """
    if g.user is not None or 'token_key' not in session or session['token_key'] not in session:
        return redirect(url_for('docs.index'))
    if request.method == 'POST':
        name = request.form['name']
        email = request.form['email']
        if not name:
            flash(u'Error: you have to provide a name.')
        elif '@' not in email:
            flash(u'Error: you have to enter a valid email address.')
        else:
            db_session.query(User).filter(User.name == name)
            try:
                db_session.add(User(name, email, session['user_id']))
                db_session.commit()
            except IntegrityError:
                flash(u'Error: user name or email address already taken.')
            else:
                flash(u'Profile successfully created.')
                return redirect(oid.get_next_url())
    return render_template('create_profile.html', next_url=oid.get_next_url())