From fcda282c2ec55212edfb1b770e52108956ed1ab4 Mon Sep 17 00:00:00 2001 From: Ashley Felton Date: Tue, 18 Jun 2024 09:33:37 +0800 Subject: [PATCH 1/2] Bugfix sometime-failing unit test (additional business logic). --- prs2/referral/test_views.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/prs2/referral/test_views.py b/prs2/referral/test_views.py index 83f808b9..5a4cdd8a 100644 --- a/prs2/referral/test_views.py +++ b/prs2/referral/test_views.py @@ -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', }, From ef38e29efd60f5d043effab6f2467a2d063fb5d6 Mon Sep 17 00:00:00 2001 From: Ashley Felton Date: Tue, 18 Jun 2024 10:00:26 +0800 Subject: [PATCH 2/2] Place try-except blocks around search result iterations (edge case exception for Typesense response). --- prs2/referral/views.py | 137 +++++++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 60 deletions(-) diff --git a/prs2/referral/views.py b/prs2/referral/views.py index fd7e1276..ede3d78c 100644 --- a/prs2/referral/views.py +++ b/prs2/referral/views.py @@ -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):