diff options
Diffstat (limited to 'gn_auth/auth/authorisation/resources/groups/views.py')
-rw-r--r-- | gn_auth/auth/authorisation/resources/groups/views.py | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/gn_auth/auth/authorisation/resources/groups/views.py b/gn_auth/auth/authorisation/resources/groups/views.py index 746e23c..0c1fefd 100644 --- a/gn_auth/auth/authorisation/resources/groups/views.py +++ b/gn_auth/auth/authorisation/resources/groups/views.py @@ -35,11 +35,41 @@ groups = Blueprint("groups", __name__) @require_oauth("profile group") 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"] + return { + **groups_details, + "total-filtered": len(_groups), + "groups": _groups[ + _start:_start+(_length if bool(_length) else len(_groups))] + } + with db.connection(current_app.config["AUTH_DB"]) as conn: - the_groups = all_groups(conn) + return jsonify(all_groups(conn).then( + partial(__add_total_group_count__, conn) + ).then( + __slice__ + ).maybe( + { + "groups": [], + "message": "No groups found!" + }, + lambda _grpdata: _grpdata)) - return jsonify(the_groups.maybe( - [], lambda grps: [asdict(grp) for grp in grps])) @groups.route("/create", methods=["POST"]) @require_oauth("profile group") |