Result pagination

pull/414/head
HeNine 3 years ago
parent c443795be3
commit 2461924d9f

@ -81,9 +81,12 @@ def get_json():
# I think websearch_to_tsquery() sanitizes its own input.
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()
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(),
"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])
def fetch_lines(db_conn, start_time, end_time, query=None):
if query is None:
return database.query(db_conn, "SELECT * FROM buscribe_transcriptions WHERE "
"start_time > %s AND "
"end_time < %s;",
start_time if start_time is not None else '-infinity',
end_time if end_time is not None else 'infinity')
else:
return database.query(db_conn, "SELECT * FROM buscribe_transcriptions WHERE "
"start_time > %(start_time)s AND "
"end_time < %(end_time)s AND "
"to_tsvector(transcription_line) @@ websearch_to_tsquery(%(text_query)s) "
"ORDER BY ts_rank_cd(to_tsvector(transcription_line), websearch_to_tsquery(%(text_query)s)) DESC, "
"start_time;",
def fetch_lines(db_conn, start_time, end_time, ts_query=None, limit=None, offset=None):
query = "SELECT * FROM buscribe_transcriptions WHERE start_time > %(start_time)s AND end_time < %(end_time)s "
if ts_query is not None:
query += "AND to_tsvector(transcription_line) @@ websearch_to_tsquery(%(text_query)s) " \
"ORDER BY ts_rank_cd(to_tsvector(transcription_line), websearch_to_tsquery(%(text_query)s)) DESC, " \
"start_time"
if limit is not None:
query += "LIMIT %(limit)s"
if offset is not None:
query += "OFFSET %(limit)s"
query += ";"
print(query)
return database.query(db_conn, query,
start_time=start_time if start_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