
- Created a new Blueprint for main routes in `routes/__init__.py`. - Implemented inventory listing and item management in `routes/inventory.py`. - Added user listing and detail views in `routes/user.py`. - Developed worklog listing and entry views in `routes/worklog.py`. - Introduced search functionality across inventory, users, and worklogs in `routes/search.py`. - Established settings management for brands, items, rooms, and functions in `routes/settings.py`. - Enhanced helper functions for rendering headers and managing data in `routes/helpers.py`. - Updated index route to display active worklogs and inventory conditions in `routes/index.py`.
68 lines
2.9 KiB
Python
68 lines
2.9 KiB
Python
from flask import request, redirect, url_for, render_template
|
|
from sqlalchemy import or_
|
|
from sqlalchemy.orm import aliased
|
|
|
|
from . import main
|
|
from .helpers import inventory_headers, user_headers, worklog_headers
|
|
from .. import db
|
|
from ..models import Inventory, User, WorkLog
|
|
from ..utils.load import eager_load_inventory_relationships, eager_load_user_relationships, eager_load_worklog_relationships
|
|
|
|
@main.route("/search")
|
|
def search():
|
|
query = request.args.get('q', '').strip()
|
|
|
|
if not query:
|
|
return redirect(url_for('main.index'))
|
|
|
|
InventoryAlias = aliased(Inventory)
|
|
UserAlias = aliased(User)
|
|
|
|
inventory_query = eager_load_inventory_relationships(db.session.query(Inventory).join(UserAlias, Inventory.owner)).filter(
|
|
or_(
|
|
Inventory.inventory_name.ilike(f"%{query}%"),
|
|
Inventory.serial.ilike(f"%{query}%"),
|
|
Inventory.barcode.ilike(f"%{query}%"),
|
|
Inventory.notes.ilike(f"%{query}%"),
|
|
UserAlias.first_name.ilike(f"%{query}%"),
|
|
UserAlias.last_name.ilike(f"%{query}%")
|
|
))
|
|
inventory_results = inventory_query.all()
|
|
user_query = eager_load_user_relationships(db.session.query(User).join(UserAlias, User.supervisor)).filter(
|
|
or_(
|
|
User.first_name.ilike(f"%{query}%"),
|
|
User.last_name.ilike(f"%{query}%"),
|
|
UserAlias.first_name.ilike(f"%{query}%"),
|
|
UserAlias.last_name.ilike(f"%{query}%")
|
|
))
|
|
user_results = user_query.all()
|
|
worklog_query = eager_load_worklog_relationships(db.session.query(WorkLog).join(UserAlias, WorkLog.contact).join(InventoryAlias, WorkLog.work_item)).filter(
|
|
or_(
|
|
WorkLog.notes.ilike(f"%{query}%"),
|
|
UserAlias.first_name.ilike(f"%{query}%"),
|
|
UserAlias.last_name.ilike(f"%{query}%"),
|
|
InventoryAlias.inventory_name.ilike(f"%{query}%"),
|
|
InventoryAlias.serial.ilike(f"%{query}%"),
|
|
InventoryAlias.barcode.ilike(f"%{query}%")
|
|
))
|
|
worklog_results = worklog_query.all()
|
|
|
|
results = {
|
|
'inventory': {
|
|
'results': inventory_query,
|
|
'headers': inventory_headers,
|
|
'rows': [{"id": item.id, "cells": [fn(item) for fn in inventory_headers.values()]} for item in inventory_results]
|
|
},
|
|
'users': {
|
|
'results': user_query,
|
|
'headers': user_headers,
|
|
'rows': [{"id": user.id, "cells": [fn(user) for fn in user_headers.values()]} for user in user_results]
|
|
},
|
|
'worklog': {
|
|
'results': worklog_query,
|
|
'headers': worklog_headers,
|
|
'rows': [{"id": log.id, "cells": [fn(log) for fn in worklog_headers.values()]} for log in worklog_results]
|
|
}
|
|
}
|
|
|
|
return render_template('search.html', title=f"Database Search ({query})" if query else "Database Search", results=results, query=query)
|