mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-26 01:46:18 -05:00
Adding search icon button to navbar.
This commit is contained in:
parent
81419b8353
commit
377c69e8a7
13 changed files with 222 additions and 86 deletions
|
|
@ -12,6 +12,7 @@ from wrolpi.downloader import download_manager
|
|||
from wrolpi.events import Events
|
||||
from wrolpi.root_api import get_blueprint, json_response
|
||||
from . import lib, schema
|
||||
from .models import Zims
|
||||
|
||||
bp = get_blueprint('Zim', '/api/zim')
|
||||
|
||||
|
|
@ -158,3 +159,36 @@ async def delete_outdated_zims(_: Request):
|
|||
lib.flag_outdated_zim_files()
|
||||
Events.send_deleted(f'Deleted {deleted_count} outdated Zims')
|
||||
return response.empty(HTTPStatus.NO_CONTENT)
|
||||
|
||||
|
||||
@bp.post('/search_estimates')
|
||||
@validate(json=schema.SearchEstimateRequest)
|
||||
async def post_search_estimates(_: Request, body: schema.SearchEstimateRequest):
|
||||
"""Get an estimated count of FileGroups/Zims which may or may not have been tagged."""
|
||||
|
||||
if not body.search_str and not body.tag_names:
|
||||
return response.empty(HTTPStatus.BAD_REQUEST)
|
||||
|
||||
if body.tag_names:
|
||||
# Get actual count of entries tagged with the tag names.
|
||||
zims_estimates = list()
|
||||
for zim, count in Zims.entries_with_tags(body.tag_names).items():
|
||||
d = dict(
|
||||
estimate=count,
|
||||
**zim.__json__(),
|
||||
)
|
||||
zims_estimates.append(d)
|
||||
else:
|
||||
# Get estimates using libzim.
|
||||
zims_estimates = list()
|
||||
for zim, estimate in Zims.estimate(body.search_str).items():
|
||||
d = dict(
|
||||
estimate=estimate,
|
||||
**zim.__json__(),
|
||||
)
|
||||
zims_estimates.append(d)
|
||||
|
||||
ret = dict(
|
||||
zims_estimates=zims_estimates
|
||||
)
|
||||
return json_response(ret)
|
||||
|
|
|
|||
|
|
@ -93,3 +93,9 @@ class ZimSubscribeRequest:
|
|||
class OutdatedZims:
|
||||
outdated: List[str]
|
||||
current: List[str]
|
||||
|
||||
|
||||
@dataclass
|
||||
class SearchEstimateRequest:
|
||||
search_str: Optional[str] = None
|
||||
tag_names: List[str] = field(default_factory=lambda: list())
|
||||
|
|
|
|||
|
|
@ -218,3 +218,15 @@ async def test_find_outdated_zim_files(test_session, test_directory, test_zim_by
|
|||
assert wikipedia_two.is_file()
|
||||
assert asdf.is_file()
|
||||
assert empty.is_file()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_estimates(test_session, test_async_client, zim_factory):
|
||||
zim_factory('test zim')
|
||||
test_session.commit()
|
||||
|
||||
body = dict(search_str='one')
|
||||
request, response = await test_async_client.post('/api/zim/search_estimates', json=body)
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert len(response.json['zims_estimates']) == 1
|
||||
assert response.json['zims_estimates'][0]['estimate'] == 2
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue