Refactor user, room, and inventory models to replace 'full_name' property with 'identifier' for consistency; update related templates and routes accordingly.
This commit is contained in:
parent
d12023ecd1
commit
462c077681
13 changed files with 129 additions and 88 deletions
|
|
@ -20,8 +20,8 @@ inventory_headers = {
|
|||
"Model": lambda i: {"text": i.model},
|
||||
"Item Type": lambda i: {"text": i.item.description} if i.item else {"text": None},
|
||||
"Shared?": lambda i: {"text": i.shared, "type": "bool", "html": checked_box if i.shared else unchecked_box},
|
||||
"Owner": lambda i: {"text": i.owner.full_name, "url": url_for("main.user", id=i.owner.id)} if i.owner else {"text": None},
|
||||
"Location": lambda i: {"text": i.location.full_name} if i.location else {"Text": None},
|
||||
"Owner": lambda i: {"text": i.owner.identifier, "url": url_for("main.user", id=i.owner.id)} if i.owner else {"text": None},
|
||||
"Location": lambda i: {"text": i.location.identifier} if i.location else {"Text": None},
|
||||
"Condition": lambda i: {"text": i.condition}
|
||||
}
|
||||
|
||||
|
|
@ -52,14 +52,14 @@ FILTER_MAP = {
|
|||
user_headers = {
|
||||
"Last Name": lambda i: {"text": i.last_name},
|
||||
"First Name": lambda i: {"text": i.first_name},
|
||||
"Supervisor": lambda i: {"text": i.supervisor.full_name, "url": url_for("main.user", id=i.supervisor.id)} if i.supervisor else {"text": None},
|
||||
"Location": lambda i: {"text": i.location.full_name} if i.location else {"text": None},
|
||||
"Supervisor": lambda i: {"text": i.supervisor.identifier, "url": url_for("main.user", id=i.supervisor.id)} if i.supervisor else {"text": None},
|
||||
"Location": lambda i: {"text": i.location.identifier} if i.location else {"text": None},
|
||||
"Staff?": lambda i: {"text": i.staff, "type": "bool", "html": checked_box if i.staff else unchecked_box},
|
||||
"Active?": lambda i: {"text": i.active, "type": "bool", "html": checked_box if i.active else unchecked_box}
|
||||
}
|
||||
|
||||
worklog_headers = {
|
||||
"Contact": lambda i: {"text": i.contact.full_name, "url": url_for("main.user", id=i.contact.id)} if i.contact else {"Text": None},
|
||||
"Contact": lambda i: {"text": i.contact.identifier, "url": url_for("main.user", id=i.contact.id)} if i.contact else {"Text": None},
|
||||
"Work Item": lambda i: {"text": i.work_item.identifier, "url": url_for('main.inventory_item',id=i.work_item.id)} if i.work_item else {"text": None},
|
||||
"Start Time": lambda i: {"text": i.start_time.strftime("%Y-%m-%d")},
|
||||
"End Time": lambda i: {"text": i.end_time.strftime("%Y-%m-%d")} if i.end_time else {"text": None},
|
||||
|
|
|
|||
|
|
@ -61,10 +61,10 @@ def index():
|
|||
users = set([log.contact for log in worklog_query if log.contact])
|
||||
work_summary = {}
|
||||
|
||||
for user in sorted(users, key=lambda u: u.full_name):
|
||||
work_summary[user.full_name] = {}
|
||||
work_summary[user.full_name]['active_count'] = len([log for log in worklog_query if log.contact == user and not log.complete])
|
||||
work_summary[user.full_name]['complete_count'] = len([log for log in worklog_query if log.contact == user and log.complete])
|
||||
for user in sorted(users, key=lambda u: u.identifier):
|
||||
work_summary[user.identifier] = {}
|
||||
work_summary[user.identifier]['active_count'] = len([log for log in worklog_query if log.contact == user and not log.complete])
|
||||
work_summary[user.identifier]['complete_count'] = len([log for log in worklog_query if log.contact == user and log.complete])
|
||||
|
||||
datasets['work_summary'] = [{
|
||||
'type': 'bar',
|
||||
|
|
|
|||
|
|
@ -31,11 +31,11 @@ def list_inventory():
|
|||
if filter_by == 'user':
|
||||
if not (user := db.session.query(User).filter(User.id == id).first()):
|
||||
return "Invalid User ID", 400
|
||||
filter_name = user.full_name
|
||||
filter_name = user.identifier
|
||||
elif filter_by == 'location':
|
||||
if not (room := db.session.query(Room).filter(Room.id == id).first()):
|
||||
return "Invalid Location ID", 400
|
||||
filter_name = room.full_name
|
||||
filter_name = room.identifier
|
||||
else:
|
||||
if not (item := db.session.query(Item).filter(Item.id == id).first()):
|
||||
return "Invalid Type ID", 400
|
||||
|
|
@ -49,7 +49,7 @@ def list_inventory():
|
|||
inventory = sorted(inventory, key=lambda i: i.identifier)
|
||||
|
||||
return render_template(
|
||||
'table.html',
|
||||
'table.html',
|
||||
title=f"Inventory Listing ({filter_name})" if filter_by else "Inventory Listing",
|
||||
breadcrumb=[{'label': 'Inventory', 'url': url_for('main.inventory_index')}],
|
||||
header=inventory_headers,
|
||||
|
|
@ -79,11 +79,11 @@ def inventory_index():
|
|||
listing = chunk_list(types, 12)
|
||||
elif category:
|
||||
return f"Dude, why {category}?"
|
||||
|
||||
|
||||
return render_template(
|
||||
'inventory_index.html',
|
||||
title=f"Inventory ({category.capitalize()} Index)" if category else "Inventory",
|
||||
category=category,
|
||||
'inventory_index.html',
|
||||
title=f"Inventory ({category.capitalize()} Index)" if category else "Inventory",
|
||||
category=category,
|
||||
listing=listing
|
||||
)
|
||||
|
||||
|
|
@ -109,14 +109,14 @@ def inventory_item(id):
|
|||
title = f"Inventory Record - {item.identifier}"
|
||||
else:
|
||||
title = "Inventory Record - Not Found"
|
||||
return render_template('error.html',
|
||||
return render_template('error.html',
|
||||
title=title,
|
||||
message=f'Inventory item with id {id} not found!',
|
||||
endpoint='inventory_item',
|
||||
endpoint_args={'id': -1})
|
||||
|
||||
return render_template("inventory.html", title=title, item=item,
|
||||
brands=brands, users=users, rooms=rooms,
|
||||
return render_template("inventory.html", title=title, item=item,
|
||||
brands=brands, users=users, rooms=rooms,
|
||||
worklog=worklog,
|
||||
worklog_headers=filtered_worklog_headers,
|
||||
worklog_rows=[{"id": log.id, "cells": [fn(log) for fn in filtered_worklog_headers.values()]} for log in worklog],
|
||||
|
|
@ -147,7 +147,7 @@ def new_inventory_item():
|
|||
worklog=[],
|
||||
worklog_headers={},
|
||||
worklog_rows=[]
|
||||
)
|
||||
)
|
||||
|
||||
@main.route("/api/inventory", methods=["POST"])
|
||||
def create_inventory_item():
|
||||
|
|
@ -160,11 +160,11 @@ def create_inventory_item():
|
|||
db.session.commit()
|
||||
|
||||
return jsonify({"success": True, "id": new_item.id}), 201
|
||||
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"success": False, "error": str(e)}), 400
|
||||
|
||||
|
||||
@main.route("/api/inventory/<int:id>", methods=["PUT"])
|
||||
def update_inventory_item(id):
|
||||
try:
|
||||
|
|
@ -194,7 +194,7 @@ def update_inventory_item(id):
|
|||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"success": False, "error": str(e)}), 400
|
||||
|
||||
|
||||
@main.route("/api/inventory/<int:id>", methods=["DELETE"])
|
||||
def delete_inventory_item(id):
|
||||
try:
|
||||
|
|
@ -202,16 +202,16 @@ def delete_inventory_item(id):
|
|||
|
||||
if not item:
|
||||
return jsonify({"success": False, "error": f"Item with ID {id} not found"}), 404
|
||||
|
||||
|
||||
db.session.delete(item)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({"success": True}), 200
|
||||
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"success": False, "error": str(e)}), 400
|
||||
|
||||
|
||||
@main.route("/api/inventory/export", methods=["POST"])
|
||||
def get_inventory_csv():
|
||||
def export_value(item, col):
|
||||
|
|
@ -220,24 +220,24 @@ def get_inventory_csv():
|
|||
case "brand":
|
||||
return item.brand.name
|
||||
case "location":
|
||||
return item.location.full_name
|
||||
return item.location.identifier
|
||||
case "owner":
|
||||
return item.owner.full_name
|
||||
return item.owner.identifier
|
||||
case "type":
|
||||
return item.item.description
|
||||
case _:
|
||||
return getattr(item, col, "")
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
|
||||
data = request.get_json()
|
||||
ids = data.get('ids', [])
|
||||
|
||||
if not ids:
|
||||
return jsonify({"success": False, "error": "No IDs provided"}), 400
|
||||
|
||||
|
||||
rows = eager_load_inventory_relationships(db.session.query(Inventory).filter(Inventory.id.in_(ids))).all()
|
||||
|
||||
|
||||
columns = [
|
||||
"id",
|
||||
"timestamp",
|
||||
|
|
@ -259,7 +259,7 @@ def get_inventory_csv():
|
|||
@main.route("/inventory_available")
|
||||
def inventory_available():
|
||||
query = eager_load_inventory_relationships(db.session.query(Inventory).filter(Inventory.condition == "Working"))
|
||||
|
||||
|
||||
inventory = query.all()
|
||||
inventory = sorted(inventory, key=lambda i: i.identifier)
|
||||
|
||||
|
|
|
|||
|
|
@ -40,9 +40,9 @@ def user(id):
|
|||
.filter(Inventory.owner_id == id) # type: ignore
|
||||
.filter(Inventory.condition.in_(ACTIVE_STATUSES))
|
||||
)
|
||||
|
||||
|
||||
inventory = inventory_query.all()
|
||||
filtered_inventory_headers = {k: v for k, v in inventory_headers.items() if k not in ['Date Entered', 'Name', 'Serial Number',
|
||||
filtered_inventory_headers = {k: v for k, v in inventory_headers.items() if k not in ['Date Entered', 'Name', 'Serial Number',
|
||||
'Bar Code', 'Condition', 'Owner', 'Notes',
|
||||
'Brand', 'Model', 'Shared?', 'Location']}
|
||||
worklog_query = eager_load_worklog_relationships(db.session.query(WorkLog)).filter(WorkLog.contact_id == id)
|
||||
|
|
@ -50,7 +50,7 @@ def user(id):
|
|||
filtered_worklog_headers = {k: v for k, v in worklog_headers.items() if k not in ['Contact', 'Follow Up?', 'Quick Analysis?']}
|
||||
|
||||
if user:
|
||||
title = f"User Record - {user.full_name}" if user.active else f"User Record - {user.full_name} (Inactive)"
|
||||
title = f"User Record - {user.identifier}" if user.active else f"User Record - {user.identifier} (Inactive)"
|
||||
else:
|
||||
title = f"User Record - User Not Found"
|
||||
return render_template(
|
||||
|
|
@ -60,9 +60,9 @@ def user(id):
|
|||
)
|
||||
|
||||
return render_template(
|
||||
"user.html",
|
||||
title=title,
|
||||
user=user, users=users, rooms=rooms, assets=inventory,
|
||||
"user.html",
|
||||
title=title,
|
||||
user=user, users=users, rooms=rooms, assets=inventory,
|
||||
inventory_headers=filtered_inventory_headers,
|
||||
inventory_rows=[{"id": item.id, "cells": [fn(item) for fn in filtered_inventory_headers.values()]} for item in inventory],
|
||||
worklog=worklog,
|
||||
|
|
@ -98,7 +98,7 @@ def create_user():
|
|||
db.session.commit()
|
||||
|
||||
return jsonify({"success": True, "id": new_user.id}), 201
|
||||
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"success": False, "error": str(e)}), 400
|
||||
|
|
@ -111,7 +111,7 @@ def update_user(id):
|
|||
|
||||
if not user:
|
||||
return jsonify({"success": False, "error": f"User with ID {id} not found."}), 404
|
||||
|
||||
|
||||
user.staff = bool(data.get("staff", user.staff))
|
||||
user.active = bool(data.get("active", user.active))
|
||||
user.last_name = data.get("last_name", user.last_name)
|
||||
|
|
@ -122,33 +122,33 @@ def update_user(id):
|
|||
db.session.commit()
|
||||
|
||||
return jsonify({"success": True, "id": user.id}), 200
|
||||
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"success": False, "error": str(e)}), 400
|
||||
|
||||
|
||||
@main.route("/api/user/export", methods=["POST"])
|
||||
def get_user_csv():
|
||||
def export_value(user, col):
|
||||
try:
|
||||
match col:
|
||||
case "location":
|
||||
return user.location.full_name
|
||||
return user.location.identifier
|
||||
case "supervisor":
|
||||
return user.supervisor.full_name
|
||||
return user.supervisor.identifier
|
||||
case _:
|
||||
return getattr(user, col, "")
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
|
||||
data = request.get_json()
|
||||
ids = data.get('ids', [])
|
||||
|
||||
if not ids:
|
||||
return jsonify({"success": False, "error": "No IDs provided"}), 400
|
||||
|
||||
|
||||
rows = eager_load_user_relationships(db.session.query(User).filter(User.id.in_(ids))).all()
|
||||
|
||||
|
||||
columns = [
|
||||
"id",
|
||||
"staff",
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ from ..utils.load import eager_load_worklog_relationships, eager_load_user_relat
|
|||
|
||||
@main.route("/worklog")
|
||||
def list_worklog():
|
||||
query = eager_load_worklog_relationships(db.session.query(WorkLog))
|
||||
query = eager_load_worklog_relationships(db.session.query(WorkLog))
|
||||
return render_template(
|
||||
'table.html',
|
||||
header=worklog_headers,
|
||||
|
|
@ -48,10 +48,10 @@ def worklog_entry(id):
|
|||
)
|
||||
|
||||
return render_template(
|
||||
"worklog.html",
|
||||
title=title,
|
||||
log=log,
|
||||
users=users,
|
||||
"worklog.html",
|
||||
title=title,
|
||||
log=log,
|
||||
users=users,
|
||||
items=items
|
||||
)
|
||||
|
||||
|
|
@ -86,7 +86,7 @@ def create_worklog():
|
|||
db.session.commit()
|
||||
|
||||
return jsonify({"success": True, "id": new_worklog.id}), 201
|
||||
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"success": False, "error": str(e)}), 400
|
||||
|
|
@ -110,7 +110,7 @@ def update_worklog(id):
|
|||
existing = {str(note.id): note for note in log.updates}
|
||||
incoming = data.get("updates", [])
|
||||
new_updates = []
|
||||
|
||||
|
||||
for note_data in incoming:
|
||||
if isinstance(note_data, dict):
|
||||
if "id" in note_data and str(note_data["id"]) in existing:
|
||||
|
|
@ -119,13 +119,13 @@ def update_worklog(id):
|
|||
new_updates.append(note)
|
||||
elif "content" in note_data:
|
||||
new_updates.append(WorkNote(content=note_data["content"]))
|
||||
|
||||
|
||||
log.updates[:] = new_updates # This replaces in-place
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({"success": True, "id": log.id}), 200
|
||||
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"success": False, "error": str(e)}), 400
|
||||
|
|
@ -137,23 +137,23 @@ def delete_worklog(id):
|
|||
|
||||
if not log:
|
||||
return jsonify({"success": False, "errpr": f"Item with ID {id} not found!"}), 404
|
||||
|
||||
|
||||
db.session.delete(log)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({"success": True}), 200
|
||||
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"success": False, "error": str(e)}), 400
|
||||
|
||||
|
||||
@main.route("/api/worklog/export", methods=["POST"])
|
||||
def get_worklog_csv():
|
||||
def export_value(log, col):
|
||||
try:
|
||||
match col:
|
||||
case "contact":
|
||||
return log.contact.full_name
|
||||
return log.contact.identifier
|
||||
case "work_item":
|
||||
return log.work_item.identifier
|
||||
case "latest_update":
|
||||
|
|
@ -164,15 +164,15 @@ def get_worklog_csv():
|
|||
return getattr(log, col, "")
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
|
||||
data = request.get_json()
|
||||
ids = data.get('ids', [])
|
||||
|
||||
if not ids:
|
||||
return jsonify({"success": False, "error": "No IDs provided"}), 400
|
||||
|
||||
|
||||
rows = eager_load_worklog_relationships(db.session.query(WorkLog).filter(WorkLog.id.in_(ids))).all()
|
||||
|
||||
|
||||
columns = [
|
||||
"id",
|
||||
"start_time",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue