add update command to script

This commit is contained in:
Connor Rhodes 2026-05-20 23:24:40 +00:00
parent f473c09a26
commit e7b3084622

View file

@ -2,16 +2,20 @@
"""Log work expenses to MongoDB (wip.work_expenses). """Log work expenses to MongoDB (wip.work_expenses).
Usage: Usage:
python log_expense.py <type> <account> <date> <note> <s2_url_1> [s2_url_2] [--route stop1,stop2,...] python log_expense.py <type> <account> <date> <note> <s2_url_1> [s2_url_2] [--route stop1|stop2]
python log_expense.py update <doc_id> [--account X] [--note X] [--route stop1|stop2]
type: "meal" or "mileage" type: "meal" or "mileage"
account: e.g. "LTISD" account: e.g. "LTISD"
date: YYYY-MM-DD date: YYYY-MM-DD
note: free-text context (quote if it has spaces) note: free-text context (quote if it has spaces)
s2_url: one or more S2 URLs of uploaded files 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. automatically prepended and appended.
update: Modify an existing entry by doc ID. Accepts --account, --note,
and/or --route.
Rules enforced: Rules enforced:
- meal: exactly 1 file per invocation - meal: exactly 1 file per invocation
- mileage: exactly 2 files per invocation (start + end odometer) - mileage: exactly 2 files per invocation (start + end odometer)
@ -19,6 +23,7 @@ Rules enforced:
import sys import sys
from datetime import datetime from datetime import datetime
from bson import ObjectId
from pymongo import MongoClient from pymongo import MongoClient
HOME = "11600 Minda Cir Austin, TX 78758" HOME = "11600 Minda Cir Austin, TX 78758"
@ -27,10 +32,48 @@ ALIASES = {
"aus": "3201 Presidential Blvd, Austin, TX 78719", "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 <doc_id> [--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(): def main():
args = sys.argv[1:] 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: if "--route" in args:
idx = args.index("--route") idx = args.index("--route")
args.pop(idx) args.pop(idx)
@ -68,7 +111,7 @@ def main():
print("Error: --route is only valid for mileage entries") print("Error: --route is only valid for mileage entries")
sys.exit(1) sys.exit(1)
client = MongoClient("mongodb://root:3wwfoUjyk2E2zWELXFlLuHqfw1ALlOp4pb2H5Vq3TImbMIHL2h1u8Jej2mjzCPl@docdb.connorrhodes.com:35563?tls=true&tlsAllowInvalidCertificates=true") client = MongoClient(MONGO_URI)
db = client["wip"] db = client["wip"]
col = db["work_expenses"] col = db["work_expenses"]