mirror of
https://github.com/logos-co/open-law.git
synced 2025-01-26 14:48:49 +00:00
search user by username
This commit is contained in:
parent
2c4f16ef28
commit
83695d234b
@ -1,19 +1,13 @@
|
||||
from flask import (
|
||||
Blueprint,
|
||||
render_template,
|
||||
request,
|
||||
flash,
|
||||
redirect,
|
||||
url_for,
|
||||
)
|
||||
from flask import Blueprint, render_template, request, flash, redirect, url_for, jsonify
|
||||
from flask_login import login_required
|
||||
from app.controllers import create_pagination
|
||||
|
||||
from app import models as m, db
|
||||
from app import forms as f
|
||||
from app.logger import log
|
||||
from config import config
|
||||
|
||||
|
||||
configuration = config()
|
||||
bp = Blueprint("user", __name__, url_prefix="/user")
|
||||
|
||||
|
||||
@ -89,3 +83,17 @@ def delete(id):
|
||||
log(log.INFO, "User deleted. User: [%s]", u)
|
||||
flash("User deleted!", "success")
|
||||
return "ok", 200
|
||||
|
||||
|
||||
@bp.route("/search", methods=["GET"])
|
||||
@login_required
|
||||
def search():
|
||||
q = request.args.get("q", type=str, default=None)
|
||||
if not q:
|
||||
return jsonify({"message": "q parameter is required"}), 422
|
||||
|
||||
users = m.User.query.order_by(m.User.username).filter(m.User.username.like(f"{q}%"))
|
||||
users = users.limit(configuration.MAX_SEARCH_RESULTS)
|
||||
users = [{"username": user.username, "id": user.id} for user in users.all()]
|
||||
|
||||
return jsonify({"users": users})
|
||||
|
@ -23,6 +23,7 @@ class BaseConfig(BaseSettings):
|
||||
# Pagination
|
||||
DEFAULT_PAGE_SIZE: int
|
||||
PAGE_LINKS_NUMBER: int
|
||||
MAX_SEARCH_RESULTS: int
|
||||
|
||||
@staticmethod
|
||||
def configure(app: Flask):
|
||||
|
@ -26,3 +26,4 @@ ADMIN_PASSWORD=admin
|
||||
# Pagination
|
||||
DEFAULT_PAGE_SIZE=8
|
||||
PAGE_LINKS_NUMBER=8
|
||||
MAX_SEARCH_RESULTS=5
|
||||
|
@ -40,6 +40,47 @@ def test_delete_user(populate: FlaskClient):
|
||||
login(populate)
|
||||
users = m.User.query.all()
|
||||
uc = len(users)
|
||||
response = populate.delete("/user/delete/1")
|
||||
response = populate.get("/user/delete/1")
|
||||
assert m.User.query.count() < uc
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_search_user(populate: FlaskClient):
|
||||
login(populate)
|
||||
MAX_SEARCH_RESULTS = populate.application.config["MAX_SEARCH_RESULTS"]
|
||||
|
||||
response = populate.get("/user/search")
|
||||
assert response.status_code == 422
|
||||
assert response.json["message"] == "q parameter is required"
|
||||
|
||||
q = "user"
|
||||
|
||||
response = populate.get(f"/user/search?q={q}")
|
||||
assert response.json
|
||||
|
||||
users = response.json.get("users")
|
||||
assert users
|
||||
assert len(users) <= MAX_SEARCH_RESULTS
|
||||
|
||||
for user in users:
|
||||
assert q in user["username"]
|
||||
|
||||
q = "user1"
|
||||
|
||||
response = populate.get(f"/user/search?q={q}")
|
||||
assert response.json
|
||||
|
||||
users = response.json.get("users")
|
||||
assert users
|
||||
assert len(users) <= MAX_SEARCH_RESULTS
|
||||
|
||||
user = users[0]
|
||||
assert user["username"] == q
|
||||
|
||||
q = "booboo"
|
||||
|
||||
response = populate.get(f"/user/search?q={q}")
|
||||
assert response.json
|
||||
|
||||
users = response.json.get("users")
|
||||
assert not users
|
||||
|
Loading…
x
Reference in New Issue
Block a user