inventory/inventory/routes/search.py
2025-10-10 09:17:02 -05:00

96 lines
3.6 KiB
Python

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", "label": "Bar Code #"},
{"field": "name"},
{"field": "serial", "label": "Serial #"},
{"field": "brand.name", "label": "Brand"},
{"field": "model"},
{"field": "device_type.description", "label": "Device Type"},
{"field": "owner.label", "label": "Contact",
"link": {"endpoint": "entry.entry", "params": {"id": "{owner.id}", "model": "user"}}},
{"field": "location.label", "label": "Location"},
]
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", "label": "Supervisor",
"link": {"endpoint": "entry.entry", "params": {"id": "{supervisor.id}", "model": "user"}}},
{"field": "location.label", "label": "Location"},
]
user_results = user_service.list({
'supervisor.label|label__icontains': q,
'fields': [
"last_name",
"first_name",
"title",
"supervisor.label",
"location.label",
]
})
worklog_columns = [
{"field": "contact.label", "label": "Contact",
"link": {"endpoint": "entry.entry", "params": {"id": "{contact.id}", "model": "user"}}},
{"field": "work_item.label", "label": "Work Item",
"link": {"endpoint": "entry.entry", "params": {"id": "{work_item.id}", "model": "inventory"}}},
{"field": "complete", "format": "yesno"},
{"field": "start_time", "format": "datetime"},
{"field": "end_time", "format": "datetime"},
{"field": "updates", "format": lambda x: len(x)},
]
worklog_results = worklog_service.list({
'contact.label|work_item.label|updates.content__icontains': q,
'fields': [
"contact.label",
"work_item.label",
"complete",
"start_time",
"end_time",
"updates",
]
})
inventory_results = render_table(inventory_results, inventory_columns, opts={"object_class": "inventory"})
user_results = render_table(user_results, user_columns, opts={"object_class": "user"})
worklog_results = render_table(worklog_results, worklog_columns, opts={"object_class": "worklog"})
return render_template('search.html', q=q, inventory_results=inventory_results, user_results=user_results, worklog_results=worklog_results)
app.register_blueprint(bp_search)