From e7b3084622a231284c4bb4dc9b733f962f474245 Mon Sep 17 00:00:00 2001 From: Connor Rhodes Date: Wed, 20 May 2026 23:24:40 +0000 Subject: [PATCH] add update command to script --- log-work-expense/scripts/log_expense.py | 51 +++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/log-work-expense/scripts/log_expense.py b/log-work-expense/scripts/log_expense.py index 0acc618..d453600 100644 --- a/log-work-expense/scripts/log_expense.py +++ b/log-work-expense/scripts/log_expense.py @@ -2,16 +2,20 @@ """Log work expenses to MongoDB (wip.work_expenses). Usage: - python log_expense.py [s2_url_2] [--route stop1,stop2,...] + python log_expense.py [s2_url_2] [--route stop1|stop2] + python log_expense.py update [--account X] [--note X] [--route stop1|stop2] type: "meal" or "mileage" account: e.g. "LTISD" date: YYYY-MM-DD note: free-text context (quote if it has spaces) s2_url: one or more S2 URLs of uploaded files - --route: comma-separated list of stops (mileage only). Home address is + --route: pipe-separated list of stops (mileage only). Home address is automatically prepended and appended. + update: Modify an existing entry by doc ID. Accepts --account, --note, + and/or --route. + Rules enforced: - meal: exactly 1 file per invocation - mileage: exactly 2 files per invocation (start + end odometer) @@ -19,6 +23,7 @@ Rules enforced: import sys from datetime import datetime +from bson import ObjectId from pymongo import MongoClient HOME = "11600 Minda Cir Austin, TX 78758" @@ -27,10 +32,48 @@ ALIASES = { "aus": "3201 Presidential Blvd, Austin, TX 78719", } +MONGO_URI = "mongodb://root:3wwfoUjyk2E2zWELXFlLuHqfw1ALlOp4pb2H5Vq3TImbMIHL2h1u8Jej2mjzCPl@docdb.connorrhodes.com:35563?tls=true&tlsAllowInvalidCertificates=true" + + +def handle_update(args): + if not args: + print("Usage: log_expense.py update [--account X] [--note X] [--route stop1|stop2]") + sys.exit(1) + doc_id = args.pop(0) + updates = {} + i = 0 + while i < len(args): + if args[i] == "--account" and i + 1 < len(args): + updates["account"] = args[i + 1]; i += 2 + elif args[i] == "--note" and i + 1 < len(args): + updates["note"] = args[i + 1]; i += 2 + elif args[i] == "--route" and i + 1 < len(args): + raw = [s.strip() for s in args[i + 1].split("|") if s.strip()] + raw = [ALIASES.get(s.lower(), s) for s in raw] + updates["route"] = [HOME] + raw + [HOME] + i += 2 + else: + print(f"Error: unknown argument '{args[i]}'"); sys.exit(1) + if not updates: + print("Error: no fields to update"); sys.exit(1) + client = MongoClient(MONGO_URI) + result = client["wip"]["work_expenses"].update_one({"_id": ObjectId(doc_id)}, {"$set": updates}) + if result.matched_count: + print(f"Updated {doc_id}: {list(updates.keys())}") + else: + print(f"Error: document {doc_id} not found") + + def main(): args = sys.argv[1:] - route_stops = None + # Handle update subcommand first + if args and args[0] == "update": + handle_update(args[1:]) + return + + # Parse --route for insert + route_stops = None if "--route" in args: idx = args.index("--route") args.pop(idx) @@ -68,7 +111,7 @@ def main(): print("Error: --route is only valid for mileage entries") sys.exit(1) - client = MongoClient("mongodb://root:3wwfoUjyk2E2zWELXFlLuHqfw1ALlOp4pb2H5Vq3TImbMIHL2h1u8Jej2mjzCPl@docdb.connorrhodes.com:35563?tls=true&tlsAllowInvalidCertificates=true") + client = MongoClient(MONGO_URI) db = client["wip"] col = db["work_expenses"]