Add list to manage with an account associated domains

This commit is contained in:
AdvanticGmbH 2022-06-28 11:15:43 +02:00
parent e6f6f9cea4
commit 6a2ba1b1c3
2 changed files with 95 additions and 40 deletions

View File

@ -610,14 +610,24 @@ def manage_user():
@operator_role_required
def edit_account(account_name=None):
users = User.query.all()
account = Account.query.filter(
Account.name == account_name).first()
all_accounts = Account.query.all()
accounts = {}
domains = Domain.query.all()
for acc in all_accounts:
accounts[acc.id] = acc
if request.method == 'GET':
if account_name is None:
if account_name is None or not account:
return render_template('admin_edit_account.html',
account=None,
account_user_ids=[],
users=users,
domains=domains,
accounts=accounts,
create=1)
else:
account = Account.query.filter(
Account.name == account_name).first()
@ -626,11 +636,14 @@ def edit_account(account_name=None):
account=account,
account_user_ids=account_user_ids,
users=users,
domains=domains,
accounts=accounts,
create=0)
if request.method == 'POST':
fdata = request.form
new_user_list = request.form.getlist('account_multi_user')
new_domain_list = request.form.getlist('account_domains')
# on POST, synthesize account and account_user_ids from form data
if not account_name:
@ -654,6 +667,8 @@ def edit_account(account_name=None):
account=account,
account_user_ids=account_user_ids,
users=users,
domains=domains,
accounts=accounts,
create=create,
invalid_accountname=True)
@ -662,19 +677,33 @@ def edit_account(account_name=None):
account=account,
account_user_ids=account_user_ids,
users=users,
domains=domains,
accounts=accounts,
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 = Account.query.filter(
Account.name == account_name).first()
old_domains = Domain.query.filter(Domain.account_id == account.id).all()
for domain_name in new_domain_list:
domain = Domain.query.filter(
Domain.name == domain_name).first()
if account.id != domain.account_id:
Domain(name=domain_name).assoc_account(account.id)
for domain in old_domains:
if domain.name not in new_domain_list:
Domain(name=domain.name).assoc_account(None)
history = History(msg='{0} account {1}'.format('Create' if create else 'Update', account.name),
created_by=current_user.username)
account.grant_privileges(new_user_list)
history.add()
return redirect(url_for('admin.manage_account'))

View File

@ -90,6 +90,28 @@
</select>
</div>
</div>
<div class="box-body">
<p>Domains on the right are associated with the account. Red marked domain names are already associated with other accounts.
Moving already associated domains to this account will overwrite the previous associated account.
</p>
<p>Hover over the red domain names to show the associated account. Click on domains to move between columns.</p>
<div class="form-group col-xs-2">
<select multiple="multiple" class="form-control" id="account_domains"
name="account_domains">
{% for domain in domains %}
{% if account != None and domain.account_id != None and account.id != domain.account_id %}
{% with account_id=domain.account_id %}
<option style="color: red" data-toggle="tooltip" title="Associated with: {{ accounts[account_id].name }}"
value="{{ domain.name }}">{{ domain.name }}</option>
{% endwith %}
{% else %}
<option {% if account.id == domain.account_id %}selected{% endif %}
value="{{ domain.name }}">{{ domain.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
</div>
<div class="box-footer">
<button type="submit"
class="btn btn-flat btn-primary">{% if create %}Create{% else %}Update{% endif %}
@ -127,40 +149,44 @@
{% endblock %}
{% block extrascripts %}
<script>
$("#account_multi_user").multiSelect({
selectableHeader: "<input type='text' class='search-input' autocomplete='off' placeholder='Username'>",
selectionHeader: "<input type='text' class='search-input' autocomplete='off' placeholder='Username'>",
afterInit: function (ms) {
var that = this,
$selectableSearch = that.$selectableUl.prev(),
$selectionSearch = that.$selectionUl.prev(),
selectableSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selectable:not(.ms-selected)',
selectionSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selection.ms-selected';
function addMultiSelect(id, placeholder) {
$(id).multiSelect({
selectableHeader: `<input type='text' class='search-input' autocomplete='off' placeholder='${placeholder}'>`,
selectionHeader: `<input type='text' class='search-input' autocomplete='off' placeholder='${placeholder}'>`,
afterInit: function (ms) {
var that = this,
$selectableSearch = that.$selectableUl.prev(),
$selectionSearch = that.$selectionUl.prev(),
selectableSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selectable:not(.ms-selected)',
selectionSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selection.ms-selected';
that.qs1 = $selectableSearch.quicksearch(selectableSearchString)
.on('keydown', function (e) {
if (e.which === 40) {
that.$selectableUl.focus();
return false;
}
});
that.qs1 = $selectableSearch.quicksearch(selectableSearchString)
.on('keydown', function (e) {
if (e.which === 40) {
that.$selectableUl.focus();
return false;
}
});
that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
.on('keydown', function (e) {
if (e.which == 40) {
that.$selectionUl.focus();
return false;
}
});
},
afterSelect: function () {
this.qs1.cache();
this.qs2.cache();
},
afterDeselect: function () {
this.qs1.cache();
this.qs2.cache();
}
});
that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
.on('keydown', function (e) {
if (e.which == 40) {
that.$selectionUl.focus();
return false;
}
});
},
afterSelect: function () {
this.qs1.cache();
this.qs2.cache();
},
afterDeselect: function () {
this.qs1.cache();
this.qs2.cache();
}
});
}
addMultiSelect("#account_multi_user", "Username")
addMultiSelect("#account_domains", "Domain")
</script>
{% endblock %}