mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-29 03:16:22 -05:00
Suggesting channels that names match, even without spaces.
This commit is contained in:
parent
6d5a56cacc
commit
85a1360a25
2 changed files with 22 additions and 5 deletions
|
|
@ -1,7 +1,7 @@
|
|||
from pathlib import Path
|
||||
from typing import List, Dict, Union
|
||||
|
||||
from sqlalchemy import asc
|
||||
from sqlalchemy import asc, or_
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
|
|
@ -203,9 +203,14 @@ def download_channel(id_: int, reset_attempts: bool = False):
|
|||
|
||||
@optional_session
|
||||
async def search_channels_by_name(name: str, limit: int = 5, session: Session = None) -> List[Channel]:
|
||||
channels = session.query(Channel) \
|
||||
.filter(Channel.name.ilike(f'%{name}%')) \
|
||||
name = name or ''
|
||||
name_no_spaces = ''.join(name.split(' '))
|
||||
stmt = session.query(Channel) \
|
||||
.filter(or_(
|
||||
Channel.name.ilike(f'%{name}%'),
|
||||
Channel.name.ilike(f'%{name_no_spaces}%'),
|
||||
)) \
|
||||
.order_by(asc(Channel.name)) \
|
||||
.limit(limit) \
|
||||
.all()
|
||||
.limit(limit)
|
||||
channels = stmt.all()
|
||||
return channels
|
||||
|
|
|
|||
|
|
@ -503,6 +503,18 @@ async def test_search_suggestions(test_session, test_async_client, channel_facto
|
|||
1,
|
||||
)
|
||||
|
||||
# Channel name "Fool" is matched because spaces are stripped in addition to only
|
||||
await assert_results(
|
||||
dict(search_str='foo l'),
|
||||
[
|
||||
{'directory': 'Fool',
|
||||
'id': 2,
|
||||
'name': 'Fool',
|
||||
'url': 'https://example.com/Fool'}],
|
||||
[],
|
||||
0,
|
||||
)
|
||||
|
||||
await assert_results(
|
||||
dict(search_str='bar'),
|
||||
[{'directory': 'Bar', 'id': 3, 'name': 'Bar', 'url': 'https://example.com/Bar'}],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue