show search results only of visible folder
This commit is contained in:
parent
1eb9361b8a
commit
c1bc20d876
@ -16,11 +16,12 @@ access_log_db.row_factory = sqlite3.Row
|
|||||||
def init_db():
|
def init_db():
|
||||||
"""Initializes the database with the required schema."""
|
"""Initializes the database with the required schema."""
|
||||||
cursor = search_db.cursor()
|
cursor = search_db.cursor()
|
||||||
# Create table with the new 'hitcount' column.
|
# Create table with the new 'hitcount' and 'basefolder' columns.
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
CREATE TABLE IF NOT EXISTS files (
|
CREATE TABLE IF NOT EXISTS files (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
relative_path TEXT,
|
relative_path TEXT,
|
||||||
|
basefolder TEXT,
|
||||||
filename TEXT,
|
filename TEXT,
|
||||||
filetype TEXT,
|
filetype TEXT,
|
||||||
transcript TEXT,
|
transcript TEXT,
|
||||||
@ -29,12 +30,17 @@ def init_db():
|
|||||||
)
|
)
|
||||||
''')
|
''')
|
||||||
search_db.commit()
|
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:
|
try:
|
||||||
cursor.execute("ALTER TABLE files ADD COLUMN hitcount INTEGER DEFAULT 0")
|
cursor.execute("ALTER TABLE files ADD COLUMN hitcount INTEGER DEFAULT 0")
|
||||||
except sqlite3.OperationalError:
|
except sqlite3.OperationalError:
|
||||||
# Likely the column already exists, so we ignore this error.
|
# Likely the column already exists, so we ignore this error.
|
||||||
pass
|
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()
|
search_db.commit()
|
||||||
|
|
||||||
def scan_dir(directory):
|
def scan_dir(directory):
|
||||||
@ -70,13 +76,14 @@ def updatefileindex():
|
|||||||
for config in config_data:
|
for config in config_data:
|
||||||
for folder in config.get("folders", []):
|
for folder in config.get("folders", []):
|
||||||
foldername = folder.get("foldername")
|
foldername = folder.get("foldername")
|
||||||
|
print(f"Processing folder: {foldername}")
|
||||||
raw_folderpath = folder.get("folderpath")
|
raw_folderpath = folder.get("folderpath")
|
||||||
norm_folderpath = os.path.normpath(raw_folderpath)
|
norm_folderpath = os.path.normpath(raw_folderpath)
|
||||||
# Precompute the length of the base folder path (plus one for the separator)
|
# Precompute the length of the base folder path (plus one for the separator)
|
||||||
base_len = len(norm_folderpath) + 1
|
base_len = len(norm_folderpath) + 1
|
||||||
|
|
||||||
# Accumulate scanned file data and keys for this base folder.
|
# 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()
|
current_keys = set()
|
||||||
|
|
||||||
for entry in scan_dir(norm_folderpath):
|
for entry in scan_dir(norm_folderpath):
|
||||||
@ -106,7 +113,7 @@ def updatefileindex():
|
|||||||
# Retrieve the hit count for this file.
|
# Retrieve the hit count for this file.
|
||||||
hit_count = get_hit_count(relative_path)
|
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))
|
current_keys.add((relative_path, entry.name))
|
||||||
|
|
||||||
# Remove database entries for files under this base folder that are no longer on disk.
|
# 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.
|
# Bulk write the scanned files using INSERT OR REPLACE.
|
||||||
cursor.executemany(
|
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
|
scanned_files
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
21
search.py
21
search.py
@ -1,5 +1,5 @@
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
from flask import Flask, render_template, request, request, jsonify
|
from flask import Flask, render_template, request, request, jsonify, session
|
||||||
import os
|
import os
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@ -16,6 +16,9 @@ def searchcommand():
|
|||||||
words = [w for w in query.split() if w]
|
words = [w for w in query.split() if w]
|
||||||
cursor = search_db.cursor()
|
cursor = search_db.cursor()
|
||||||
|
|
||||||
|
allowed_basefolders = list(session['folders'].keys())
|
||||||
|
print("Allowed base folders:", allowed_basefolders)
|
||||||
|
|
||||||
if not include_transcript:
|
if not include_transcript:
|
||||||
conditions = []
|
conditions = []
|
||||||
params = []
|
params = []
|
||||||
@ -23,10 +26,15 @@ def searchcommand():
|
|||||||
for word in words:
|
for word in words:
|
||||||
conditions.append("(relative_path LIKE ? OR filename LIKE ?)")
|
conditions.append("(relative_path LIKE ? OR filename LIKE ?)")
|
||||||
params.extend([f"%{word}%", f"%{word}%"])
|
params.extend([f"%{word}%", f"%{word}%"])
|
||||||
# search category in filename
|
# Search category in filename
|
||||||
if category:
|
if category:
|
||||||
conditions.append("(filename LIKE ?)")
|
conditions.append("(filename LIKE ?)")
|
||||||
params.extend([f"%{category}%"])
|
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"
|
sql = "SELECT * FROM files"
|
||||||
if conditions:
|
if conditions:
|
||||||
@ -40,14 +48,19 @@ def searchcommand():
|
|||||||
# Advanced search: include transcript. Count transcript hits.
|
# Advanced search: include transcript. Count transcript hits.
|
||||||
conditions = []
|
conditions = []
|
||||||
params = []
|
params = []
|
||||||
# Apply query words only for filename and transcript
|
# Apply query words for filename and transcript
|
||||||
for word in words:
|
for word in words:
|
||||||
conditions.append("(filename LIKE ? OR transcript LIKE ?)")
|
conditions.append("(filename LIKE ? OR transcript LIKE ?)")
|
||||||
params.extend([f"%{word}%", f"%{word}%"])
|
params.extend([f"%{word}%", f"%{word}%"])
|
||||||
# search category in filename
|
# Search category in filename
|
||||||
if category:
|
if category:
|
||||||
conditions.append("(filename LIKE ?)")
|
conditions.append("(filename LIKE ?)")
|
||||||
params.extend([f"%{category}%"])
|
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"
|
sql = "SELECT * FROM files"
|
||||||
if conditions:
|
if conditions:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user