Skip to content

Commit

Permalink
Add form to add tags to selected popup
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanqui committed Aug 14, 2024
1 parent 1d490de commit 1c05ab2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
21 changes: 19 additions & 2 deletions rhinventory/admin_views/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from math import ceil
from typing import Optional, Union, Iterable

from flask import Response, redirect, request, flash, url_for, get_template_attribute
from flask import Response, abort, redirect, request, flash, url_for, get_template_attribute
from wtforms import RadioField, TextAreaField, Field
import wtforms.validators
from flask_admin import expose
Expand All @@ -30,7 +30,7 @@
from rhinventory.models.log import LogEvent, log
from rhinventory.forms import FileForm
from rhinventory.models.asset import AssetCategory
from rhinventory.models.asset_attributes import Company
from rhinventory.models.asset_attributes import AssetTag, Company
from rhinventory.util import require_write_access

TESTING = "pytest" in sys.modules
Expand Down Expand Up @@ -717,6 +717,23 @@ def set_parent_bulk(self) -> Response:
flash(message, "success")

return redirect(url_for('.index_view'))

@expose('/add_tag_bulk/', methods=['POST'])
@require_write_access
def add_tag_bulk(self) -> Response:
asset_tag: AssetTag | None = db.session.query(AssetTag).get(int(request.form['tag_id']))
if not asset_tag:
abort(404)

bulk_datetime = datetime.utcnow()
assets: list[Asset] = get_asset_list_from_request_args()
for asset in assets:
asset.tags.append(asset_tag)
db.session.add(asset)
db.session.commit()
flash(f"Given {len(assets)} assets the tag {asset_tag}.", 'success')

return redirect(url_for('.index_view'))

@expose('/bulk_new/', methods=('GET', 'POST'))
@require_write_access
Expand Down
1 change: 1 addition & 0 deletions rhinventory/templates/admin/asset/details.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ <h2><small>{{ render_asset_code(model) }}</small> {{ model.name }}</h2>
<tr><td><strong>Tags</strong></td>
<td>
{% for tag in model.tags %}
{{ icon('tag') }}
{% if not current_user.is_authenticated %}
{{ tag.name -}}
{%- if not loop.last -%},{% endif %}
Expand Down
55 changes: 32 additions & 23 deletions rhinventory/templates/admin/asset/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
action="{{ get_url('transaction.add_to_view') }}"
method="POST"
>
<input id="selected-add-transaction-id-asset-id" type="hidden" name="asset_id" value="">
<input id="selected-add-transaction-id-transaction-id" name="transaction_id" type="text" placeholder="Transaction ID" style="margin: 0; padding: 2px; width: 100px;">
<input type="hidden" name="asset_id" value="">
<input name="transaction_id" type="text" placeholder="Transaction ID" style="margin: 0; padding: 2px; width: 100px;">
<div class="btn-group">
<input type="submit" value="Add to transaction" class="btn">
</div>
Expand All @@ -92,8 +92,8 @@
method="POST"
>
{{ icon('jar') }}
<input id="selected-set-parent-bulk-asset-id" type="hidden" name="asset_id" value="">
<input id="selected-set-parent-bulk-parent-asset-id" name="parent_asset_id" type="text" placeholder="Parent asset ID" >
<input type="hidden" name="asset_id" value="">
<input name="parent_asset_id" type="text" placeholder="Parent asset ID" >
<div class="btn-group">
<input type="submit" value="Set parent" class="btn">
</div>
Expand All @@ -103,7 +103,18 @@
TODO
</div>
<div class="tab-pane" id="selected-tab-tag">
TODO
<form
id="selected-add-tag-bulk-form"
class="input-append"
action="{{ get_url('asset.add_tag_bulk') }}"
method="POST"
>
<input type="hidden" name="asset_id" value="">
<input name="tag_id" type="text" placeholder="Tag ID" >
<div class="btn-group">
<input type="submit" value="Add tag" class="btn">
</div>
</form>
</div>
</div>
</div>
Expand Down Expand Up @@ -210,27 +221,25 @@
document.location.href = "{{ get_url('transaction.create_view') }}?asset_id=[" + selected + "]";
}

document.getElementById("selected-add-to-transaction-form").addEventListener('submit', function(event) {
var transaction_id = document.getElementById("selected-add-transaction-id-transaction-id").value;
if (transaction_id == "") {
alert("Transaction ID is required");
event.preventDefault()
return false;
}
function makeSelectedForm(action, object_name) {
document.getElementById(`selected-${action}-form`).addEventListener('submit', function(event) {
console.log(`#selected-${action}-form input[name="${object_name}_id"]`)
var id = document.querySelector(`#selected-${action}-form input[name="${object_name}_id"]`).value;
if (id == "") {
alert(`${object_name} ID is required`);
event.preventDefault()
return false;
}

document.getElementById("selected-add-transaction-id-asset-id").value = selected;
})
document.querySelector(`#selected-${action}-form input[name="asset_id"]`).value = selected;
})
}

document.getElementById("selected-set-parent-bulk-form").addEventListener('submit', function(event) {
var transaction_id = document.getElementById("selected-set-parent-bulk-parent-asset-id").value;
if (transaction_id == "") {
alert("Parent asset ID is required");
event.preventDefault()
return false;
}
makeSelectedForm('add-to-transaction', 'transaction');

document.getElementById("selected-set-parent-bulk-asset-id").value = selected;
})
makeSelectedForm('set-parent-bulk', 'parent_asset');

makeSelectedForm('add-tag-bulk', 'tag');

/* Select multiple checkboxes with shift */

Expand Down

0 comments on commit 1c05ab2

Please sign in to comment.