34 lines
915 B
Python
34 lines
915 B
Python
import sqlite3
|
|
import random
|
|
from flask import Flask, g, request, render_template, make_response
|
|
|
|
app = Flask(__name__)
|
|
|
|
def get_db():
|
|
db = getattr(g, '_database', None)
|
|
if db is None:
|
|
db = g._database = sqlite3.connect("./web.db")
|
|
return db
|
|
|
|
@app.route("/", methods = ['GET', 'POST'])
|
|
def root():
|
|
request
|
|
user_cookie = request.cookies.get("userid")
|
|
if request.method == "POST":
|
|
print(str(request.form))
|
|
if request.method == "GET":
|
|
posts = get_db().cursor().execute(f"select * from posts").fetchall()
|
|
|
|
resp = make_response(render_template('./index.html', posts=posts))
|
|
if user_cookie is None:
|
|
resp.set_cookie("userid", str(random.randint(100000, 999999)))
|
|
return resp
|
|
|
|
return "YES"
|
|
|
|
@app.teardown_appcontext
|
|
def close_connection(exception):
|
|
db = getattr(g, '_database', None)
|
|
if db is not None:
|
|
db.close()
|
|
|