Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/assets/services/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

from django.db import connection
from django.db.models import Case, Q, Value, When
from django.db.models import Case, F, Q, Value, When
from django.db.models.fields import FloatField

MAX_SEARCH_WORDS = 20
Expand Down Expand Up @@ -48,7 +48,7 @@ def _build_fts_search(queryset, words, search_text, icontains_q):

return (
queryset.annotate(
fts_rank=SearchRank("search_vector", search_query),
fts_rank=SearchRank(F("search_vector"), search_query),
barcode_boost=barcode_exact,
)
.filter(combined_filter)
Expand Down
56 changes: 56 additions & 0 deletions src/assets/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5821,6 +5821,34 @@ def test_asset_list_filters_by_category(
assert asset.name in content
assert other_asset.name not in content

def test_text_search_by_category_name(
self, client_logged_in, category, location, user, department
):
"""Searching for a category name returns assets in that category."""
from assets.models import Asset, Category

cat = Category.objects.create(name="Religious", department=department)
matching = Asset.objects.create(
name="Plain Chalice",
category=cat,
current_location=location,
status="active",
created_by=user,
)
unmatched = Asset.objects.create(
name="Wooden Table",
category=category,
current_location=location,
status="active",
created_by=user,
)
url = reverse("assets:asset_list")
response = client_logged_in.get(url, {"q": "Religious"})
assert response.status_code == 200
content = response.content.decode()
assert matching.name in content
assert unmatched.name not in content


# ============================================================
# V213 (S2.6.1-04): Search by location
Expand Down Expand Up @@ -7743,6 +7771,34 @@ def test_export_multi_word_search_excludes_partial_match(
]
assert "Red Bonnet" not in names

def test_export_search_by_category_name(
self, admin_client, location, user, department
):
"""Export with category name query includes matching assets."""
from assets.models import Category

cat = Category.objects.create(name="Religious", department=department)
matching = AssetFactory(
name="Plain Chalice",
category=cat,
current_location=location,
status="active",
created_by=user,
)
url = reverse("assets:export_assets")
resp = admin_client.get(url, {"q": "Religious"})
assert resp.status_code == 200
from io import BytesIO

import openpyxl

wb = openpyxl.load_workbook(BytesIO(resp.content))
ws = wb["Assets"]
names = [
row[0].value for row in ws.iter_rows(min_row=2) if row[0].value
]
assert matching.name in names


# ============================================================
# PR #61 REGRESSION: Query truncation to 200 characters
Expand Down
14 changes: 10 additions & 4 deletions src/assets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ def asset_list(request):
q = request.GET.get("q", "").strip()[:200]
if q:

queryset = build_asset_search(queryset, q, include_nfc=True)
queryset = build_asset_search(
queryset, q, include_nfc=True, include_category=True
)
Comment thread
andrewyager marked this conversation as resolved.

# Filters
department = request.GET.get("department")
Expand Down Expand Up @@ -4188,7 +4190,9 @@ def export_assets(request):
q = request.GET.get("q", "").strip()[:200]
if q:

queryset = build_asset_search(queryset, q, include_nfc=False)
queryset = build_asset_search(
queryset, q, include_nfc=False, include_category=True
)
Comment thread
andrewyager marked this conversation as resolved.

buffer = export_assets_xlsx(queryset)

Expand Down Expand Up @@ -4544,9 +4548,11 @@ def print_all_filtered_labels(request):
if status:
queryset = queryset.filter(status=status)

q = request.GET.get("q", "")
q = request.GET.get("q", "").strip()[:200]
if q:
queryset = build_asset_search(queryset, q, include_nfc=True)
queryset = build_asset_search(
queryset, q, include_nfc=True, include_category=True
)

department = request.GET.get("department")
if department:
Expand Down
Loading