def main(): args = sys.argv[1:] if args: if args[0] == "init_db": Base.metadata.create_all(engine) if args[0] == "add": paste = Paste("<html>hi</html>", lexer="html", expiry=timedelta(seconds=5)) session.add(paste) session.commit() if args[0] == "remove": paste = session.query(Paste).filter(Paste.id == int(args[1])).first() session.delete(paste) session.commit() if args[0] == "list": for paste in session.query(Paste).all(): print(paste) if args[0] == "reap": pastes = session.query(Paste).filter(Paste.exp_date < datetime.utcnow()).all() for paste in pastes: session.delete(paste) session.commit() print("Reaped {} expired pastes".format(len(pastes)))
def remove(removal_id): paste = session.query(Paste).filter(Paste.removal_id == removal_id).first() if not paste: return render_template("404.html"), 404 session.delete(paste) session.commit() return redirect(url_for("index"))
def raw(paste_id): paste = session.query(Paste).filter(Paste.paste_id == paste_id).first() if not paste: return render_template("404.html"), 404 response = make_response(paste.raw) response.headers["content-type"] = "text/plain" return response
def show(paste_id): paste = session.query(Paste).filter(Paste.paste_id == paste_id).first() if not paste: return render_template("404.html"), 404 can_delete = request.cookies.get("removal") == str(paste.removal_id) return render_template("show.html", paste=paste, pagetitle="show", can_delete=can_delete)
def remove_json(): paste = session.query(Paste).filter(Paste.removal_id == request.form["removal_id"]).first() if not paste: return "not found", 404 session.delete(paste) session.commit() response = make_response(json.dumps([{"paste_id": paste.paste_id, "status": "removed"}])) response.headers["content-type"] = "application/json" return response
def show_json(paste_id): paste = session.query(Paste).filter(Paste.paste_id == paste_id).first() if not paste: return "not found", 404 response = make_response(json.dumps({"paste_id": paste.paste_id, "raw": paste.raw, "fmt": paste.fmt, "lexer": paste.lexer, "expiry": paste.exp_date.isoformat()})) response.headers["content-type"] = "application/json" return response