From f649b8eed71a34fc5891ce136e35a9148bf4ee96 Mon Sep 17 00:00:00 2001 From: 007vedant Date: Tue, 25 May 2021 22:54:59 +0530 Subject: [PATCH] updated and improved search api enables querying through names via regex enables querying through id enables global search --- server/config.py | 2 -- server/records/routes.py | 24 ++++++++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/server/config.py b/server/config.py index 72c11d9..e742f56 100644 --- a/server/config.py +++ b/server/config.py @@ -9,8 +9,6 @@ class Config: MONGO_URI = os.getenv("MONGO_URI") SECRET_KEY = os.getenv("SECRET_KEY") - # MONGO_URI = "mongodb+srv://scooptroop:scooptroop123@elixir.csdhw.mongodb.net/elixir?retryWrites=true&w=majority" - # SECRET_KEY = "thiswillcontainsecretkey" ALLOWED_EXTENSIONS = {"txt", "pdf", "doc", "jpg", "jpeg", "png"} Path("server/static/uploads").mkdir(parents=True, exist_ok=True) UPLOAD_FOLDER = os.path.join(os.getcwd(), "server", "static", "uploads") diff --git a/server/records/routes.py b/server/records/routes.py index 67b9a21..105dcb7 100644 --- a/server/records/routes.py +++ b/server/records/routes.py @@ -1,5 +1,5 @@ import os -import json +import re import datetime from bson import ObjectId from server.config import Config @@ -156,16 +156,28 @@ def addAttachment(_id, rid): return jsonify({"message": "Attachement Added!"}), 200 -@records.route("/api/users/search", defaults={"hid": None}, methods=["GET"]) -@records.route("/api/users/search/", methods=["GET"]) +@records.route("/api/users/search", methods=["GET"]) @token_required -def getHealthOfficials(_id, hid): - if hid is None: +def getHealthOfficials(_id): + hid = request.args.get("hid", default=None, type=str) + name = request.args.get("name", default=None, type=str) + regex = re.compile(f".*{name}.*", re.IGNORECASE) + + if hid is None and name is None: healthOfficials = HealthOfficial.objects.scalar("name", "email").to_json() return healthOfficials, 200 - else: + + elif hid and name is None: healthOfficial = HealthOfficial.objects(_id=ObjectId(hid)).scalar( "name", "email" ) healthOfficial = healthOfficial.to_json() return healthOfficial, 200 + + elif name and hid is None: + healthOfficial = HealthOfficial.objects(name=regex).scalar("name", "email") + healthOfficial = healthOfficial.to_json() + return healthOfficial, 200 + + else: + return jsonify({"message": "Bad Request"}), 400