Add View: Add New Messages
from flask import request from flask import Response def done(msg, status=200): return Response(msg, mimetype='text/plain', status=status) def perform_add(db): # Get the information to add msg = request.args.get('MSG','') handle = request.args.get('NAME',None) if handle is None: return done('Missing name', 400) # Insert into the database. t = db.execute("INSERT INTO comments (handle, message) VALUES (?, ?)", (handle,msg)) if t.rowcount != 1: return done('Database store failed', 500) else: db.commit() # Attempt to clean old entries. Do it in blocks. Clean when more than # 60, and trim back to 50. Just makes the updates less frequent than # deleting every 51st message. t = db.execute("SELECT max(id), min(id) FROM comments").fetchone() if not (t[0] is None): (max,min) = t if max - min > 60: db.execute("DELETE FROM comments WHERE id < ?", ((max - 50),)) db.commit() return done("OK") def mk_add(app): @app.route("/add") def add(): from . import db return perform_add(db.get_db())