Skip to content

Commit 4515e98

Browse files
authored
Merge pull request #20 from simple-repository/feature/exact-search-result
Simplify exact search result handling, and make sure that if the project is in the index, we return the result
2 parents 5a447ec + 851dd42 commit 4515e98

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
lines changed

simple_repository_browser/controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ async def search(self, request: fastapi.Request, query: str, page: int = 1) -> s
109109
# Note: page is 1 based. We don't have a page 0.
110110
page_size = 50
111111
try:
112-
response = self.model.project_query(
112+
response = await self.model.project_query(
113113
query=query, page_size=page_size, page=page
114114
)
115115
except errors.InvalidSearchQuery as e:

simple_repository_browser/model.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import dataclasses
2+
import datetime
13
import itertools
24
import math
35
import sqlite3
@@ -15,18 +17,24 @@
1517
from .short_release_info import ReleaseInfoModel, ShortReleaseInfo
1618

1719

20+
@dataclasses.dataclass(frozen=True)
21+
class SearchResultItem:
22+
canonical_name: str
23+
summary: str | None = None
24+
release_version: str | None = None
25+
release_date: datetime.datetime | None = None
26+
27+
1828
class RepositoryStatsModel(typing.TypedDict):
1929
n_packages: int
2030
n_dist_info: int
2131
n_packages_w_dist_info: int
2232

2333

2434
class QueryResultModel(typing.TypedDict):
25-
exact: tuple[str, str, str, str] | None
2635
search_query: str
27-
results: list[tuple[str, str, str, str]]
36+
results: list[SearchResultItem]
2837
results_count: int # May be more than in the results list (since paginated).
29-
single_name_proposal: str | None
3038
page: int # Note: starts at 1.
3139
n_pages: int
3240

@@ -102,7 +110,9 @@ def _compatibility_matrix(
102110
# Compute the compatibility matrix for the given files.
103111
return compatibility_matrix.compatibility_matrix(files)
104112

105-
def project_query(self, query: str, page_size: int, page: int) -> QueryResultModel:
113+
async def project_query(
114+
self, query: str, page_size: int, page: int
115+
) -> QueryResultModel:
106116
try:
107117
search_terms = _search.parse(query)
108118
except _search.ParseError:
@@ -133,27 +143,34 @@ def project_query(self, query: str, page_size: int, page: int) -> QueryResultMod
133143
f"Requested page (page: {page}) is beyond the number of pages ({n_pages})",
134144
)
135145

136-
if single_name_proposal:
137-
exact = cursor.execute(
138-
"SELECT canonical_name, summary, release_version, release_date FROM projects WHERE canonical_name == ?",
139-
(single_name_proposal,),
140-
).fetchone()
141146
results = cursor.execute(
142147
"SELECT canonical_name, summary, release_version, release_date FROM projects WHERE "
143148
f"{condition_query} LIMIT ? OFFSET ?",
144149
condition_terms + (page_size, offset),
145150
).fetchall()
146151

147-
# Drop the duplicate.
148-
if exact in results:
149-
results.remove(exact)
152+
# Convert results to SearchResultItem objects
153+
results = [SearchResultItem(*result) for result in results]
154+
155+
# Check if single_name_proposal is already in the results
156+
if single_name_proposal and page == 1:
157+
exact_found = any(r.canonical_name == single_name_proposal for r in results)
158+
if not exact_found:
159+
# Not in results, check if it exists in repository
160+
try:
161+
await self.source.get_project_page(single_name_proposal)
162+
# Package exists in repository! Add it to the beginning
163+
results.insert(
164+
0, SearchResultItem(canonical_name=single_name_proposal)
165+
)
166+
n_results += 1
167+
except PackageNotFoundError:
168+
pass
150169

151170
return QueryResultModel(
152-
exact=exact,
153171
search_query=query,
154172
results=results,
155173
results_count=n_results,
156-
single_name_proposal=single_name_proposal,
157174
page=page,
158175
n_pages=n_pages,
159176
)

simple_repository_browser/templates/base/search.html

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
{% macro package_card(content) -%}
12-
<a href="{{ url_for('project', project_name=content[0]) }}">
12+
<a href="{{ url_for('project', project_name=content.canonical_name) }}">
1313
<div class="card" style="margin: 1em;">
1414
<div class="card-body">
1515
<img src="{{ static_file_url('/images/python-logo-only.svg') }}" style="padding: 0 1em 0 0.5em; vertical-align: top; width: 64px;" >
@@ -30,25 +30,11 @@
3030
<!-- <div class="col-md-3"></div>-->
3131
<div class="col-md-9 mx-auto">
3232

33-
{% if exact %}
34-
Exact match found:
35-
{{ package_card(exact) }}
36-
{% endif %}
37-
38-
{% if not exact and single_name_proposal %}
39-
No exact match found. Were you expecting one? If so, visit the
40-
<a href="{{ url_for('project', project_name=single_name_proposal) }}">
41-
"{{ single_name_proposal }}" project page
42-
</a>
43-
to have the index updated automatically.
44-
<br><br>
45-
{% elif results_count == 0 %}
46-
No results found. Consider reducing the number of filters in your query.
47-
{% endif %}
48-
49-
{% if results_count > 0 %}
33+
{% if results_count == 0 %}
34+
No results found.
35+
{% else %}
5036
Found {{ results_count }} results
51-
{% if n_pages > 1%}. Page {{ page }} of {{ n_pages }}:{% endif %}
37+
{% if n_pages > 1%}. Page {{ page }} of {{ n_pages }}:{% endif %}
5238
<br>
5339

5440
{% for result in results %}

0 commit comments

Comments
 (0)