mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-03 05:09:58 -05:00
Adding feature which adds toggles to Zim files to enable/disable auto search.
This commit is contained in:
parent
531ec3d65e
commit
3d6ccad0c0
11 changed files with 192 additions and 21 deletions
|
|
@ -74,14 +74,6 @@ async def search_archives(_: Request, body: schema.ArchiveSearchRequest):
|
|||
return json_response(ret)
|
||||
|
||||
|
||||
@register_switch_handler('singlefile_upload_switch_handler')
|
||||
async def singlefile_upload_switch_handler(singlefile):
|
||||
archive = await lib.singlefile_to_archive(singlefile)
|
||||
logger.info(f'Created Archive from upload: {archive}')
|
||||
name = archive.file_group.title or archive.file_group.url
|
||||
Events.send_archive_uploaded(f'Created Archive from upload: {name}', url=archive.location)
|
||||
|
||||
|
||||
@archive_bp.get('/upload')
|
||||
@openapi.definition(
|
||||
summary='A message to confirm to the user that they have the correct upload URL.'
|
||||
|
|
@ -91,6 +83,23 @@ async def get_upload_singlefile(request: Request):
|
|||
' This requires a POST request.')
|
||||
|
||||
|
||||
@register_switch_handler('singlefile_upload_switch_handler')
|
||||
async def singlefile_upload_switch_handler(singlefile):
|
||||
"""Used by `post_upload_singlefile` to upload a single file"""
|
||||
from wrolpi.downloader import download_manager, Download
|
||||
from . import ArchiveDownloader
|
||||
|
||||
archive = await lib.singlefile_to_archive(singlefile)
|
||||
logger.info(f'Created Archive from upload: {archive}')
|
||||
name = archive.file_group.title or archive.file_group.url
|
||||
Events.send_archive_uploaded(f'Created Archive from upload: {name}', url=archive.location)
|
||||
url = archive.file_group.url
|
||||
if url and (download := Download.get_by_url(url)):
|
||||
if download.is_failed and download.downloader == ArchiveDownloader.name:
|
||||
# Download was attempted and failed, user manually archived the URL.
|
||||
download_manager.delete_download(download.id)
|
||||
|
||||
|
||||
@archive_bp.post('/upload')
|
||||
@openapi.definition(
|
||||
summary='Upload SingleFile from SingleFile browser extension and convert it to an Archive.'
|
||||
|
|
|
|||
|
|
@ -43,6 +43,18 @@ async def delete_zims(_: Request, zim_ids: str):
|
|||
return response.empty()
|
||||
|
||||
|
||||
@zim_bp.post('/<zim_id:[0-9]+>/auto_search')
|
||||
@openapi.definition(
|
||||
summary='Change if a Zim file will be searched by default.',
|
||||
body=schema.ZimAutoSearchRequest,
|
||||
)
|
||||
@validate(schema.ZimAutoSearchRequest)
|
||||
async def post_set_zim_auto_search(_: Request, zim_id: int, body: schema.ZimAutoSearchRequest):
|
||||
zim_id = int(zim_id)
|
||||
lib.set_zim_auto_search(zim_id, body.auto_search)
|
||||
return response.empty()
|
||||
|
||||
|
||||
@zim_bp.post('/search/<zim_id:[0-9]+>')
|
||||
@openapi.definition(
|
||||
summary='Search all entries of a Zim',
|
||||
|
|
|
|||
|
|
@ -572,3 +572,10 @@ async def restart_kiwix():
|
|||
if result.return_code != 0 and result.stderr:
|
||||
logger.debug(result.stderr)
|
||||
return result.return_code
|
||||
|
||||
|
||||
@optional_session
|
||||
def set_zim_auto_search(zim_id: int, auto_search: bool, session: Session = None):
|
||||
zim = Zim.find_by_id(zim_id, session)
|
||||
zim.auto_search = auto_search
|
||||
session.commit()
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@ from datetime import datetime
|
|||
from typing import List, Tuple, OrderedDict as OrderedDictType, Dict, Optional, Set
|
||||
|
||||
from libzim import Archive, Searcher, Query, Entry, SuggestionSearcher
|
||||
from sqlalchemy import Column, Integer, BigInteger, ForeignKey, Text, tuple_
|
||||
from sqlalchemy import Column, Integer, BigInteger, ForeignKey, Text, tuple_, Boolean
|
||||
from sqlalchemy.orm import relationship, Session
|
||||
from sqlalchemy.orm.exc import NoResultFound # noqa
|
||||
|
||||
from modules.zim.errors import UnknownZimEntry, UnknownZimTagEntry
|
||||
from modules.zim.errors import UnknownZimEntry, UnknownZimTagEntry, UnknownZim
|
||||
from wrolpi import dates, tags
|
||||
from wrolpi.common import Base, logger, get_relative_to_media_directory
|
||||
from wrolpi.dates import TZDateTime
|
||||
|
|
@ -72,6 +72,7 @@ class Zim(Base):
|
|||
|
||||
file_group_id = Column(BigInteger, ForeignKey('file_group.id', ondelete='CASCADE'), unique=True, nullable=False)
|
||||
file_group: FileGroup = relationship('FileGroup')
|
||||
auto_search = Column(Boolean, nullable=False, default=True)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Zim id={self.id} file_group_id={self.file_group_id} path={self.path}>'
|
||||
|
|
@ -83,6 +84,7 @@ class Zim(Base):
|
|||
file_group_id=self.file_group_id,
|
||||
metadata=self.zim_metadata,
|
||||
size=self.file_group.size,
|
||||
auto_search=self.auto_search,
|
||||
)
|
||||
return d
|
||||
|
||||
|
|
@ -94,6 +96,37 @@ class Zim(Base):
|
|||
session.flush([file_group, zim])
|
||||
return zim
|
||||
|
||||
@staticmethod
|
||||
@optional_session
|
||||
def get_by_path(path: pathlib.Path, session: Session = None) -> Optional['Zim']:
|
||||
zim = session.query(Zim).filter_by(path=path).one_or_none()
|
||||
if not zim and path.is_file():
|
||||
zim = Zim.from_paths(session, path)
|
||||
session.commit()
|
||||
return zim
|
||||
|
||||
@staticmethod
|
||||
@optional_session
|
||||
def find_by_path(path: pathlib.Path, session: Session = None) -> 'Zim':
|
||||
zim = Zim.get_by_path(path, session=session)
|
||||
if not zim:
|
||||
raise UnknownZim(f'Cannot find zim at path')
|
||||
return zim
|
||||
|
||||
@staticmethod
|
||||
@optional_session
|
||||
def get_by_id(zim_id: int, session: Session = None) -> Optional['Zim']:
|
||||
zim = session.query(Zim).filter_by(id=zim_id).one_or_none()
|
||||
return zim
|
||||
|
||||
@staticmethod
|
||||
@optional_session
|
||||
def find_by_id(zim_id: int, session: Session = None) -> 'Zim':
|
||||
zim = Zim.get_by_id(zim_id, session=session)
|
||||
if not zim:
|
||||
raise UnknownZim(f'Cannot find zim with ID {zim_id}')
|
||||
return zim
|
||||
|
||||
def get_zim(self) -> Archive:
|
||||
if not self.path or not self.path.is_file():
|
||||
raise FileNotFoundError(f'{self} does not exist!')
|
||||
|
|
@ -295,7 +328,8 @@ class Zims:
|
|||
zims = cls.get_all(session=session)
|
||||
results = OrderedDict()
|
||||
for zim in zims:
|
||||
results[zim] = zim.estimate(search_str)
|
||||
if zim.auto_search:
|
||||
results[zim] = zim.estimate(search_str)
|
||||
return results
|
||||
|
||||
@classmethod
|
||||
|
|
|
|||
|
|
@ -10,6 +10,11 @@ class ZimSearchRequest:
|
|||
limit: Optional[int] = 10
|
||||
|
||||
|
||||
@dataclass
|
||||
class ZimAutoSearchRequest:
|
||||
auto_search: bool
|
||||
|
||||
|
||||
@dataclass
|
||||
class ZimMetadata:
|
||||
path: str
|
||||
|
|
|
|||
|
|
@ -241,3 +241,40 @@ async def test_search_estimates(test_session, async_client, zim_factory):
|
|||
assert response.status_code == HTTPStatus.OK
|
||||
assert len(response.json['zims_estimates']) == 1
|
||||
assert response.json['zims_estimates'][0]['estimate'] == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zim_auto_search(test_session, async_client, zim_factory):
|
||||
"""The auto-searching of any Zim file can be toggled."""
|
||||
zim = zim_factory('test_zim_auto_search.zim')
|
||||
assert zim.auto_search is True, 'Zim should be searched by default.'
|
||||
|
||||
# Zim is estimated by default.
|
||||
body = dict(search_str='one')
|
||||
request, response = await async_client.post('/api/zim/search_estimates', json=body)
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert len(response.json['zims_estimates']) == 1
|
||||
|
||||
# Change auto_search so Zim is not estimated by default.
|
||||
body = dict(auto_search=False)
|
||||
request, response = await async_client.post(f'/api/zim/{zim.id}/auto_search', json=body)
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
assert zim.auto_search is False
|
||||
|
||||
# Estimates are empty because the only Zim was filtered out.
|
||||
body = dict(search_str='one')
|
||||
request, response = await async_client.post('/api/zim/search_estimates', json=body)
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert len(response.json['zims_estimates']) == 0
|
||||
|
||||
# Change auto_search so Zim is now estimated again.
|
||||
body = dict(auto_search=True)
|
||||
request, response = await async_client.post(f'/api/zim/{zim.id}/auto_search', json=body)
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
assert zim.auto_search is True
|
||||
|
||||
# Zim is estimated by default.
|
||||
body = dict(search_str='one')
|
||||
request, response = await async_client.post('/api/zim/search_estimates', json=body)
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert len(response.json['zims_estimates']) == 1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue