def post_account():
    # Flask apparently throws 400 errors if POST form data isn't present.
    # Full name
    user_id = session['user_id']
    location = settings.get_user(user_id)['feed_location']
    user = User(local_url=location)

    if request.form.get('full_name_changed') == 'true':
        user.full_name = request.form['full_name']
    # Username
    if request.form.get('username_changed') == 'true':
        username= request.form['username']
        if len(username) > 0:
            user.username = username
    # Bio
    if request.form.get('bio_changed') == 'true':
        user.description = request.form['bio']
    # Email
    if request.form.get('email_changed') == 'true':
        email = request.form['email']
        user_dict = settings.get_user(user_id)
        user_dict['email'] = email
        settings.add_user(user_dict)
    # Password
    if request.form.get('password_changed') == 'true':
        password = request.form['password']
        password_confirm = request.form['password_confirm']
        if password == password_confirm:
            user_dict = settings.get_user(user_id)
            user_dict['pwd_hash'] = generate_password_hash(password)
            settings.add_user(user_dict)
    # Language
    if request.form.get('language_changed') == 'true':
        user.language = request.form['language']
    return render_template('account.html', user=user, error='Your settings have been saved.')
def post_register():
    """ Registers a new user. """
    error = ''
    user_full_name = request.form['full_name']
    username = request.form['username']
    password = request.form['password']
    password_confirm = request.form['password_confirm']
    email = request.form['email']

    # User is already logged in.
    if 'user_id' in session:
        return redirect(url_for('home'))
    # No more users can register.
    elif settings.get('single_user_mode') \
            and len(settings.get('registered_users')) > 0:
        error = 'No more users can register at this time.'
    # Username is alredy registered.
    users = settings.get('registered_users')
    username_taken = True if \
            len([uid for uid, ud, in users.iteritems() if ud.get('username') == username]) > 0 \
            else False
    if username_taken:
        return redirect(url_for('get_login'))

    # Register the new user.
    if username is None:
        error = 'No username provided.'
    elif len(username) < 8 or len(username) > 25:
        error = 'Username is not the correct length. \
                Please enter a username between 8-25 characters.'
    elif re.search('[^a-zA-Z0-9\_]', username) is not None:
        error = 'Usernames can only contain letters an numbers.'
    elif re.search('\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b', email) is not None:
        error = 'Please enter a valid email address.'
    elif password is None or password_confirm is None:
        error = 'You must fill in your password.'
    elif password != request.form['password_confirm']:
        error = 'Passwords do not match.'
    elif len(password) < 8 or re.search('[a-zA-Z0-9]', password) is None:
        error = 'Your password must be at least 8 characters long and \
                must be a combination of numbers and letters. Special\
                characters are allowed and encouraged.'
    else:
        new_user, feed_location, blocks_location, follows_location = User.create(username)

        if settings.get('single_user_mode'):
            settings.add('single_user_id', new_user.user_id)

        domain = settings.get('domain')
        new_user.profile = 'http://{0}/{1}'.format(domain, new_user.username)
        new_user.link = 'http://{0}/{1}/feed.xml'.format(domain, new_user.username)
        new_user.follows_url = 'http://{0}/{1}/follows.xml'.format(domain, new_user.username)
        new_user.blocks_url = 'http://{0}/{1}/blocks.xml'.format(domain, new_user.username)
        new_user.message_url = 'http://{0}/{1}/message.'.format(domain, new_user.username)
        new_user.language = 'en'

        # Update the settings.
        pwd_hash = generate_password_hash(password)
        settings.add_user(username=new_user.username,
                pwd_hash=pwd_hash,
                user_id=new_user.user_id,
                feed_location=feed_location,
                blocks_location=blocks_location,
                follows_location=follows_location)
        session['user_id'] = new_user.user_id
        return redirect(url_for('home'))

    return render_template('registration.html', error=error)