From c09d614c8bfdfd6e8787d53c097f637e9252a85a Mon Sep 17 00:00:00 2001 From: Daniel Rahamim Date: Tue, 15 Oct 2024 22:59:38 -0700 Subject: [PATCH] feat(search): add ability to search for staff or asset by id (#191) * feat(search): add ability to search for staff or asset by id Co-authored-by: Daniel Rahamim <3834219+drahamim@users.noreply.github.com> * style(app): fix syntax issue in core code Signed-off-by: Daniel Rahamim <3834219+drahamim@users.noreply.github.com> * fix(links): staff id links incorrectly tried to load assets detail Signed-off-by: Daniel Rahamim <3834219+drahamim@users.noreply.github.com> * style(app): remove excess spaces Signed-off-by: Daniel Rahamim <3834219+drahamim@users.noreply.github.com> --------- Signed-off-by: Daniel Rahamim <3834219+drahamim@users.noreply.github.com> Co-authored-by: Daniel Rahamim <3834219+drahamim@users.noreply.github.com> --- src/invenflask/app.py | 52 +++++++++++++++++++++++++-- src/invenflask/templates/base.html | 6 ++-- src/invenflask/templates/history.html | 2 +- src/invenflask/templates/index.html | 2 +- src/invenflask/templates/search.html | 49 +++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 src/invenflask/templates/search.html diff --git a/src/invenflask/app.py b/src/invenflask/app.py index 312bb91..034fbeb 100644 --- a/src/invenflask/app.py +++ b/src/invenflask/app.py @@ -320,7 +320,8 @@ def return_asset(): else: assets_still_out = len(current_checkouts) flash('Asset was successfully returned!', "success") - flash(f'Staffer still has {assets_still_out} assets checked out', "warning") + flash( + f'Staffer still has {assets_still_out} assets checked out', "warning") return redirect(url_for('return_asset')) except Exception as e: app.logger.error(e) @@ -356,11 +357,13 @@ def single_history(rq_type, item_id): if rq_type == 'asset': item_info = db.session.query(Asset).get(item_id) current = db.session.query(Checkout).filter_by(assetid=item_id).all() - history = db.session.query(History).order_by('returntime').filter_by(assetid=item_id).all() + history = db.session.query(History).order_by( + 'returntime').filter_by(assetid=item_id).all() elif rq_type == 'staff': item_info = db.session.query(Staff).get(item_id) current = db.session.query(Checkout).filter_by(staffid=item_id).all() - history = db.session.query(History).order_by('returntime').filter_by(staffid=item_id).all() + history = db.session.query(History).order_by( + 'returntime').filter_by(staffid=item_id).all() return render_template( 'single_history.html', hist_type=rq_type, current=current, history=history, item_info=item_info @@ -478,3 +481,46 @@ def settings(): form.timezone.data = db.session.query(GlobalSet).filter( GlobalSet.settingid == "timezone").first().setting return render_template('settings.html', title='Settings', form=form) + + +@app.route('/search', methods=["GET", "POST"]) +def search(): + app.logger.info('Search request') + query = request.args.get('query') + app.logger.info + (f'Search request: {query}') + query = str(query) + asset = db.session.query(Asset).filter( + func.lower(Asset.id).like(f"%{query.lower()}%")).all() + staff = db.session.query(Staff).filter( + func.lower(Staff.id).like(f"%{query.lower()}%")).all() + db.session.commit() + print(asset) + print(staff) + + if len(asset) == 1 and len(staff) == 0: + app.logger.info + (f'Asset found: {query}') + return redirect(url_for('single_history', + rq_type='asset', item_id=asset[0].id)) + elif len(staff) == 1 and len(asset) == 0: + app.logger.info + (f'Staff found: {query}') + app.logger.info + (staff[0].id) + return redirect(url_for('single_history', + rq_type='staff', item_id=staff[0].id)) + elif not asset and not staff: + flash('No results found', 'warning') + app.logger.info + ('No results found') + return redirect(url_for('index')) + else: + flash("multiple results found", 'warning') + app.logger.info + ('multiple results found') + app.logger.info + (len(asset)) + app.logger.info + (len(staff)) + return render_template('search.html', assets=asset, staff=staff) diff --git a/src/invenflask/templates/base.html b/src/invenflask/templates/base.html index 12b5da2..6d3cbb1 100644 --- a/src/invenflask/templates/base.html +++ b/src/invenflask/templates/base.html @@ -55,10 +55,10 @@ --> - + diff --git a/src/invenflask/templates/history.html b/src/invenflask/templates/history.html index 8a07dd8..b98044e 100644 --- a/src/invenflask/templates/history.html +++ b/src/invenflask/templates/history.html @@ -16,7 +16,7 @@ {% for asset in assets %} {{ asset.assetid }} - {{ asset.staffid }} + {{ asset.staffid }} {{ asset.department }} {{ asset.division }} {{ moment(timestamp=asset.checkouttime).format('LLL') }} diff --git a/src/invenflask/templates/index.html b/src/invenflask/templates/index.html index 0960f7a..b8c7755 100644 --- a/src/invenflask/templates/index.html +++ b/src/invenflask/templates/index.html @@ -37,7 +37,7 @@

Checked Out Assets

{% for item in checkouts %} {{ item.assetid }} - {{ item.staffid }} + {{ item.staffid }} {{ item.department }} {{ moment(item.timestamp).format('LLL') }} diff --git a/src/invenflask/templates/search.html b/src/invenflask/templates/search.html new file mode 100644 index 0000000..069a441 --- /dev/null +++ b/src/invenflask/templates/search.html @@ -0,0 +1,49 @@ +{% extends 'base.html' %} +{% block content %} +

{% block title %} Search {% endblock %}

+

Staff

+ + + + + + + + + + + + + {% for person in staff %} + + + + + + + + + {% endfor %} + +
Staff IDFirst NameLast NameDivisionDepartmentTitle
{{ person.id }}{{ person.first_name }}{{ person.last_name }}{{ person.division }}{{ person.department }}{{ person.title }}
+ +

Assets

+ + + + + + + + + + {% for item in assets %} + + + + + + {% endfor %} + +
Asset IDAsset TypeAsset Status
{{ item.id }}{{ item.asset_type }}{{ item.asset_status }}
+{% endblock %} \ No newline at end of file