show search results only of visible folder

This commit is contained in:
lelo 2025-04-05 16:01:17 +00:00
parent 1eb9361b8a
commit c1bc20d876
2 changed files with 29 additions and 9 deletions

View File

@ -16,11 +16,12 @@ access_log_db.row_factory = sqlite3.Row
def init_db():
"""Initializes the database with the required schema."""
cursor = search_db.cursor()
# Create table with the new 'hitcount' column.
# Create table with the new 'hitcount' and 'basefolder' columns.
cursor.execute('''
CREATE TABLE IF NOT EXISTS files (
id INTEGER PRIMARY KEY AUTOINCREMENT,
relative_path TEXT,
basefolder TEXT,
filename TEXT,
filetype TEXT,
transcript TEXT,
@ -29,12 +30,17 @@ def init_db():
)
''')
search_db.commit()
# If the table already existed, try to add the 'hitcount' column.
# If the table already existed, try to add the new columns.
try:
cursor.execute("ALTER TABLE files ADD COLUMN hitcount INTEGER DEFAULT 0")
except sqlite3.OperationalError:
# Likely the column already exists, so we ignore this error.
pass
try:
cursor.execute("ALTER TABLE files ADD COLUMN basefolder TEXT")
except sqlite3.OperationalError:
# Likely the column already exists, so we ignore this error.
pass
search_db.commit()
def scan_dir(directory):
@ -70,13 +76,14 @@ def updatefileindex():
for config in config_data:
for folder in config.get("folders", []):
foldername = folder.get("foldername")
print(f"Processing folder: {foldername}")
raw_folderpath = folder.get("folderpath")
norm_folderpath = os.path.normpath(raw_folderpath)
# Precompute the length of the base folder path (plus one for the separator)
base_len = len(norm_folderpath) + 1
# Accumulate scanned file data and keys for this base folder.
scanned_files = [] # Each entry: (relative_path, filename, filetype, transcript, hitcount)
scanned_files = [] # Each entry: (relative_path, basefolder, filename, filetype, transcript, hitcount)
current_keys = set()
for entry in scan_dir(norm_folderpath):
@ -106,7 +113,7 @@ def updatefileindex():
# Retrieve the hit count for this file.
hit_count = get_hit_count(relative_path)
scanned_files.append((relative_path, entry.name, filetype, transcript, hit_count))
scanned_files.append((relative_path, foldername, entry.name, filetype, transcript, hit_count))
current_keys.add((relative_path, entry.name))
# Remove database entries for files under this base folder that are no longer on disk.
@ -120,7 +127,7 @@ def updatefileindex():
# Bulk write the scanned files using INSERT OR REPLACE.
cursor.executemany(
"INSERT OR REPLACE INTO files (relative_path, filename, filetype, transcript, hitcount) VALUES (?, ?, ?, ?, ?)",
"INSERT OR REPLACE INTO files (relative_path, basefolder, filename, filetype, transcript, hitcount) VALUES (?, ?, ?, ?, ?, ?)",
scanned_files
)

View File

@ -1,5 +1,5 @@
import sqlite3
from flask import Flask, render_template, request, request, jsonify
from flask import Flask, render_template, request, request, jsonify, session
import os
app = Flask(__name__)
@ -15,6 +15,9 @@ def searchcommand():
include_transcript = request.form.get("includeTranscript") in ["true", "on"]
words = [w for w in query.split() if w]
cursor = search_db.cursor()
allowed_basefolders = list(session['folders'].keys())
print("Allowed base folders:", allowed_basefolders)
if not include_transcript:
conditions = []
@ -23,10 +26,15 @@ def searchcommand():
for word in words:
conditions.append("(relative_path LIKE ? OR filename LIKE ?)")
params.extend([f"%{word}%", f"%{word}%"])
# search category in filename
# Search category in filename
if category:
conditions.append("(filename LIKE ?)")
params.extend([f"%{category}%"])
# Only include rows where basefolder is in allowed_basefolders
if allowed_basefolders:
placeholders = ",".join("?" for _ in allowed_basefolders)
conditions.append(f"basefolder IN ({placeholders})")
params.extend(allowed_basefolders)
sql = "SELECT * FROM files"
if conditions:
@ -40,14 +48,19 @@ def searchcommand():
# Advanced search: include transcript. Count transcript hits.
conditions = []
params = []
# Apply query words only for filename and transcript
# Apply query words for filename and transcript
for word in words:
conditions.append("(filename LIKE ? OR transcript LIKE ?)")
params.extend([f"%{word}%", f"%{word}%"])
# search category in filename
# Search category in filename
if category:
conditions.append("(filename LIKE ?)")
params.extend([f"%{category}%"])
# Only include rows where basefolder is in allowed_basefolders
if allowed_basefolders:
placeholders = ",".join("?" for _ in allowed_basefolders)
conditions.append(f"basefolder IN ({placeholders})")
params.extend(allowed_basefolders)
sql = "SELECT * FROM files"
if conditions: