Result pagination

borken-flask
HeNine 3 years ago
parent c443795be3
commit 2461924d9f

@ -81,9 +81,12 @@ def get_json():
# I think websearch_to_tsquery() sanitizes its own input. # I think websearch_to_tsquery() sanitizes its own input.
query = request.args.get('query', default=None) query = request.args.get('query', default=None)
limit = request.args.get('limit', default=None, type=int)
offset = request.args.get('offset', default=None, type=int)
db_conn = app.db_manager.get_conn() db_conn = app.db_manager.get_conn()
results = fetch_lines(db_conn, start_time, end_time, query) results = fetch_lines(db_conn, start_time, end_time, query, limit, offset)
return jsonify([{"start_time": row.start_time.isoformat(), return jsonify([{"start_time": row.start_time.isoformat(),
"start_bus_time": round_bus_time(row.start_time - app.bustime_start), "start_bus_time": round_bus_time(row.start_time - app.bustime_start),
@ -92,20 +95,28 @@ def get_json():
"text": row.transcription_line} for row in results]) "text": row.transcription_line} for row in results])
def fetch_lines(db_conn, start_time, end_time, query=None): def fetch_lines(db_conn, start_time, end_time, ts_query=None, limit=None, offset=None):
if query is None: query = "SELECT * FROM buscribe_transcriptions WHERE start_time > %(start_time)s AND end_time < %(end_time)s "
return database.query(db_conn, "SELECT * FROM buscribe_transcriptions WHERE "
"start_time > %s AND " if ts_query is not None:
"end_time < %s;", query += "AND to_tsvector(transcription_line) @@ websearch_to_tsquery(%(text_query)s) " \
start_time if start_time is not None else '-infinity', "ORDER BY ts_rank_cd(to_tsvector(transcription_line), websearch_to_tsquery(%(text_query)s)) DESC, " \
end_time if end_time is not None else 'infinity') "start_time"
else:
return database.query(db_conn, "SELECT * FROM buscribe_transcriptions WHERE " if limit is not None:
"start_time > %(start_time)s AND " query += "LIMIT %(limit)s"
"end_time < %(end_time)s AND "
"to_tsvector(transcription_line) @@ websearch_to_tsquery(%(text_query)s) " if offset is not None:
"ORDER BY ts_rank_cd(to_tsvector(transcription_line), websearch_to_tsquery(%(text_query)s)) DESC, " query += "OFFSET %(limit)s"
"start_time;",
query += ";"
print(query)
return database.query(db_conn, query,
start_time=start_time if start_time is not None else '-infinity', start_time=start_time if start_time is not None else '-infinity',
end_time=end_time if end_time is not None else 'infinity', end_time=end_time if end_time is not None else 'infinity',
text_query=query) text_query=ts_query,
limit=limit,
offset=offset
)

Loading…
Cancel
Save