This commit is contained in:
2025-10-31 11:19:49 -07:00
parent d7817dac75
commit 8c31125d5b
2 changed files with 15 additions and 22 deletions

36
app.py
View File

@@ -65,30 +65,22 @@ def get_user_profile(uid: str):
return {"enabled": bool(data.get("enabled", False)), "caseEmail": data.get("caseEmail")} return {"enabled": bool(data.get("enabled", False)), "caseEmail": data.get("caseEmail")}
def fetch_all_projects_for_user(uid: str): def fetch_all_projects():
"""Fetch all projects for a user and cache them""" """Fetch all projects for a user and cache them"""
print("Fetching projects....")
# Get bearer token # Get bearer token
bearer = get_filevine_bearer() bearer = get_filevine_bearer()
# List projects (all pages) # List projects (all pages)
projects = list_all_projects(bearer) projects = list_all_projects(bearer)
# Filter to ProjectEmailAddress == caseEmail
profile = get_user_profile(uid)
case_email = profile.get("caseEmail")
# if case_email:
# filtered = [p for p in projects if str(p.get("ProjectEmailAddress", "")).lower() == str(case_email).lower()]
# else:
# filtered = []
filtered = projects
# Fetch details for each # Fetch details for each
detailed_rows = [] detailed_rows = []
for p in filtered: for p in projects:
pid = (p.get("projectId") or {}).get("native") pid = (p.get("projectId") or {}).get("native")
c = fetch_client(bearer, (p.get("clientId") or {}).get("native")) c = fetch_client(bearer, (p.get("clientId") or {}).get("native"))
cs = fetch_contacts(bearer, pid) cs = fetch_contacts(bearer, pid)
print("CS IS", cs)
if pid is None: if pid is None:
continue continue
@@ -114,6 +106,12 @@ def fetch_all_projects_for_user(uid: str):
project_cache.set_projects(detailed_rows) project_cache.set_projects(detailed_rows)
return detailed_rows return detailed_rows
def async_cache_projects():
from threading import Thread
thread = Thread(target=fetch_all_projects, args=())
thread.daemon = True
thread.start()
# --- Routes --- # --- Routes ---
@app.route("/") @app.route("/")
def index(): def index():
@@ -152,11 +150,7 @@ def session_login():
# Optional: short session # Optional: short session
session["expires_at"] = (datetime.utcnow() + timedelta(hours=8)).isoformat() session["expires_at"] = (datetime.utcnow() + timedelta(hours=8)).isoformat()
# Async update cache after login async_cache_projects()
from threading import Thread
thread = Thread(target=fetch_all_projects_for_user, args=(uid,))
thread.daemon = True
thread.start()
return jsonify({"ok": True}) return jsonify({"ok": True})
except Exception as e: except Exception as e:
@@ -193,7 +187,6 @@ def get_filevine_bearer():
resp = requests.post(url, data=data, headers=headers, timeout=30) resp = requests.post(url, data=data, headers=headers, timeout=30)
resp.raise_for_status() resp.raise_for_status()
js = resp.json() js = resp.json()
print(js)
return js.get("access_token") return js.get("access_token")
@@ -205,7 +198,6 @@ def list_all_projects(bearer: str):
"x-fv-orgid": str(FV_ORG_ID), "x-fv-orgid": str(FV_ORG_ID),
"x-fv-userid": str(FV_USER_ID), "x-fv-userid": str(FV_USER_ID),
} }
print(headers)
results = [] results = []
last_id = None last_id = None
tries = 0 tries = 0
@@ -217,7 +209,6 @@ def list_all_projects(bearer: str):
# Some deployments use LastID/Offset pagination; adapt if needed # Some deployments use LastID/Offset pagination; adapt if needed
params["lastID"] = last_id params["lastID"] = last_id
r = requests.get(url, headers=headers, params=params, timeout=30) r = requests.get(url, headers=headers, params=params, timeout=30)
print(r.content)
r.raise_for_status() r.raise_for_status()
page = r.json() page = r.json()
items = page.get("items", []) items = page.get("items", [])
@@ -229,7 +220,6 @@ def list_all_projects(bearer: str):
# Safety valve # Safety valve
if tries > 200: if tries > 200:
break break
print("RESULTS", results)
return results return results
@@ -290,14 +280,16 @@ def dashboard():
print("USING CACHE") print("USING CACHE")
else: else:
# Fetch and cache projects # Fetch and cache projects
detailed_rows = fetch_all_projects_for_user(uid) detailed_rows = fetch_all_projects()
print("FETCHING") print("FETCHING")
print("HI", len(detailed_rows)) print("HI", len(detailed_rows))
# 5) Render table # 5) Render table
return render_template("dashboard.html", rows=detailed_rows, case_email=case_email) return render_template("dashboard.html", rows=detailed_rows, case_email=case_email)
# GAE compatibility # GAE compatibility
if __name__ == "__main__": if __name__ == "__main__":
async_cache_projects()
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", "5004"))) app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", "5004")))

View File

@@ -23,6 +23,7 @@ class ProjectCache:
return self._cache.copy() return self._cache.copy()
def set_projects(self, projects): def set_projects(self, projects):
print(f"Caching new projects: {len(projects)}")
"""Set projects in cache with current timestamp""" """Set projects in cache with current timestamp"""
with self._lock: with self._lock:
self._cache = projects.copy() if projects else {} self._cache = projects.copy() if projects else {}