datentrog/datentrog.py

55 lines
1.4 KiB
Python
Raw Normal View History

2021-11-08 23:23:07 +00:00
#!/usr/bin/env python3
import bottle
import sqlite3
application = bottle.default_app()
connection = sqlite3.connect("db.sqlite")
cursor = connection.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS visitors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
event TEXT,
time DATETIME,
name TEXT,
address TEXT,
contact TEXT
);
""")
@application.get("/")
@bottle.view("datentrog.tpl")
def datentrog_get():
return dict(event=bottle.request.query.get("event", "Heizhaus"))
@application.post("/")
@bottle.view("datentrog.tpl")
def datentrog_post():
data = {"feedback": True, "success": True}
for field in ("event", "name", "address", "contact"):
data[field] = bottle.request.forms.get(field, "")
data["success"] = data["success"] and data[field]
if data["success"]:
cursor.execute("""
INSERT INTO visitors (event, time, name, address, contact)
VALUES (:event, datetime('now'), :name, :address, :contact);
""", data)
connection.commit()
return data
@application.get("/cleanup")
def cleanup():
cursor.execute("""
DELETE FROM visitors WHERE time < datetime('now', '-1 month');
""")
connection.commit()
if __name__ == "__main__":
bottle.run(host="0.0.0.0", port=8080)
else:
application.catchall = False