Add search to repo list
Jonas Haag
4 months ago
5 | 5 |
|
6 | 6 |
<h2>
|
7 | 7 |
Repositories
|
8 | |
<span>
|
9 | |
{% if order_by == 'name' %}
|
10 | |
(<a href="?">order by last updated</a>)
|
11 | |
{% else %}
|
12 | |
(<a href="?by-name=yep">order by name</a>)
|
13 | |
{% endif %}
|
14 | |
</span>
|
15 | 8 |
</h2>
|
|
9 |
|
|
10 |
<form>
|
|
11 |
<div>
|
|
12 |
Order by:
|
|
13 |
<input type="radio" id="order_by_last_updated" name="order_by" value="last_updated" onChange="this.form.submit()" {% if order_by == "last_updated" %} checked {% endif %}>
|
|
14 |
<label for="order_by_last_updated">Last updated</label>
|
|
15 |
<input type="radio" id="order_by_name" name="order_by" value="name" onChange="this.form.submit()" {% if order_by == "name" %} checked {% endif %}>
|
|
16 |
<label for="order_by_name">Name</label>
|
|
17 |
</div>
|
|
18 |
<br>
|
|
19 |
<label for="search_query">Search repos:</label>
|
|
20 |
<input type="search" id="search_query" name="q" value="{{ search_query|default }}">
|
|
21 |
<button type="submit">Search</button>
|
|
22 |
</form>
|
|
23 |
|
16 | 24 |
<ul class=repolist>
|
17 | 25 |
{% for repo in repos %}
|
18 | 26 |
{% set last_updated_at = repo.fast_get_last_updated_at() %}
|
|
36 | 44 |
</div>
|
37 | 45 |
</a>
|
38 | 46 |
</li>
|
|
47 |
{% else %}
|
|
48 |
<li><div class=name>No repositories to show!</div></li>
|
39 | 49 |
{% endfor %}
|
40 | 50 |
</ul>
|
41 | 51 |
|
46 | 46 |
|
47 | 47 |
|
48 | 48 |
def repo_list():
|
49 | |
"""Show a list of all repos and can be sorted by last update."""
|
50 | |
if "by-name" in request.args:
|
51 | |
order_by = "name"
|
|
49 |
"""Show a list of all repos. Can be sorted by last update and repo names can be searched."""
|
|
50 |
repos = [repo.freeze() for repo in current_app.valid_repos.values()]
|
|
51 |
invalid_repos = current_app.invalid_repos.values()
|
|
52 |
|
|
53 |
order_by = request.args.get("order_by") or "last_updated"
|
|
54 |
search_query = request.args.get("q") or ""
|
|
55 |
|
|
56 |
if search_query:
|
|
57 |
repos = [r for r in repos if search_query.lower() in r.name.lower()]
|
|
58 |
invalid_repos = [
|
|
59 |
r for r in invalid_repos if search_query.lower() in r.name.lower()
|
|
60 |
]
|
|
61 |
|
|
62 |
if order_by == "name":
|
52 | 63 |
sort_key = lambda repo: repo.name
|
53 | 64 |
else:
|
54 | |
order_by = "last_updated"
|
55 | |
sort_key = lambda repo: (-(repo.fast_get_last_updated_at() or -1), repo.name)
|
56 | |
repos = sorted(
|
57 | |
[repo.freeze() for repo in current_app.valid_repos.values()], key=sort_key
|
58 | |
)
|
59 | |
invalid_repos = sorted(
|
60 | |
current_app.invalid_repos.values(), key=lambda repo: repo.name
|
61 | |
)
|
|
65 |
sort_key = lambda repo: (
|
|
66 |
-(repo.fast_get_last_updated_at() or -1),
|
|
67 |
repo.name,
|
|
68 |
)
|
|
69 |
|
|
70 |
repos = sorted(repos, key=sort_key)
|
|
71 |
invalid_repos = sorted(invalid_repos, key=lambda repo: repo.name)
|
|
72 |
|
62 | 73 |
return render_template(
|
63 | 74 |
"repo_list.html",
|
64 | 75 |
repos=repos,
|
65 | 76 |
invalid_repos=invalid_repos,
|
66 | 77 |
order_by=order_by,
|
|
78 |
search_query=search_query,
|
67 | 79 |
base_href=None,
|
68 | 80 |
)
|
69 | 81 |
|
2 | 2 |
import tarfile
|
3 | 3 |
import contextlib
|
4 | 4 |
from .utils import *
|
|
5 |
|
|
6 |
|
|
7 |
def test_repo_list():
|
|
8 |
with serve():
|
|
9 |
response = requests.get(UNAUTH_TEST_SERVER).text
|
|
10 |
assert TEST_REPO_BASE_URL in response
|
|
11 |
assert TEST_REPO_DONT_RENDER_BASE_URL in response
|
|
12 |
assert TEST_REPO_NO_NEWLINE_BASE_URL in response
|
|
13 |
assert TEST_INVALID_REPO_NAME in response
|
|
14 |
|
|
15 |
|
|
16 |
def test_repo_list_search_repo():
|
|
17 |
with serve():
|
|
18 |
response = requests.get(
|
|
19 |
UNAUTH_TEST_SERVER + "?q=" + TEST_INVALID_REPO_NAME
|
|
20 |
).text
|
|
21 |
assert not TEST_REPO_BASE_URL in response
|
|
22 |
assert not TEST_REPO_DONT_RENDER_BASE_URL in response
|
|
23 |
assert not TEST_REPO_NO_NEWLINE_BASE_URL in response
|
|
24 |
assert TEST_INVALID_REPO_NAME in response
|
5 | 25 |
|
6 | 26 |
|
7 | 27 |
def test_download():
|