Skip to content

Commit

Permalink
Merge pull request #363 from ropable/master
Browse files Browse the repository at this point in the history
Bugfix: place try-except blocks around search result iterations
  • Loading branch information
ropable authored Jun 18, 2024
2 parents 7ba2a70 + ef38e29 commit 73b4190
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 61 deletions.
4 changes: 3 additions & 1 deletion prs2/referral/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,15 @@ def test_cancel(self):
def test_post(self):
"""Test that updating a referral actually changes it
"""
# Use referral type without avoid additional form validation.
ref_type = ReferralType.objects.get(slug='clearing-permit-dwer')
resp = self.client.post(
self.url,
{
'referring_org': self.ref.referring_org.pk,
'reference': 'New reference value',
'referral_date': '21/12/2022',
'type': self.ref.type.pk,
'type': ref_type.pk,
'regions': [Region.objects.first().pk],
'save': 'Save',
},
Expand Down
137 changes: 77 additions & 60 deletions prs2/referral/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,93 +284,110 @@ def get_context_data(self, **kwargs):
# This is REALLY AWKWARD in this instance, as the highlight(s) will be
# for some random `field_name`. So the next line just dumps out the FIRST
# element in the `highlight` dict for usage below (we only need one).
highlight = next(iter(hit["highlight"].values()))
ref = Referral.objects.get(pk=hit["document"]["id"])
referrals[ref.pk] = {
"referral": ref,
"highlight": highlight["snippet"],
"records": [],
"notes": [],
"tasks": [],
"conditions": [],
}

# Records
search_q["query_by"] = "name,description,file_name,file_content"
search_result = client.collections["records"].documents.search(search_q)
context["records_count"] = search_result["found"]
for hit in search_result["hits"]:
highlight = next(iter(hit["highlight"].values()))
ref = Referral.objects.get(pk=hit["document"]["referral_id"])
if ref.pk in referrals:
referrals[ref.pk]["records"].append((hit["document"]["id"], highlight["snippet"]))
else:
# Very occasionally, Typesense returns a blank dictionary for `highlight`
# that causes a StopIteration exception (hence the try-except).
try:
highlight = next(iter(hit["highlight"].values()))
ref = Referral.objects.get(pk=hit["document"]["id"])
referrals[ref.pk] = {
"referral": ref,
"highlight": {},
"records": [(hit["document"]["id"], highlight["snippet"])],
"highlight": highlight["snippet"],
"records": [],
"notes": [],
"tasks": [],
"conditions": [],
}
except:
pass

# Records
search_q["query_by"] = "name,description,file_name,file_content"
search_result = client.collections["records"].documents.search(search_q)
context["records_count"] = search_result["found"]
for hit in search_result["hits"]:
try:
highlight = next(iter(hit["highlight"].values()))
ref = Referral.objects.get(pk=hit["document"]["referral_id"])
if ref.pk in referrals:
referrals[ref.pk]["records"].append((hit["document"]["id"], highlight["snippet"]))
else:
referrals[ref.pk] = {
"referral": ref,
"highlight": {},
"records": [(hit["document"]["id"], highlight["snippet"])],
"notes": [],
"tasks": [],
"conditions": [],
}
except:
pass

# Notes
search_q["query_by"] = "note"
search_result = client.collections["notes"].documents.search(search_q)
context["notes_count"] = search_result["found"]
for hit in search_result["hits"]:
highlight = next(iter(hit["highlight"].values()))
ref = Referral.objects.get(pk=hit["document"]["referral_id"])
if ref.pk in referrals:
referrals[ref.pk]["notes"].append((hit["document"]["id"], highlight["snippet"]))
else:
referrals[ref.pk] = {
"referral": ref,
"highlight": {},
"records": [],
"notes": [(hit["document"]["id"], highlight["snippet"])],
"tasks": [],
"conditions": [],
}
try:
highlight = next(iter(hit["highlight"].values()))
ref = Referral.objects.get(pk=hit["document"]["referral_id"])
if ref.pk in referrals:
referrals[ref.pk]["notes"].append((hit["document"]["id"], highlight["snippet"]))
else:
referrals[ref.pk] = {
"referral": ref,
"highlight": {},
"records": [],
"notes": [(hit["document"]["id"], highlight["snippet"])],
"tasks": [],
"conditions": [],
}
except:
pass

# Tasks
search_q["query_by"] = "description,assigned_user"
search_result = client.collections["tasks"].documents.search(search_q)
context["tasks_count"] = search_result["found"]
for hit in search_result["hits"]:
highlight = next(iter(hit["highlight"].values()))
ref = Referral.objects.get(pk=hit["document"]["referral_id"])
if ref.pk in referrals:
referrals[ref.pk]["tasks"].append((hit["document"]["id"], highlight["snippet"]))
else:
referrals[ref.pk] = {
"referral": ref,
"highlight": {},
"records": [],
"notes": [],
"tasks": [(hit["document"]["id"], highlight["snippet"])],
"conditions": [],
}

# Conditions
search_q["query_by"] = "proposed_condition,approved_condition"
search_result = client.collections["conditions"].documents.search(search_q)
context["conditions_count"] = search_result["found"]
for hit in search_result["hits"]:
if "referral_id" in hit["document"]:
ref = Referral.objects.get(pk=hit["document"]["referral_id"])
try:
highlight = next(iter(hit["highlight"].values()))
ref = Referral.objects.get(pk=hit["document"]["referral_id"])
if ref.pk in referrals:
referrals[ref.pk]["conditions"].append((hit["document"]["id"], highlight["snippet"]))
referrals[ref.pk]["tasks"].append((hit["document"]["id"], highlight["snippet"]))
else:
referrals[ref.pk] = {
"referral": ref,
"highlight": {},
"records": [],
"notes": [],
"tasks": [],
"conditions": [(hit["document"]["id"], highlight["snippet"])],
"tasks": [(hit["document"]["id"], highlight["snippet"])],
"conditions": [],
}
except:
pass

# Conditions
search_q["query_by"] = "proposed_condition,approved_condition"
search_result = client.collections["conditions"].documents.search(search_q)
context["conditions_count"] = search_result["found"]
for hit in search_result["hits"]:
try:
if "referral_id" in hit["document"]:
ref = Referral.objects.get(pk=hit["document"]["referral_id"])
highlight = next(iter(hit["highlight"].values()))
if ref.pk in referrals:
referrals[ref.pk]["conditions"].append((hit["document"]["id"], highlight["snippet"]))
else:
referrals[ref.pk] = {
"referral": ref,
"highlight": {},
"records": [],
"notes": [],
"tasks": [],
"conditions": [(hit["document"]["id"], highlight["snippet"])],
}
except:
pass

# Combine the results into the template context (sort referrals by descending ID).
for result in sorted(referrals.items(), reverse=True):
Expand Down

0 comments on commit 73b4190

Please sign in to comment.