From 13b91193cc569e1de206f1354362b0c60cf16704 Mon Sep 17 00:00:00 2001 From: fadingNA Date: Mon, 18 Nov 2024 00:05:48 -0500 Subject: [PATCH] backend : update sources/paginated to search document by query 'name' --- application/api/user/routes.py | 13 ++++++++++++- application/celery_init.py | 10 +++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/application/api/user/routes.py b/application/api/user/routes.py index 6a2f3bea..5ebd54c8 100644 --- a/application/api/user/routes.py +++ b/application/api/user/routes.py @@ -473,11 +473,22 @@ class PaginatedSources(Resource): sort_order = request.args.get("order", "desc") # Default to 'desc' page = int(request.args.get("page", 1)) # Default to 1 rows_per_page = int(request.args.get("rows", 10)) # Default to 10 + # add .strip() to remove leading and trailing whitespaces + search_term = request.args.get( + "search", "" + ).strip() # add search for filter documents - # Prepare + # Prepare query for filtering query = {"user": user} + if search_term: + query["name"] = { + "$regex": search_term, + "$options": "i", # using case-insensitive search + } + total_documents = sources_collection.count_documents(query) total_pages = max(1, math.ceil(total_documents / rows_per_page)) + page = min(max(1, page), total_pages) # add this to make sure page inbound is within the range sort_order = 1 if sort_order == "asc" else -1 skip = (page - 1) * rows_per_page diff --git a/application/celery_init.py b/application/celery_init.py index c5838083..185cc87f 100644 --- a/application/celery_init.py +++ b/application/celery_init.py @@ -2,14 +2,22 @@ from celery import Celery from application.core.settings import settings from celery.signals import setup_logging + def make_celery(app_name=__name__): - celery = Celery(app_name, broker=settings.CELERY_BROKER_URL, backend=settings.CELERY_RESULT_BACKEND) + celery = Celery( + app_name, + broker=settings.CELERY_BROKER_URL, + backend=settings.CELERY_RESULT_BACKEND, + ) celery.conf.update(settings) return celery + @setup_logging.connect def config_loggers(*args, **kwargs): from application.core.logging_config import setup_logging + setup_logging() + celery = make_celery()