Files can be searched.

This commit is contained in:
Roland 2022-02-01 14:11:14 -07:00
parent a5e8ef16b6
commit 643c86e9a4
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
7 changed files with 187 additions and 87 deletions

View file

@ -126,6 +126,9 @@ def refresh_files():
def search(search_str: str, limit: int, offset: int):
"""
Search the "title" of each file. The title is the words in the file's stem.
"""
with get_db_curs() as curs:
stmt = '''
SELECT id, ts_rank_cd(textsearch, websearch_to_tsquery(%(search_str)s)), COUNT(*) OVER() AS total

View file

@ -9,26 +9,27 @@ from wrolpi.media_path import MediaPathType
class File(ModelHelper, Base):
__tablename__ = 'file'
id = Column(Integer, primary_key=True)
path = Column(MediaPathType)
mimetype = Column(String)
modification_datetime = Column(TZDateTime)
path = Column(MediaPathType)
size = Column(Integer)
title = Column(String)
title = deferred(Column(String)) # used by textsearch column
textsearch = deferred(
Column(tsvector, Computed('''to_tsvector('english'::regconfig, (COALESCE(title, ''::text) || ' '::text))''')))
Column(tsvector, Computed('''to_tsvector('english'::regconfig, title)''')))
def __repr__(self):
return f'<File id={self.id} path={self.path.relative_to(get_media_directory())} mime={self.mimetype}>'
def __json__(self):
path = self.path.path.relative_to(get_media_directory())
d = dict(
id=self.id,
mimetype=self.mimetype,
modification_datetime=self.modification_datetime,
path=self.path.path.relative_to(get_media_directory()),
path=path,
size=self.size,
title=self.title,
key=path,
)
return d

View file

@ -124,12 +124,15 @@ def do_search(test_client, search_str, total, expected):
assert response.json['totals']['files'] == total
for file, expected in zip_longest(response.json['files'], expected):
assert_dict_contains(file, expected)
# FileBrowser in React requires a key. We use the path.
assert file['path'] == file['key']
def test_files_search(test_session, test_client, make_files_structure):
# You can search an empty directory.
do_search(test_client, 'nothing', 0, [])
# Create files in the temporary directory. Add some contents so the mimetype can be tested.
files = [
'foo_is_the_name.txt',
'archives/bar.txt',