Klaus Demo ~jonashaag/klaus / af63b21
Add search to repo list Jonas Haag 13 days ago
3 changed file(s) with 60 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
55
66 <h2>
77 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>
158 </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
1624 <ul class=repolist>
1725 {% for repo in repos %}
1826 {% set last_updated_at = repo.fast_get_last_updated_at() %}
3644 </div>
3745 </a>
3846 </li>
47 {% else %}
48 <li><div class=name>No repositories to show!</div></li>
3949 {% endfor %}
4050 </ul>
4151
4646
4747
4848 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":
5263 sort_key = lambda repo: repo.name
5364 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
6273 return render_template(
6374 "repo_list.html",
6475 repos=repos,
6576 invalid_repos=invalid_repos,
6677 order_by=order_by,
78 search_query=search_query,
6779 base_href=None,
6880 )
6981
22 import tarfile
33 import contextlib
44 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
525
626
727 def test_download():