supports lookup by domains
This commit is contained in:
70
app.py
70
app.py
@@ -34,7 +34,7 @@ def projects_for(profile, case_email_match, per_page, offset):
|
||||
Filter projects based on user profile and case_email query string argument.
|
||||
|
||||
Args:
|
||||
profile (dict): User profile containing 'enabled', 'is_admin', and 'case_email' fields
|
||||
profile (dict): User profile containing 'enabled', 'is_admin', 'case_email', and 'case_domain_email' fields
|
||||
case_email_match (str): Case email from query string argument, or None
|
||||
|
||||
Returns:
|
||||
@@ -50,40 +50,52 @@ def projects_for(profile, case_email_match, per_page, offset):
|
||||
cnt = 0
|
||||
if is_admin:
|
||||
if case_email_match:
|
||||
projects_ref = db.collection("projects").where("viewing_emails", "array_contains", case_email_match.lower())
|
||||
# Get filtered document IDs using client-side filtering with partial match
|
||||
z = db.collection("projects").select(["viewing_emails", "matter_description", "id"])
|
||||
# Get all matching documents with their IDs and descriptions
|
||||
matching_docs = [(x.id, x.to_dict().get('matter_description', '')) for x in z.stream()
|
||||
if any(case_email_match.lower() in email.lower() for email in x.to_dict().get('viewing_emails', []))]
|
||||
count = len(matching_docs)
|
||||
case_email_match_lower = case_email_match.lower().strip()
|
||||
|
||||
# Sort by matter_description
|
||||
matching_docs.sort(key=lambda x: x[1].lower())
|
||||
|
||||
# Extract just the IDs after sorting
|
||||
filtered_ids = [doc_id for doc_id, _ in matching_docs]
|
||||
|
||||
# Apply client-side pagination
|
||||
filtered_ids = filtered_ids[offset:offset + per_page]
|
||||
|
||||
print(f"Filtered document IDs (partial match, sorted, paginated): {filtered_ids}")
|
||||
projects_ref = db.collection("projects")
|
||||
projects = []
|
||||
for doc_id in filtered_ids:
|
||||
doc = projects_ref.document(doc_id).get()
|
||||
if doc.exists:
|
||||
# Check if case_email_match is a valid email address (contains @)
|
||||
if '@' in case_email_match_lower and not case_email_match_lower.startswith('@'):
|
||||
# If it's a complete email address, filter by exact match in viewing_emails
|
||||
projects_ref = db.collection("projects").where("viewing_emails", "array_contains", case_email_match_lower)
|
||||
cnt = int(projects_ref.count().get()[0][0].value)
|
||||
projects = []
|
||||
for doc in projects_ref.order_by("matter_description").limit(per_page).offset(offset).stream():
|
||||
projects.append(doc.to_dict())
|
||||
|
||||
return (projects, count)
|
||||
return (projects, cnt)
|
||||
else:
|
||||
# If no @ sign, treat as domain search
|
||||
# Also handle cases like "@gmail.com" by extracting the domain
|
||||
domain_search = case_email_match_lower
|
||||
if domain_search.startswith('@'):
|
||||
domain_search = domain_search[1:] # Remove the @ sign
|
||||
|
||||
# Filter by domain match in viewing_emails
|
||||
projects_ref = db.collection("projects").where("viewing_domains", "array_contains", domain_search)
|
||||
print("HERE domain", domain_search)
|
||||
cnt = int(projects_ref.count().get()[0][0].value)
|
||||
|
||||
projects = []
|
||||
for doc in projects_ref.order_by("matter_description").limit(per_page).offset(offset).stream():
|
||||
projects.append(doc.to_dict())
|
||||
return (projects, cnt)
|
||||
|
||||
else:
|
||||
projects_ref = db.collection("projects")
|
||||
|
||||
else:
|
||||
if not profile.get("case_email"):
|
||||
# For non-admin users, check if they have domain email or specific case email
|
||||
case_domain_email = profile.get("case_domain_email", "")
|
||||
case_email = profile.get("case_email", "")
|
||||
|
||||
if case_domain_email:
|
||||
# Use exact match on viewing_domains field
|
||||
domain_lower = case_domain_email.lower()
|
||||
projects_ref = db.collection("projects").where("viewing_domains", "array_contains", domain_lower)
|
||||
elif case_email:
|
||||
# Use the original logic for specific case email match
|
||||
projects_ref = db.collection("projects").where("viewing_emails", "array_contains", case_email.lower())
|
||||
else:
|
||||
return ([], 0)
|
||||
projects_ref = db.collection("projects").where("viewing_emails", "array_contains", profile.get("case_email").to_lower())
|
||||
|
||||
cnt = int(projects_ref.count().get()[0][0].value)
|
||||
projects = []
|
||||
for doc in projects_ref.order_by("matter_description").limit(per_page).offset(offset).stream():
|
||||
@@ -127,7 +139,7 @@ def index():
|
||||
if not uid:
|
||||
return redirect(url_for("login"))
|
||||
profile = get_user_profile(uid)
|
||||
if profile.get("enabled") and profile.get("case_email"):
|
||||
if profile.get("enabled") and (profile.get("case_email") or profile.get("case_domain_email")):
|
||||
return redirect(url_for("dashboard"))
|
||||
return redirect(url_for("welcome"))
|
||||
|
||||
@@ -197,7 +209,7 @@ def dashboard(page=1):
|
||||
case_email_match = None
|
||||
if is_admin and request.args.get('case_email'):
|
||||
case_email_match = request.args.get('case_email')
|
||||
if not is_admin and not profile.get('case_email'):
|
||||
if not is_admin and (not profile.get('case_email') and not profile.get('case_domain_email')):
|
||||
return redirect(url_for("welcome"))
|
||||
paginated_rows, total_projects = projects_for(profile, case_email_match, per_page, offset)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user