Esempio n. 1
0
def user_save():

    from model import User

    # Load user id from original username
    user = User.getInstanceFromUsername(request.form['original_username'])

    errors = []
    # Validate data
    # Check for required fields
    labels = {'username': '******', 'email': 'E-mail', 'display_name': 'Display Name'}
    for el in ['username', 'email', 'display_name']:
        if el not in request.form or len(request.form[el]) == 0:
            errors.append("'{0}' is a required field.".format(labels[el]))

    # Make sure username is not currently used
    if user.username != request.form['username'] and User.usernameExists(request.form['username'], exclude_user_id = user.user_id):
        errors.append("The username '{username}' already exists in our system. Please choose a different one.".format(username = request.form['username']))

    # No spaces in username or password
    if ' ' in request.form['username']:
        errors.append('Username may not contain spaces')
    if len(request.form['username']) not in range(4, 31):
        errors.append('Username must be 4-30 characters long.')

    # If errors, display form again
    if len(errors) != 0:

        role_id = ''
        if 'role_id' in request.form and len(request.form['role_id']) > 0:
            role_id = int(request.form['role_id'])


        return render_template(
            'user/form.html', error = "<br/>\n".join(errors),
            original_username = request.form['original_username'],
            username = request.form['username'],
            email = request.form['email'],
            display_name = request.form['display_name'],
            active = int(request.form['active']),
            role_ids = [role_id]
        )

    # If no errors, save user as not active and redirect to awaiting approval page
    user.username = request.form['username']
    user.email = request.form['email']
    user.display_name = request.form['display_name']
    user.active = request.form['active']
    user.save()

    user.update_roles([request.form['role_id']])
    return redirect(url_for('user_manage'))
Esempio n. 2
0
def user_form(username):

    from model import User
    user = User.getInstanceFromUsername(username)

    # Do not allow editing administrators
    if user.is_administrator():

        return redirect(url_for('user'))

    return render_template('user/form.html',
                username = user.username,
                original_username = user.username,
                email = user.email,
                display_name = user.display_name,
                active = user.active,
                role_ids = user.role_ids()
    )