From 3d317e750536f61f32e09d64f2f3fddfc8ce6dcf Mon Sep 17 00:00:00 2001 From: Miguel Astor Date: Mon, 21 Aug 2023 21:57:22 -0400 Subject: [PATCH] Reworked search to optimize a bit. --- viewer/views.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/viewer/views.py b/viewer/views.py index dae28bf..27ef1e9 100644 --- a/viewer/views.py +++ b/viewer/views.py @@ -34,11 +34,12 @@ def do_recursive_search(start_path, query): """ # Get all sub-dirs and images that match the query. - subdirs = sorted([i.absolute() for i in start_path.iterdir() if i.is_dir() and query.lower() in i.name.lower()]) - images = sorted([i for i in start_path.iterdir() if i.is_file() and filetype.is_image(str(i)) and query.lower() in i.name.lower()]) + all_subdirs = sorted([i for i in start_path.iterdir() if i.is_dir()]) + subdirs = sorted([i for i in all_subdirs if query.lower() in i.name.lower()]) + images = sorted([i for i in start_path.iterdir() if i.is_file() and query.lower() in i.name.lower()]) # For all sub-directories, regardless of the query. - for subdir in sorted([i for i in start_path.iterdir() if i.is_dir()]): + for subdir in all_subdirs: # Do a recursive search. rec_subdirs, rec_images = do_recursive_search(subdir, query.lower()) @@ -94,6 +95,9 @@ def gallery_view(request, path = None): # If there is a search query then search the current directory recursively. subdirs, images = do_recursive_search(full_path, search) + # Only keep image files. + images = [image for image in images if filetype.is_image(str(image))] + # For every sub-directory found, prepare it's name and path for rendering. subdir_data = [] for subdir in subdirs: