diff options
Diffstat (limited to 'gn_auth')
-rw-r--r-- | gn_auth/auth/authorisation/resources/groups/models.py | 48 | ||||
-rw-r--r-- | gn_auth/auth/authorisation/resources/groups/views.py | 32 |
2 files changed, 53 insertions, 27 deletions
diff --git a/gn_auth/auth/authorisation/resources/groups/models.py b/gn_auth/auth/authorisation/resources/groups/models.py index 2df5f04..b8a5235 100644 --- a/gn_auth/auth/authorisation/resources/groups/models.py +++ b/gn_auth/auth/authorisation/resources/groups/models.py @@ -237,15 +237,53 @@ def is_group_leader(conn: db.DbConnection, user: User, group: Group) -> bool: return "group-leader" in role_names -def all_groups(conn: db.DbConnection) -> Maybe[Sequence[Group]]: +def __build_groups_list_query__(base: str, search: Optional[str] = None) -> tuple[str, tuple[Optional[str], ...]]: + """Build up the query from given search terms.""" + if bool(search): + _search = search.strip() + return ((f"{base} WHERE groups.group_name LIKE ? " + "OR groups.group_metadata LIKE ?"), + (f"%{search}%", f"%{search}%")) + return base, tuple() + + +def __limit_results_length__(base: str, start: int = 0, length: int = 0) -> str: + """Add the `LIMIT … OFFSET …` clause to query `base`.""" + if length > 0: + return f"{base} LIMIT {length} OFFSET {start}" + return base + + +def all_groups( + conn: db.DbConnection, + search: Optional[str] = None, + start: int = 0, + length: int = 0 +) -> Maybe[tuple[tuple[Group, ...], int, int]]: """Retrieve all existing groups""" with db.cursor(conn) as cursor: - cursor.execute("SELECT * FROM groups") + cursor.execute("SELECT COUNT(*) FROM groups") + _groups_total_count = int(cursor.fetchone()["COUNT(*)"]) + + _qdets = __build_groups_list_query__( + "SELECT COUNT(*) FROM groups", search) + cursor.execute(*__build_groups_list_query__( + "SELECT COUNT(*) FROM groups", search)) + _filtered_total_count = int(cursor.fetchone()["COUNT(*)"]) + + _query, _params = __build_groups_list_query__( + "SELECT * FROM groups", search) + + cursor.execute(__limit_results_length__(_query, start, length), + _params) res = cursor.fetchall() if res: - return Just(tuple( - Group(row["group_id"], row["group_name"], - json.loads(row["group_metadata"])) for row in res)) + return Just(( + tuple( + Group(row["group_id"], row["group_name"], + json.loads(row["group_metadata"])) for row in res), + _groups_total_count, + _filtered_total_count)) return Nothing diff --git a/gn_auth/auth/authorisation/resources/groups/views.py b/gn_auth/auth/authorisation/resources/groups/views.py index 0c1fefd..1e95f09 100644 --- a/gn_auth/auth/authorisation/resources/groups/views.py +++ b/gn_auth/auth/authorisation/resources/groups/views.py @@ -36,33 +36,21 @@ groups = Blueprint("groups", __name__) def list_groups(): """Return the list of groups that exist.""" _kwargs = {key: value for key, value in request_json().items()} - _total = 0 - _start = int(_kwargs.get("start", "0")) - _length = int(_kwargs.get("length", "0")) - def __add_total_group_count__(conn, groups_list): - with db.cursor(conn) as cursor: - cursor.execute("SELECT COUNT(*) FROM groups") - _total = int(cursor.fetchone()["COUNT(*)"]) - - return { - "total-groups": _total, - "groups": groups_list - } - - def __slice__(groups_details): - _groups = groups_details["groups"] + def __add_total_group_count__(conn, groups_info): return { - **groups_details, - "total-filtered": len(_groups), - "groups": _groups[ - _start:_start+(_length if bool(_length) else len(_groups))] + "groups": groups_info[0], + "total-groups": groups_info[1], + "total-filtered": groups_info[2] } with db.connection(current_app.config["AUTH_DB"]) as conn: - return jsonify(all_groups(conn).then( - partial(__add_total_group_count__, conn) + return jsonify(all_groups( + conn, + search=_kwargs.get("search"), + start=int(_kwargs.get("start", "0")), + length=int(_kwargs.get("length", "0")) ).then( - __slice__ + partial(__add_total_group_count__, conn) ).maybe( { "groups": [], |