Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 8 additions & 19 deletions src/pir_pipeline/dashboard/finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from pir_pipeline.dashboard.db import get_db
from pir_pipeline.dashboard.utils import administrator
from pir_pipeline.utils.dashboard_utils import get_search_query
from pir_pipeline.utils.SQLAlchemyUtils import SQLAlchemyUtils

bp = Blueprint("finalize", __name__, url_prefix="/finalize")
Expand Down Expand Up @@ -96,32 +97,21 @@ def search_pending_questions(keyword: str, db: SQLAlchemyUtils) -> dict:
proposed_changes = db.tables["finalize"]
link_history = db.tables["link_history"]

# Escape keyword (https://stackoverflow.com/questions/4202538/escape-special-characters-in-a-python-string)
keyword = re.escape(keyword)

# Adapted from
# https://stackoverflow.com/questions/34838302/sqlalchemy-adding-or-condition-with-different-filter
conditions = []
for column in proposed_changes.c.keys():
if db.engine.dialect.name == "mysql":
conditions.append(
proposed_changes.c[column].regexp_match(bindparam("keyword"))
)
else:
conditions.append(
func.cast(proposed_changes.c[column], String).regexp_match(
func.cast(bindparam("keyword"), String), "i"
)
)
search_query, _ = get_search_query(keyword, db)
search_query = select(search_query.c.question_id).distinct().cte("sq")

query = (
select(proposed_changes, link_history.c.user)
.where(or_(*conditions))
.join(
link_history,
link_history.c.link_id == proposed_changes.c.id,
isouter=True,
)
.cte("q")
)

query = select(query).join(
search_query, query.c.question_id == search_query.c.question_id
)

return query, keyword
Expand Down Expand Up @@ -182,6 +172,5 @@ def data():
keyword = request.form["keyword-search"]
query, keyword = search_pending_questions(keyword, db)
records = db.get_records(query, {"keyword": keyword})
print(query, records)

return records.to_dict(orient="index")
73 changes: 61 additions & 12 deletions src/pir_pipeline/utils/dashboard_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
BinaryExpression,
String,
Subquery,
Table,
TableClause,
and_,
bindparam,
Expand Down Expand Up @@ -64,22 +65,25 @@ def get_matches(payload: dict, db: SQLAlchemyUtils) -> list:
return records


def get_search_results(
def build_filter(filters: list[str], table: Table):
final = []
for filter in filters:
variable, value = filter.split(":")
if value.isnumeric():
final.append(table.c[variable] == int(value))
else:
final.append(table.c[variable] == value)

return and_(*final)


def get_search_query(
keyword: str,
db: SQLAlchemyUtils,
id_column: str = "question_id",
years: list[int] = [],
) -> dict:
"""Return results for the search page
):

Args:
keyword (str): The term to search for
db (SQLAlchemyUtils): SQLAlchemyUtils object for database interactions
id_column (str): Column to use as the primary identifier

Returns:
dict: Dictionary of search results
"""
table = "question"
table = db.tables[table]

Expand All @@ -96,6 +100,9 @@ def get_search_results(
columns = tuple(columns.keys())

# Escape keyword (https://stackoverflow.com/questions/4202538/escape-special-characters-in-a-python-string)
filters = re.findall(r"\w+:\w+", keyword)
filters = [re.escape(filter) for filter in filters]
keyword = re.split(r"\w+:\w+", keyword)[-1].strip()
keyword = re.escape(keyword)

# Create regex match query
Expand All @@ -114,7 +121,11 @@ def get_search_results(
)
)

keyword_query = keyword_query.where(or_(*conditions)).order_by(
where_condition = or_(*conditions)
if filters:
where_condition = and_(build_filter(filters, table), where_condition)

keyword_query = keyword_query.where(where_condition).order_by(
table.c["uqid"], table.c["year"].desc()
)
if years:
Expand Down Expand Up @@ -147,6 +158,44 @@ def get_search_results(
)
.order_by(table.c["uqid"], table.c["year"].desc())
)

return search_query, search_dict


def get_search_results(
keyword: str,
db: SQLAlchemyUtils,
id_column: str = "question_id",
years: list[int] = [],
) -> dict:
"""Return results for the search page

Args:
keyword (str): The term to search for
db (SQLAlchemyUtils): SQLAlchemyUtils object for database interactions
id_column (str): Column to use as the primary identifier

Returns:
dict: Dictionary of search results
"""
table = "question"
table = db.tables[table]

columns = [
"question_id",
"uqid",
"year",
"question_number",
"question_name",
"question_text",
]

columns = OrderedDict([(col, None) for col in columns])
columns = tuple(columns.keys())

search_query, search_dict = get_search_query(keyword, db, id_column, years)

with db.engine.connect() as conn:
result = conn.execute(search_query)

# Convert results to dictionary
Expand Down
Loading