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