Poll View: Query Recent Messages
from flask import request def gen_poll(db): # Get the client position req = int(request.args.get('head',1)) # Find the maximum message number. t = db.execute("SELECT max(id) FROM comments").fetchone() if t[0] is None: head = 1 else: head = t[0] + 1 # Find the minium start = max(1, req, head-10) # Fetch the messages. t = db.execute("SELECT * FROM comments " + f"WHERE {start} <= id and id < {head} ORDER BY id") handles = [] messages = [] for row in t: handles += [ row[1] ] messages += [ row[2] ] return { "head": head, "messages": messages, "ids": handles } def mk_poll(app): @app.route("/poll") def poll(): from . import db return gen_poll(db.get_db())