From 85a1360a2518caf6e2937407d7a61e30e9a2ddc4 Mon Sep 17 00:00:00 2001 From: Roland Date: Mon, 19 Feb 2024 14:55:53 -0700 Subject: [PATCH] Suggesting channels that names match, even without spaces. --- modules/videos/channel/lib.py | 15 ++++++++++----- wrolpi/test/test_root_api.py | 12 ++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/modules/videos/channel/lib.py b/modules/videos/channel/lib.py index 07bb32ae..9170ae52 100644 --- a/modules/videos/channel/lib.py +++ b/modules/videos/channel/lib.py @@ -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 diff --git a/wrolpi/test/test_root_api.py b/wrolpi/test/test_root_api.py index b196d6f9..df0626c7 100644 --- a/wrolpi/test/test_root_api.py +++ b/wrolpi/test/test_root_api.py @@ -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'}],