-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from TheJacksonLaboratory/G3-490-update-genewe…
…aver-db-for-threshold-fixes G3-490: Improvements for set threshold fixes.
- Loading branch information
Showing
10 changed files
with
603 additions
and
269 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "geneweaver-db" | ||
version = "0.5.0" | ||
version = "0.6.0a0" | ||
description = "Database Interaction Services for GeneWeaver" | ||
authors = ["Jax Computational Sciences <[email protected]>"] | ||
readme = "README.md" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
"""Functions for querying the user table. | ||
The functions that return one or more entire user records are: | ||
- by_api_key | ||
- by_sso_id | ||
- by_user_id | ||
- by_email | ||
""" | ||
|
||
from typing import List | ||
|
||
from geneweaver.db.query import user | ||
from geneweaver.db.utils import temp_override_row_factory | ||
from psycopg import AsyncCursor, rows | ||
|
||
|
||
async def by_api_key(cursor: AsyncCursor, api_key: str) -> List: | ||
"""Get user info by api key. | ||
:param cursor: The database cursor. | ||
:param api_key: The api key to search for. | ||
:return: list of results using `.fetchall()` | ||
""" | ||
await cursor.execute(*user.by_api_key(api_key)) | ||
return await cursor.fetchall() | ||
|
||
|
||
async def by_sso_id(cursor: AsyncCursor, sso_id: str) -> List: | ||
"""Get user info by sso id. | ||
:param cursor: The database cursor. | ||
:param sso_id: The sso id to search for. | ||
:return: list of results using `.fetchall()` | ||
""" | ||
await cursor.execute(*user.by_sso_id(sso_id)) | ||
return await cursor.fetchall() | ||
|
||
|
||
async def by_sso_id_and_email(cursor: AsyncCursor, sso_id: str, email: str) -> List: | ||
"""Get user info by sso id and email. | ||
:param cursor: The database cursor. | ||
:param sso_id: The sso id to search for. | ||
:param email: The email to search for. | ||
:return: list of results using `.fetchall()` | ||
""" | ||
await cursor.execute(*user.by_sso_id_and_email(sso_id, email)) | ||
return await cursor.fetchall() | ||
|
||
|
||
async def by_user_id(cursor: AsyncCursor, user_id: int) -> List: | ||
"""Get user info by user id. | ||
:param cursor: The database cursor. | ||
:param user_id: The user id (internal) to search for. | ||
:return: list of results using `.fetchall()` | ||
""" | ||
await cursor.execute(*user.by_id(user_id)) | ||
return await cursor.fetchall() | ||
|
||
|
||
async def by_email(cursor: AsyncCursor, email: str) -> List: | ||
"""Get user info by email. | ||
:param cursor: The database cursor. | ||
:param email: The email to search for. | ||
:return: list of results using `.fetchall()` | ||
""" | ||
await cursor.execute(*user.by_email(email)) | ||
return await cursor.fetchall() | ||
|
||
|
||
@temp_override_row_factory(rows.tuple_row) | ||
async def email_exists(cursor: AsyncCursor, email: str) -> bool: | ||
"""Check if email exists. | ||
:param cursor: The database cursor. | ||
:param email: The email to check. | ||
:return: True if the email exists, otherwise False. | ||
""" | ||
await cursor.execute(*user.email_exists(email)) | ||
exists = bool((await cursor.fetchone())[0]) | ||
return exists | ||
|
||
|
||
@temp_override_row_factory(rows.tuple_row) | ||
async def sso_id_exists(cursor: AsyncCursor, sso_id: str) -> bool: | ||
"""Check if sso id exists. | ||
:param cursor: The database cursor. | ||
:param sso_id: The sso id to check. | ||
:return: True if the sso id exists, otherwise False. | ||
""" | ||
await cursor.execute(*user.sso_id_exists(sso_id)) | ||
exists = bool((await cursor.fetchone())[0]) | ||
return exists | ||
|
||
|
||
@temp_override_row_factory(rows.tuple_row) | ||
async def is_curator_or_higher(cursor: AsyncCursor, user_id: int) -> bool: | ||
"""Check if a user is a curator or higher. | ||
:param cursor: The database cursor. | ||
:param user_id: The user id to check. | ||
:return: True if the user is a curator or higher, otherwise False. | ||
""" | ||
await cursor.execute(*user.is_curator_or_higher(user_id)) | ||
exists = bool((await cursor.fetchone())[0]) | ||
return exists | ||
|
||
|
||
@temp_override_row_factory(rows.tuple_row) | ||
async def is_assigned_curation( | ||
cursor: AsyncCursor, user_id: int, geneset_id: int | ||
) -> bool: | ||
"""Check if a user is assigned curation. | ||
:param cursor: The database cursor. | ||
:param user_id: The user id to check. | ||
:param geneset_id: The geneset id to check. | ||
:return: True if the user is assigned curation, otherwise False. | ||
""" | ||
await cursor.execute(*user.is_assigned_curation(user_id, geneset_id)) | ||
exists = bool((await cursor.fetchone())[0]) | ||
return exists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
"""Query Builder for Users DB functions.""" | ||
|
||
from typing import Tuple | ||
|
||
from geneweaver.db.utils import format_sql_fields | ||
from psycopg.sql import SQL, Composed | ||
|
||
USER_FIELD_MAP = { | ||
"usr_id": "id", | ||
"usr_email": "email", | ||
"user_prefs": "prefs", | ||
"is_guest": "is_guest", | ||
"usr_first_name": "first_name", | ||
"usr_last_name": "last_name", | ||
"usr_admin": "admin", | ||
"usr_last_seen": "last_seen", | ||
"usr_created": "created", | ||
"ip_addr": "ip_address", | ||
"apikey": "api_key", | ||
"usr_sso_id": "sso_id", | ||
} | ||
|
||
USER_FIELDS = format_sql_fields(USER_FIELD_MAP, query_table="usr") | ||
|
||
|
||
USER_QUERY = SQL("SELECT") + SQL(",").join(USER_FIELDS) + SQL("FROM usr") | ||
|
||
|
||
def by_id(user_id: int) -> Tuple[Composed, dict]: | ||
"""Get user by id.""" | ||
query = USER_QUERY + SQL("WHERE usr_id = %(user_id)s;") | ||
params = {"user_id": user_id} | ||
return query.join(" "), params | ||
|
||
|
||
def by_sso_id(sso_id: str) -> Tuple[Composed, dict]: | ||
"""Get user by sso id.""" | ||
query = USER_QUERY + SQL("WHERE usr_sso_id = %(user_id)s;") | ||
params = {"sso_id": sso_id} | ||
return query.join(" "), params | ||
|
||
|
||
def by_email(email: str) -> Tuple[Composed, dict]: | ||
"""Get user by email.""" | ||
query = USER_QUERY + SQL("WHERE usr_email = %(email)s;") | ||
params = {"email": email} | ||
return query.join(" "), params | ||
|
||
|
||
def by_sso_id_and_email(sso_id: str, email: str) -> Tuple[Composed, dict]: | ||
"""Get user by sso id and email.""" | ||
query = USER_QUERY + SQL("WHERE usr_sso_id = %(sso_id)s AND usr_email = %(email)s;") | ||
params = {"sso_id": sso_id, "email": email} | ||
return query.join(" "), params | ||
|
||
|
||
def by_api_key(api_key: str) -> Tuple[Composed, dict]: | ||
"""Get user by api key.""" | ||
query = USER_QUERY + SQL("WHERE apikey = %(api_key)s;") | ||
params = {"api_key": api_key} | ||
return query.join(" "), params | ||
|
||
|
||
def email_exists(email: str) -> Tuple[Composed, dict]: | ||
"""Check if email exists.""" | ||
query = SQL("SELECT") + SQL( | ||
"EXISTS(SELECT 1 FROM usr WHERE usr_email = %(email)s);" | ||
) | ||
params = {"email": email} | ||
return query.join(" "), params | ||
|
||
|
||
def sso_id_exists(sso_id: str) -> Tuple[Composed, dict]: | ||
"""Check if sso id exists.""" | ||
query = SQL("SELECT") + SQL( | ||
"EXISTS(SELECT 1 FROM usr WHERE usr_sso_id = %(sso_id)s);" | ||
) | ||
params = {"sso_id": sso_id} | ||
return query.join(" "), params | ||
|
||
|
||
def is_curator_or_higher__query() -> Composed: | ||
"""Build SQL query to check if user is a curator or higher.""" | ||
return ( | ||
SQL("EXISTS(") | ||
+ SQL("SELECT 1") | ||
+ SQL("FROM usr") | ||
+ SQL("WHERE usr_id = %(user_id)s") | ||
+ SQL("AND usr_admin > 0)") | ||
) | ||
|
||
|
||
def is_assigned_curation__query() -> Composed: | ||
"""Build SQL query to check if user is assigned curation.""" | ||
return ( | ||
SQL("EXISTS(") | ||
+ SQL("SELECT 1") | ||
+ SQL("FROM curation_assignments") | ||
+ SQL("WHERE curator = %(user_id)s") | ||
+ SQL("AND gs_id = %(geneset_id)s") | ||
+ SQL("AND curation_state = 2)") | ||
) | ||
|
||
|
||
def is_curator_or_higher(user_id: int) -> Tuple[Composed, dict]: | ||
"""Check if user is a curator or higher.""" | ||
query = SQL("SELECT") + is_curator_or_higher__query() + SQL(";") | ||
params = {"user_id": user_id} | ||
return query.join(" "), params | ||
|
||
|
||
def is_assigned_curation(user_id: int, geneset_id: int) -> Tuple[Composed, dict]: | ||
"""Check if user is assigned curation.""" | ||
query = SQL("SELECT") + is_assigned_curation__query() + SQL(";") | ||
params = {"user_id": user_id, "geneset_id": geneset_id} | ||
return query.join(" "), params |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.