Skip to content

Commit

Permalink
Merge pull request #362 from ropable/master
Browse files Browse the repository at this point in the history
Bugfix for combined search view
  • Loading branch information
ropable authored Jun 18, 2024
2 parents 47579a3 + 0c73565 commit 7ba2a70
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/image-build-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
# Build and push Docker image (not on PR)
#----------------------------------------------
- name: Build and push Docker image
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
Expand Down
2 changes: 1 addition & 1 deletion kustomize/overlays/prod/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ patches:
- path: geoserver_service_patch.yaml
images:
- name: ghcr.io/dbca-wa/prs
newTag: 2.5.47
newTag: 2.5.48
22 changes: 11 additions & 11 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion prs2/referral/templates/referral/prs_index_search.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h1>{{ page_heading }}</h1>
Search matches:
<ul>
{% for highlight in result.highlights %}
<li>{{ highlight.field|capfirst }}: {{ highlight.snippet|safe }}</li>
<li>{{ highlight.0|capfirst }}: {{ highlight.1|safe }}</li>
{% endfor %}
</ul>
</td>
Expand Down
22 changes: 10 additions & 12 deletions prs2/referral/templates/referral/prs_index_search_combined.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,31 @@ <h1>{{ page_heading }}</h1>
<td colspan="{{ referral_headers|length }}">
Search matches:
<ul>
{% if result.highlights %}
{% for highlight in result.highlights %}
<li>{{ highlight.field|capfirst }}: {{ highlight.snippet|safe }}</li>
{% endfor %}
{% if result.highlight %}
<li>{{ result.highlight|safe }}</li>
{% endif %}

{% if result.records %}
{% for highlight in result.records %}
<li><a href="{% url 'prs_object_detail' model='records' pk=highlight.0 %}">Record {{ highlight.0 }}</a>: {{ highlight.1.snippet|safe }}</li>
{% for record in result.records %}
<li><a href="{% url 'prs_object_detail' model='records' pk=record.0 %}">Record {{ record.0 }}</a>: {{ record.1|safe }}</li>
{% endfor %}
{% endif %}

{% if result.notes %}
{% for highlight in result.notes %}
<li><a href="{% url 'prs_object_detail' model='notes' pk=highlight.0 %}">Note {{ highlight.0 }}</a>: {{ highlight.1.snippet|safe }}</li>
{% for note in result.notes %}
<li><a href="{% url 'prs_object_detail' model='notes' pk=note.0 %}">Note {{ note.0 }}</a>: {{ note.1|safe }}</li>
{% endfor %}
{% endif %}

{% if result.tasks %}
{% for highlight in result.tasks %}
<li><a href="{% url 'prs_object_detail' model='tasks' pk=highlight.0 %}">Task {{ highlight.0 }}</a>: {{ highlight.1.snippet|safe }}</li>
{% for task in result.tasks %}
<li><a href="{% url 'prs_object_detail' model='tasks' pk=task.0 %}">Task {{ task.0 }}</a>: {{ task.1|safe }}</li>
{% endfor %}
{% endif %}

{% if result.conditions %}
{% for highlight in result.conditions %}
<li><a href="{% url 'prs_object_detail' model='conditions' pk=highlight.0 %}">Condition {{ highlight.0 }}</a>: {{ highlight.1.snippet|safe }}</li>
{% for condition in result.conditions %}
<li><a href="{% url 'prs_object_detail' model='conditions' pk=condition.0 %}">Condition {{ condition.0 }}</a>: {{ condition.1|safe }}</li>
{% endfor %}
{% endif %}
</ul>
Expand Down
79 changes: 53 additions & 26 deletions prs2/referral/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,43 +189,56 @@ def get_context_data(self, **kwargs):
paginator = Paginator(tuple(_ for _ in range(search_result["found"])), 20)
context["page_obj"] = paginator.get_page(page)

# Replace underscores in search field names with spaces.
for hit in search_result["hits"]:
highlights = []
for highlight in hit["highlights"]:
highlight["field"] = highlight["field"].replace("_", " ")
highlights.append(highlight)
hit["highlights"] = highlights

if collection == "referrals":
for hit in search_result["hits"]:
highlights = []
for key, value in hit["highlight"].items():
# Replace underscores in search field names with spaces.
highlights.append((key.replace("_", " "), value["snippet"]))
hit["highlights"] = highlights
context["search_result"].append({
"object": Referral.objects.get(pk=hit["document"]["id"]),
"highlights": hit["highlights"],
"highlights": highlights,
})
elif collection == "records":
for hit in search_result["hits"]:
highlights = []
for key, value in hit["highlight"].items():
highlights.append((key.replace("_", " "), value["snippet"]))
hit["highlights"] = highlights
context["search_result"].append({
"object": Record.objects.get(pk=hit["document"]["id"]),
"highlights": hit["highlights"],
"highlights": highlights,
})
elif collection == "notes":
for hit in search_result["hits"]:
highlights = []
for key, value in hit["highlight"].items():
highlights.append((key.replace("_", " "), value["snippet"]))
hit["highlights"] = highlights
context["search_result"].append({
"object": Note.objects.get(pk=hit["document"]["id"]),
"highlights": hit["highlights"],
"highlights": highlights,
})
elif collection == "tasks":
for hit in search_result["hits"]:
highlights = []
for key, value in hit["highlight"].items():
highlights.append((key.replace("_", " "), value["snippet"]))
hit["highlights"] = highlights
context["search_result"].append({
"object": Task.objects.get(pk=hit["document"]["id"]),
"highlights": hit["highlights"],
"highlights": highlights,
})
elif collection == "conditions":
for hit in search_result["hits"]:
highlights = []
for key, value in hit["highlight"].items():
highlights.append((key.replace("_", " "), value["snippet"]))
hit["highlights"] = highlights
context["search_result"].append({
"object": Condition.objects.get(pk=hit["document"]["id"]),
"highlights": hit["highlights"],
"highlights": highlights,
})

return context
Expand Down Expand Up @@ -262,10 +275,20 @@ def get_context_data(self, **kwargs):
search_result = client.collections["referrals"].documents.search(search_q)
context["referrals_count"] = search_result["found"]
for hit in search_result["hits"]:
# Explanation for the line below: the Typesense API search response
# returns a list of document resources, each containing a `highlight` key
# that consists of a dict which contains 1+ `<field_name>` keys, each of which
# consists of a dict containing the text snippet and matched tokens.
# Earlier (<0.25) versions of the API returned `highlights` as a list instead
# of `highlight` as this dict.
# 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,
"highlights": hit["highlights"],
"highlight": highlight["snippet"],
"records": [],
"notes": [],
"tasks": [],
Expand All @@ -277,14 +300,15 @@ def get_context_data(self, **kwargs):
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"], hit["highlights"][0]))
referrals[ref.pk]["records"].append((hit["document"]["id"], highlight["snippet"]))
else:
referrals[ref.pk] = {
"referral": ref,
"highlights": [],
"records": [(hit["document"]["id"], hit["highlights"][0])],
"highlight": {},
"records": [(hit["document"]["id"], highlight["snippet"])],
"notes": [],
"tasks": [],
"conditions": [],
Expand All @@ -295,15 +319,16 @@ def get_context_data(self, **kwargs):
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"], hit["highlights"][0]))
referrals[ref.pk]["notes"].append((hit["document"]["id"], highlight["snippet"]))
else:
referrals[ref.pk] = {
"referral": ref,
"highlights": [],
"highlight": {},
"records": [],
"notes": [(hit["document"]["id"], hit["highlights"][0])],
"notes": [(hit["document"]["id"], highlight["snippet"])],
"tasks": [],
"conditions": [],
}
Expand All @@ -313,16 +338,17 @@ def get_context_data(self, **kwargs):
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"], hit["highlights"][0]))
referrals[ref.pk]["tasks"].append((hit["document"]["id"], highlight["snippet"]))
else:
referrals[ref.pk] = {
"referral": ref,
"highlights": [],
"highlight": {},
"records": [],
"notes": [],
"tasks": [(hit["document"]["id"], hit["highlights"][0])],
"tasks": [(hit["document"]["id"], highlight["snippet"])],
"conditions": [],
}

Expand All @@ -333,16 +359,17 @@ def get_context_data(self, **kwargs):
for hit in search_result["hits"]:
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"], hit["highlights"][0]))
referrals[ref.pk]["conditions"].append((hit["document"]["id"], highlight["snippet"]))
else:
referrals[ref.pk] = {
"referral": ref,
"highlights": [],
"highlight": {},
"records": [],
"notes": [],
"tasks": [],
"conditions": [(hit["document"]["id"], hit["highlights"][0])],
"conditions": [(hit["document"]["id"], highlight["snippet"])],
}

# Combine the results into the template context (sort referrals by descending ID).
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "prs"
version = "2.5.47"
version = "2.5.48"
description = "Planning Referral System corporate application"
authors = ["Ashley Felton <[email protected]>"]
license = "Apache-2.0"
Expand Down Expand Up @@ -37,7 +37,7 @@ django-crum = "0.7.9"
django-storages = {version = "1.14.3", extras = ["azure"]}
sentry-sdk = {version = "2.5.1", extras = ["django"]}
crispy-bootstrap5 = "2024.2"
redis = "5.0.5"
redis = "5.0.6"
xlsxwriter = "3.2.0"

[tool.poetry.group.dev.dependencies]
Expand Down

0 comments on commit 7ba2a70

Please sign in to comment.