Skip to content

Commit 305557d

Browse files
committed
Move /search-users to users blueprint
1 parent 81beb95 commit 305557d

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

pydatalab/src/pydatalab/routes/v0_1/items.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from pydatalab.logger import LOGGER
1414
from pydatalab.models import ITEM_MODELS
1515
from pydatalab.models.items import Item
16-
from pydatalab.models.people import Person
1716
from pydatalab.models.relationships import RelationshipType
1817
from pydatalab.models.utils import generate_unique_refcode
1918
from pydatalab.mongo import flask_mongo
@@ -981,43 +980,3 @@ def save_item():
981980
)
982981

983982
return jsonify(status="success", last_modified=updated_data["last_modified"]), 200
984-
985-
986-
@ITEMS.route("/search-users/", methods=["GET"])
987-
def search_users():
988-
"""Perform free text search on users and return the top results.
989-
GET parameters:
990-
query: String with the search terms.
991-
nresults: Maximum number of (default 100)
992-
993-
Returns:
994-
response list of dictionaries containing the matching items in order of
995-
descending match score.
996-
"""
997-
998-
query = request.args.get("query", type=str)
999-
nresults = request.args.get("nresults", default=100, type=int)
1000-
types = request.args.get("types", default=None)
1001-
1002-
match_obj = {"$text": {"$search": query}}
1003-
if types is not None:
1004-
match_obj["type"] = {"$in": types}
1005-
1006-
cursor = flask_mongo.db.users.aggregate(
1007-
[
1008-
{"$match": match_obj},
1009-
{"$sort": {"score": {"$meta": "textScore"}}},
1010-
{"$limit": nresults},
1011-
{
1012-
"$project": {
1013-
"_id": 1,
1014-
"identities": 1,
1015-
"display_name": 1,
1016-
"contact_email": 1,
1017-
}
1018-
},
1019-
]
1020-
)
1021-
return jsonify(
1022-
{"status": "success", "users": list(json.loads(Person(**d).json()) for d in cursor)}
1023-
), 200

pydatalab/src/pydatalab/routes/v0_1/users.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import json
2+
13
from bson import ObjectId
24
from flask import Blueprint, jsonify, request
35
from flask_login import current_user
46

57
from pydatalab.config import CONFIG
6-
from pydatalab.models.people import DisplayName, EmailStr
8+
from pydatalab.models.people import DisplayName, EmailStr, Person
79
from pydatalab.mongo import flask_mongo
10+
from pydatalab.permissions import active_users_or_get_only
811

912
USERS = Blueprint("users", __name__)
1013

@@ -71,3 +74,44 @@ def save_user(user_id):
7174
)
7275

7376
return (jsonify({"status": "success"}), 200)
77+
78+
79+
@active_users_or_get_only
80+
@USERS.route("/search-users/", methods=["GET"])
81+
def search_users():
82+
"""Perform free text search on users and return the top results.
83+
GET parameters:
84+
query: String with the search terms.
85+
nresults: Maximum number of (default 100)
86+
87+
Returns:
88+
response list of dictionaries containing the matching items in order of
89+
descending match score.
90+
"""
91+
92+
query = request.args.get("query", type=str)
93+
nresults = request.args.get("nresults", default=100, type=int)
94+
types = request.args.get("types", default=None)
95+
96+
match_obj = {"$text": {"$search": query}}
97+
if types is not None:
98+
match_obj["type"] = {"$in": types}
99+
100+
cursor = flask_mongo.db.users.aggregate(
101+
[
102+
{"$match": match_obj},
103+
{"$sort": {"score": {"$meta": "textScore"}}},
104+
{"$limit": nresults},
105+
{
106+
"$project": {
107+
"_id": 1,
108+
"identities": 1,
109+
"display_name": 1,
110+
"contact_email": 1,
111+
}
112+
},
113+
]
114+
)
115+
return jsonify(
116+
{"status": "success", "users": list(json.loads(Person(**d).json()) for d in cursor)}
117+
), 200

0 commit comments

Comments
 (0)