def suggest(): import re query = {} prefix = request.values.get("prefix") if prefix: try: prefix = re.compile("^" + prefix) query = { "$or": [ { "username": prefix }, { "first_name": prefix }, { "last_name": prefix }, ] } except re.error: pass users = User.find(query).sort("username", 1).limit(10) users = [ user.to_dict(fields=["username", "first_name", "last_name"]) for user in users ] return json_response({"data": users})
def search(): query = request.values.get("q") if query is None: raise InputDataError("'q' param is missing") posts = paginated(search_posts(query), transform=transform) author_ids = {ObjectId(x["author_id"]) for x in posts["data"]} authors = User.find({"_id": {"$in": list(author_ids)}}).all() return json_response({"results": posts, "authors": {"data": authors}})
def drop(self): fx_users = User.find({"username": re.compile(r"^fx_test_user")}) user_ids = [u._id for u in fx_users] ctx.log.info("dropping fixture comments") Comment.destroy_many({"author_id": {"$in": user_ids}}) ctx.log.info("dropping fixture answers") Answer.destroy_many({"author_id": {"$in": user_ids}}) ctx.log.info("dropping fixture questions") Question.destroy_many({"author_id": {"$in": user_ids}}) ctx.log.info("dropping fixture users") User.destroy_many({"username": re.compile(r"^fx_test_user")})
def list_users(ids): ids = ids.split(",") ids = [resolve_id(id_) for id_ in ids] items = User.find( {"$or": [ { "_id": { "$in": ids } }, { "username": { "$in": ids } }, ]}) items = [user.to_dict(USER_FIELDS) for user in items] return {"data": items}
def index(): u: User = get_user_from_app_context() if "_sort" in request.values: srt = request.values["_sort"] else: srt = "rating" sortExpr = SORT_MAP.get(srt) if sortExpr is None: raise ApiError(f"unknown sort operator \"{srt}\"") if get_boolean_request_param("_mine"): if u: # only mine posts query = {"author_id": u._id} else: # nothing to show, user is not logged in query = {"_id": NilObjectId} else: # otherwise show all not deleted posts query = { "$or": [ { "deleted": False }, ] } if u: # if user is logged in, he can view his own deleted posts query["$or"].append({"author_id": u._id}) questions = Question.find(query).sort(sortExpr) results = paginated( questions, transform=default_transform(fields=QUESTION_LIST_FIELDS)) author_ids = set() for q in results["data"]: author_ids.add(resolve_id(q["author_id"])) authors = User.find({"_id": {"$in": list(author_ids)}}) return json_response({"questions": results, "authors": {"data": authors}})