@admin_bp.route('/account/edit/<account_name>', methods=['GET', 'POST'])
@admin_bp.route('/account/edit', methods=['GET', 'POST'])
@login_required
@operator_role_required
def edit_account(account_name=None):
    users = User.query.all()
    if (request.method == 'GET'):
        if (account_name is None):
            return render_template('admin_edit_account.html', account_user_ids=[], users=users, create=1)
        else:
            account = Account.query.filter((Account.name == account_name)).first()
            account_user_ids = account.get_user()
            return render_template('admin_edit_account.html', account=account, account_user_ids=account_user_ids, users=users, create=0)
    if (request.method == 'POST'):
        fdata = request.form
        new_user_list = request.form.getlist('account_multi_user')
        if (not account_name):
            account_name = fdata['accountname']
        account = Account(name=account_name, description=fdata['accountdescription'], contact=fdata['accountcontact'], mail=fdata['accountmail'])
        account_user_ids = []
        for username in new_user_list:
            userid = User(username=username).get_user_info_by_username().id
            account_user_ids.append(userid)
        create = int(fdata['create'])
        if create:
            if ((account.name == '') or (account.name != account_name.lower())):
                return render_template('admin_edit_account.html', account=account, account_user_ids=account_user_ids, users=users, create=create, invalid_accountname=True)
            if Account.query.filter((Account.name == account.name)).first():
                return render_template('admin_edit_account.html', account=account, account_user_ids=account_user_ids, users=users, create=create, duplicate_accountname=True)
            result = account.create_account()
            history = History(msg='Create account {0}'.format(account.name), created_by=current_user.username)
        else:
            result = account.update_account()
            history = History(msg='Update account {0}'.format(account.name), created_by=current_user.username)
        if result['status']:
            account.grant_privileges(new_user_list)
            history.add()
            return redirect(url_for('admin.manage_account'))
        return render_template('admin_edit_account.html', account=account, account_user_ids=account_user_ids, users=users, create=create, error=result['msg'])
@admin_bp.route('/account/edit/<account_name>', methods=['GET', 'POST'])
@admin_bp.route('/account/edit', methods=['GET', 'POST'])
@login_required
@operator_role_required
def edit_account(account_name=None):
    users = User.query.all()
    if (request.method == 'GET'):
        if (account_name is None):
            return render_template('admin_edit_account.html', account_user_ids=[], users=users, create=1)
        else:
            account = Account.query.filter((Account.name == account_name)).first()
            account_user_ids = account.get_user()
            return render_template('admin_edit_account.html', account=account, account_user_ids=account_user_ids, users=users, create=0)
    if (request.method == 'POST'):
        fdata = request.form
        new_user_list = request.form.getlist('account_multi_user')
        if (not account_name):
            account_name = fdata['accountname']
        account = Account(name=account_name, description=fdata['accountdescription'], contact=fdata['accountcontact'], mail=fdata['accountmail'])
        account_user_ids = []
        for username in new_user_list:
            userid = User(username=username).get_user_info_by_username().id
            account_user_ids.append(userid)
        create = int(fdata['create'])
        if create:
            if ((account.name == ) or (account.name != account_name.lower())):
                return render_template('admin_edit_account.html', account=account, account_user_ids=account_user_ids, users=users, create=create, invalid_accountname=True)
            if Account.query.filter((Account.name == account.name)).first():
                return render_template('admin_edit_account.html', account=account, account_user_ids=account_user_ids, users=users, create=create, duplicate_accountname=True)
            result = account.create_account()
            history = History(msg='Create account {0}'.format(account.name), created_by=current_user.username)
        else:
            result = account.update_account()
            history = History(msg='Update account {0}'.format(account.name), created_by=current_user.username)
        if result['status']:
            account.grant_privileges(new_user_list)
            history.add()
            return redirect(url_for('admin.manage_account'))
        return render_template('admin_edit_account.html', account=account, account_user_ids=account_user_ids, users=users, create=create, error=result['msg'])