diff --git a/features/activity.py b/features/activity.py index b30b696..4085c6f 100644 --- a/features/activity.py +++ b/features/activity.py @@ -19,9 +19,27 @@ class Activity: # We don't want logging to break the main application flow current_app.logger.error(f"Error logging activity: {e}") - def get_recent_logs(self, limit=50, offset=0): - """Fetches recent activity logs with person names, supporting pagination.""" - query = """ + def get_recent_logs(self, limit=50, offset=0, search_query=None): + """Fetches recent activity logs with person names, supporting pagination and search.""" + params = [limit, offset] + search_clause = "" + + if search_query: + # Add wildcard percentages for partial matching + term = f"%{search_query}%" + search_clause = """ + WHERE + p.name ILIKE %s OR + al.action ILIKE %s OR + al.entity_type ILIKE %s OR + al.details ILIKE %s + """ + # Prepend search terms to params list (limit/offset must change position if we were using ? placeholders + # but with %s list, order matters. Let's reconstruct consistent order). + # Actually, LIMIT/OFFSET are at the end. Search params come before. + params = [term, term, term, term, limit, offset] + + query = f""" SELECT al.id, al.person_id, @@ -35,7 +53,8 @@ class Activity: al.timestamp FROM activity_log al LEFT JOIN person p ON al.person_id = p.person_id + {search_clause} ORDER BY al.timestamp DESC LIMIT %s OFFSET %s """ - return self.execute(query, [limit, offset]) + return self.execute(query, params) diff --git a/routes/settings.py b/routes/settings.py index 6fb5c08..24dd1b7 100644 --- a/routes/settings.py +++ b/routes/settings.py @@ -53,7 +53,9 @@ def settings_activity(): def settings_activity_logs(): limit = 50 offset = request.args.get('offset', 0, type=int) - logs = db.activityRequest.get_recent_logs(limit=limit, offset=offset) + search_query = request.args.get('search_query', '') + + logs = db.activityRequest.get_recent_logs(limit=limit, offset=offset, search_query=search_query) # Check if there are more logs to load has_more = len(logs) == limit @@ -61,4 +63,6 @@ def settings_activity_logs(): return render_template('partials/activity_logs.html', logs=logs, offset=offset, - has_more=has_more) + has_more=has_more, + search_query=search_query, + limit=limit) diff --git a/templates/partials/activity_logs.html b/templates/partials/activity_logs.html index 9ecde5c..ea212c6 100644 --- a/templates/partials/activity_logs.html +++ b/templates/partials/activity_logs.html @@ -50,7 +50,8 @@ {% if has_more %}