@admin_bp.route('/user/edit/<user_username>', methods=['GET', 'POST'])
@admin_bp.route('/user/edit', methods=['GET', 'POST'])
@login_required
@operator_role_required
def edit_user(user_username=None):
    if user_username:
        user = User.query.filter((User.username == user_username)).first()
        create = False
        if (not user):
            return (render_template('errors/404.html'), 404)
        if ((user.role.name == 'Administrator') and (current_user.role.name != 'Administrator')):
            return (render_template('errors/401.html'), 401)
    else:
        user = None
        create = True
    if (request.method == 'GET'):
        return render_template('admin_edit_user.html', user=user, create=create)
    elif (request.method == 'POST'):
        fdata = request.form
        if create:
            user_username = fdata.get('username', '').strip()
        user = User(username=user_username, plain_text_password=fdata.get('password', ''), firstname=fdata.get('firstname', '').strip(), lastname=fdata.get('lastname', '').strip(), email=fdata.get('email', '').strip(), reload_info=False)
        if create:
            if (not fdata.get('password', '')):
                return render_template('admin_edit_user.html', user=user, create=create, blank_password=True)
            result = user.create_local_user()
            history = History(msg='Created user {0}'.format(user.username), created_by=current_user.username)
        else:
            result = user.update_local_user()
            history = History(msg='Updated user {0}'.format(user.username), created_by=current_user.username)
        if result['status']:
            history.add()
            return redirect(url_for('admin.manage_user'))
        return render_template('admin_edit_user.html', user=user, create=create, error=result['msg'])
@admin_bp.route('/user/edit/<user_username>', methods=['GET', 'POST'])
@admin_bp.route('/user/edit', methods=['GET', 'POST'])
@login_required
@operator_role_required
def edit_user(user_username=None):
    if user_username:
        user = User.query.filter((User.username == user_username)).first()
        create = False
        if (not user):
            return (render_template('errors/404.html'), 404)
        if ((user.role.name == 'Administrator') and (current_user.role.name != 'Administrator')):
            return (render_template('errors/401.html'), 401)
    else:
        user = None
        create = True
    if (request.method == 'GET'):
        return render_template('admin_edit_user.html', user=user, create=create)
    elif (request.method == 'POST'):
        fdata = request.form
        if create:
            user_username = fdata.get('username', ).strip()
        user = User(username=user_username, plain_text_password=fdata.get('password', ), firstname=fdata.get('firstname', ).strip(), lastname=fdata.get('lastname', ).strip(), email=fdata.get('email', ).strip(), reload_info=False)
        if create:
            if (not fdata.get('password', )):
                return render_template('admin_edit_user.html', user=user, create=create, blank_password=True)
            result = user.create_local_user()
            history = History(msg='Created user {0}'.format(user.username), created_by=current_user.username)
        else:
            result = user.update_local_user()
            history = History(msg='Updated user {0}'.format(user.username), created_by=current_user.username)
        if result['status']:
            history.add()
            return redirect(url_for('admin.manage_user'))
        return render_template('admin_edit_user.html', user=user, create=create, error=result['msg'])