mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-21 07:26:18 -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
33
alembic/versions/dc6637ced17b_zim_auto_search.py
Normal file
33
alembic/versions/dc6637ced17b_zim_auto_search.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
"""Zim auto search
|
||||
|
||||
Revision ID: dc6637ced17b
|
||||
Revises: 1f2325523525
|
||||
Create Date: 2025-01-30 19:02:32.692428
|
||||
|
||||
"""
|
||||
import os
|
||||
|
||||
from alembic import op
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'dc6637ced17b'
|
||||
down_revision = '1f2325523525'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
DOCKERIZED = True if os.environ.get('DOCKER', '').lower().startswith('t') else False
|
||||
|
||||
|
||||
def upgrade():
|
||||
bind = op.get_bind()
|
||||
session = Session(bind=bind)
|
||||
|
||||
session.execute('ALTER TABLE zim ADD COLUMN auto_search BOOLEAN DEFAULT TRUE')
|
||||
|
||||
|
||||
def downgrade():
|
||||
bind = op.get_bind()
|
||||
session = Session(bind=bind)
|
||||
|
||||
session.execute('ALTER TABLE zim DROP COLUMN IF EXISTS auto_search')
|
||||
|
|
@ -113,6 +113,7 @@ function EvaluateForm() {
|
|||
<pre>sqrt(15)</pre>
|
||||
<pre>15 km to miles</pre>
|
||||
<pre>65 degF to degC</pre>
|
||||
<pre>7 * 750 Mb</pre>
|
||||
</span>
|
||||
|
||||
const inputRef = React.useRef();
|
||||
|
|
|
|||
|
|
@ -1333,6 +1333,14 @@ export async function zimUnsubscribe(id) {
|
|||
return response.status === 204;
|
||||
}
|
||||
|
||||
export async function setZimAutoSearch(id, auto_search) {
|
||||
const body = {auto_search};
|
||||
const response = await apiPost(`${API_URI}/zim/${id}/auto_search`, body);
|
||||
if (response.status !== 204) {
|
||||
throw new Error('Failed to set Zim auto search');
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchDecoded(vinNumber) {
|
||||
const body = {vin_number: vinNumber};
|
||||
const response = await apiPost(`${API_URI}/vin_number_decoder`, body);
|
||||
|
|
|
|||
|
|
@ -795,7 +795,7 @@ export function ThrottleToggle() {
|
|||
</div>;
|
||||
}
|
||||
|
||||
export function Toggle({label, checked, disabled, onChange, icon}) {
|
||||
export function Toggle({label, checked, disabled, onChange, icon, popupContent = null}) {
|
||||
// Custom toggle because Semantic UI does not handle inverted labels correctly.
|
||||
const {t, inverted} = useContext(ThemeContext);
|
||||
|
||||
|
|
@ -833,7 +833,7 @@ export function Toggle({label, checked, disabled, onChange, icon}) {
|
|||
icon = <Icon name={icon}/>
|
||||
}
|
||||
|
||||
return <>
|
||||
const body = <span>
|
||||
<div className='toggle' onMouseUp={onMouseUp}>
|
||||
<input type="checkbox" className={inputClassName} checked={checked} onChange={onMouseUp}
|
||||
data-testid='toggle'/>
|
||||
|
|
@ -843,7 +843,12 @@ export function Toggle({label, checked, disabled, onChange, icon}) {
|
|||
{icon}
|
||||
{label}
|
||||
</span>
|
||||
</>
|
||||
</span>
|
||||
|
||||
if (popupContent) {
|
||||
return <Popup on='hover' trigger={body} content={popupContent}/>
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
export function DisableDownloadsToggle() {
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ import {
|
|||
Paginator,
|
||||
TabLinks,
|
||||
TagIcon,
|
||||
Toggle,
|
||||
useTitle,
|
||||
WarningMessage
|
||||
} from "./Common";
|
||||
|
|
@ -53,6 +54,7 @@ import {
|
|||
fetchZimSubscriptions,
|
||||
refreshFiles,
|
||||
saveSettings,
|
||||
setZimAutoSearch,
|
||||
tagZimEntry,
|
||||
untagZimEntry,
|
||||
zimSubscribe,
|
||||
|
|
@ -343,9 +345,9 @@ const ZimCatalogItemRow = ({item, subscriptions, iso_639_codes, fetchSubscriptio
|
|||
const subscription = name in subscriptions ? subscriptions[name] : null;
|
||||
const subscriptionLanguage = subscription ? subscription['language'] : 'en';
|
||||
|
||||
const [langauge, setLanguage] = useState(subscriptionLanguage);
|
||||
const [language, setLanguage] = useState(subscriptionLanguage);
|
||||
const [pending, setPending] = useState(false);
|
||||
const languageChange = subscription ? langauge !== subscription['language'] : false;
|
||||
const languageChange = subscription ? language !== subscription['language'] : false;
|
||||
|
||||
const wrolModeEnabled = useWROLMode();
|
||||
|
||||
|
|
@ -356,7 +358,7 @@ const ZimCatalogItemRow = ({item, subscriptions, iso_639_codes, fetchSubscriptio
|
|||
if (subscription && !languageChange) {
|
||||
success = await zimUnsubscribe(subscription['id']);
|
||||
} else {
|
||||
success = await zimSubscribe(name, langauge);
|
||||
success = await zimSubscribe(name, language);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
|
|
@ -387,7 +389,7 @@ const ZimCatalogItemRow = ({item, subscriptions, iso_639_codes, fetchSubscriptio
|
|||
const languageDropdown = <Dropdown fluid search selection
|
||||
placeholder='Language'
|
||||
options={languageOptions}
|
||||
value={langauge}
|
||||
value={language}
|
||||
disabled={wrolModeEnabled}
|
||||
onChange={handleLanguageChange}
|
||||
/>;
|
||||
|
|
@ -445,12 +447,28 @@ class ManageZim extends React.Component {
|
|||
}
|
||||
}
|
||||
|
||||
zimFileTableRow = (zim, sortData) => {
|
||||
const {path, size} = zim;
|
||||
zimFileTableRow = (zim, sortData, localFetchZims) => {
|
||||
const {id, path, size, auto_search} = zim;
|
||||
|
||||
const toggleZimAutoSearch = async () => {
|
||||
try {
|
||||
await setZimAutoSearch(id, !auto_search);
|
||||
} catch (e) {
|
||||
throw e;
|
||||
} finally {
|
||||
await localFetchZims();
|
||||
}
|
||||
}
|
||||
const toggle = <Toggle
|
||||
checked={auto_search}
|
||||
onChange={toggleZimAutoSearch}
|
||||
popupContent='Enable/Disable searching this Zim file in the Global Search.'
|
||||
/>
|
||||
|
||||
return <TableRow key={path}>
|
||||
<TableCell>{path}</TableCell>
|
||||
<TableCell>{humanFileSize(size)}</TableCell>
|
||||
<TableCell>{toggle}</TableCell>
|
||||
</TableRow>
|
||||
}
|
||||
|
||||
|
|
@ -460,6 +478,7 @@ class ManageZim extends React.Component {
|
|||
const zimFilesHeaders = [
|
||||
{key: 'path', text: 'Path', sortBy: 'path', width: 14},
|
||||
{key: 'size', text: 'Size', sortBy: 'size', width: 2},
|
||||
{key: 'search', text: 'Search', sortBy: 'auto_search', width: 2},
|
||||
];
|
||||
let zimFilesBody = <Placeholder>
|
||||
<PlaceholderHeader>
|
||||
|
|
@ -469,8 +488,9 @@ class ManageZim extends React.Component {
|
|||
</Placeholder>;
|
||||
if (zims && zims.length >= 1) {
|
||||
zimFilesBody = <SortableTable
|
||||
tableProps={{striped: true}}
|
||||
data={zims}
|
||||
rowFunc={this.zimFileTableRow}
|
||||
rowFunc={(i, sortData) => this.zimFileTableRow(i, sortData, this.fetchZims.bind(this))}
|
||||
rowKey='path'
|
||||
tableHeaders={zimFilesHeaders}
|
||||
/>;
|
||||
|
|
|
|||
|
|
@ -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