Implementing search.
This commit is contained in:
parent
4c56149f1b
commit
07512aee93
5 changed files with 212 additions and 24 deletions
69
inventory/routes/search.py
Normal file
69
inventory/routes/search.py
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
from flask import Blueprint, render_template, request
|
||||
|
||||
import crudkit
|
||||
|
||||
from crudkit.ui.fragments import render_table
|
||||
|
||||
from ..models import Inventory, User, WorkLog
|
||||
|
||||
bp_search = Blueprint("search", __name__)
|
||||
|
||||
def init_search_routes(app):
|
||||
@bp_search.get("/search")
|
||||
def search():
|
||||
q = request.args.get("q")
|
||||
if not q:
|
||||
return "Oh no!"
|
||||
|
||||
inventory_service = crudkit.crud.get_service(Inventory)
|
||||
user_service = crudkit.crud.get_service(User)
|
||||
worklog_service = crudkit.crud.get_service(WorkLog)
|
||||
|
||||
inventory_columns = [
|
||||
{"field": "label"},
|
||||
{"field": "barcode"},
|
||||
{"field": "name"},
|
||||
{"field": "serial"},
|
||||
{"field": "brand.name"},
|
||||
{"field": "model"},
|
||||
{"field": "device_type.description"},
|
||||
{"field": "owner.label"},
|
||||
{"field": "location.label"}
|
||||
]
|
||||
inventory_results = inventory_service.list({
|
||||
'notes|label|owner.label__icontains': q,
|
||||
'fields': [
|
||||
"label",
|
||||
"name",
|
||||
"barcode",
|
||||
"serial",
|
||||
"brand.name",
|
||||
"model",
|
||||
"device_type.description",
|
||||
"owner.label",
|
||||
"location.label",
|
||||
]
|
||||
})
|
||||
|
||||
user_columns = [
|
||||
{"field": "last_name"},
|
||||
{"field": "first_name"},
|
||||
{"field": "title"},
|
||||
{"field": "supervisor.label"},
|
||||
]
|
||||
user_results = user_service.list({
|
||||
'supervisor.label|label__icontains': q,
|
||||
'fields': [
|
||||
"last_name",
|
||||
"first_name",
|
||||
"title",
|
||||
"supervisor.label",
|
||||
]
|
||||
})
|
||||
|
||||
inventory_results = render_table(inventory_results, inventory_columns)
|
||||
user_results = render_table(user_results, user_columns)
|
||||
|
||||
return render_template('search.html', q=q, inventory_results=inventory_results, user_results=user_results)
|
||||
|
||||
app.register_blueprint(bp_search)
|
||||
Loading…
Add table
Add a link
Reference in a new issue