Skip to content

Commit

Permalink
feat(search): add ability to search for staff or asset by id (#191)
Browse files Browse the repository at this point in the history
* feat(search): add ability to search for staff or asset by id

Co-authored-by: Daniel Rahamim <[email protected]>

* style(app): fix syntax issue in core code

Signed-off-by: Daniel Rahamim <[email protected]>

* fix(links): staff id links incorrectly tried to load assets detail

Signed-off-by: Daniel Rahamim <[email protected]>

* style(app): remove excess spaces

Signed-off-by: Daniel Rahamim <[email protected]>

---------

Signed-off-by: Daniel Rahamim <[email protected]>
Co-authored-by: Daniel Rahamim <[email protected]>
  • Loading branch information
drahamim and drahamim authored Oct 16, 2024
1 parent 4c7621f commit c09d614
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 8 deletions.
52 changes: 49 additions & 3 deletions src/invenflask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions src/invenflask/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@
</a>
</li> -->

<!-- <form class="d-flex">
<input class="form-control me-sm-2" type="search" placeholder="Search">
<form class="d-flex", method="GET" action="/search" >
<input class="form-control me-sm-2" type="text" name="query" placeholder="Search">
<button class="btn btn-secondary my-2 my-sm-0" type="submit">Search</button>
</form> -->
</form>
</div>
</div>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion src/invenflask/templates/history.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% for asset in assets %}
<tr>
<td><a href='/single_history/asset/{{asset.assetid}}'> {{ asset.assetid }}</a></td>
<td><a href='/single_history/asset/{{asset.staffid}}'>{{ asset.staffid }}</a></td>
<td><a href='/single_history/staff/{{asset.staffid}}'>{{ asset.staffid }}</a></td>
<td>{{ asset.department }}</td>
<td>{{ asset.division }}</td>
<td>{{ moment(timestamp=asset.checkouttime).format('LLL') }}</td>
Expand Down
2 changes: 1 addition & 1 deletion src/invenflask/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h4>Checked Out Assets</h4>
{% for item in checkouts %}
<tr>
<td><a href='/single_history/asset/{{item.assetid}}'> {{ item.assetid }}</a></td>
<td><a href='/single_history/asset/{{item.staffid}}'>{{ item.staffid }}</a></td>
<td><a href='/single_history/staff/{{item.staffid}}'>{{ item.staffid }}</a></td>
<td>{{ item.department }}</td>
<td>{{ moment(item.timestamp).format('LLL') }}</td>
</td>
Expand Down
49 changes: 49 additions & 0 deletions src/invenflask/templates/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{% extends 'base.html' %}
{% block content %}
<h1>{% block title %} Search {% endblock %}</h1>
<h4>Staff</h4>
<table id="staff" class="table table-striped">
<thead>
<tr>
<th>Staff ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Division</th>
<th>Department</th>
<th>Title</th>
</tr>
</thead>
<tbody>
{% for person in staff %}
<tr>
<td><a href='/single_history/staff/{{person.id}}'> {{ person.id }}</a></td>
<td>{{ person.first_name }}</td>
<td>{{ person.last_name }}</td>
<td>{{ person.division }}</td>
<td>{{ person.department }}</td>
<td>{{ person.title }}</td>
</tr>
{% endfor %}
</tbody>
</table>

<h4>Assets</h4>
<table id="assets" class="table table-striped">
<thead>
<tr>
<th>Asset ID</th>
<th>Asset Type</th>
<th>Asset Status</th>
</tr>
</thead>
<tbody>
{% for item in assets %}
<tr>
<td><a href='/single_history/asset/{{item.id}}'> {{ item.id }}</a></td>
<td>{{ item.asset_type }}</td>
<td>{{ item.asset_status }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}

0 comments on commit c09d614

Please sign in to comment.