datentrog/datentrog.py

113 lines
2.6 KiB
Python
Raw Normal View History

2021-11-08 23:23:07 +00:00
#!/usr/bin/env python3
import bottle
2021-11-10 16:20:35 +00:00
import io
import qrcode
import qrcode.image.styledpil as qrstyled
2021-11-08 23:23:07 +00:00
import sqlite3
application = bottle.default_app()
connection = sqlite3.connect("db.sqlite")
cursor = connection.cursor()
2021-11-10 21:17:21 +00:00
cursor.execute(
"""
2021-11-08 23:23:07 +00:00
CREATE TABLE IF NOT EXISTS visitors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
event TEXT,
time DATETIME,
name TEXT,
address TEXT,
contact TEXT
);
2021-11-10 21:17:21 +00:00
"""
)
2021-11-08 23:23:07 +00:00
2021-11-10 16:20:35 +00:00
def url(route):
2021-11-10 21:17:21 +00:00
return "%s://%s/%s" % (
bottle.request.urlparts.scheme,
bottle.request.urlparts.netloc,
route,
)
2021-11-10 16:20:35 +00:00
2021-11-08 23:23:07 +00:00
@application.get("/")
2021-11-09 16:39:36 +00:00
@bottle.view("template/new.tpl")
def new_get():
pass
2021-11-09 03:38:04 +00:00
2021-11-09 16:39:36 +00:00
@application.post("/")
@bottle.view("template/new.tpl")
def new_post():
event = bottle.request.forms.get("event", "")
if event:
2021-11-10 16:20:35 +00:00
bottle.redirect(url("qr/" + event))
2021-11-09 16:39:36 +00:00
return dict(event=event, feedback=True)
2021-11-08 23:23:07 +00:00
2021-11-09 16:39:36 +00:00
@application.get("/static/<file:path>")
def static_get(file):
return bottle.static_file(file, root="static")
2021-11-09 03:38:04 +00:00
2021-11-10 16:20:35 +00:00
@application.get("/qr/<event>")
@bottle.view("template/qr.tpl")
def qr_get(event):
return dict(event=event, url=url("event/" + event))
@application.get("/qr/img/<event>.png")
def qr_img_get(event):
qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=20)
qr.add_data(url("event/" + event))
2021-11-10 21:17:21 +00:00
image = qr.make_image(
image_factory=qrstyled.StyledPilImage,
embeded_image_path="heizhaus_logo_square.png",
)
2021-11-10 16:20:35 +00:00
buffer = io.BytesIO()
image.save(buffer, format="PNG")
bottle.response.content_type = "image/png"
return buffer.getvalue()
2021-11-09 03:38:04 +00:00
@application.get("/event/<event>")
2021-11-09 16:39:36 +00:00
@bottle.view("template/event.tpl")
2021-11-09 03:38:04 +00:00
def event_get(event):
return dict(event=event)
@application.post("/event/<event>")
2021-11-09 16:39:36 +00:00
@bottle.view("template/event.tpl")
2021-11-09 03:38:04 +00:00
def event_post(event):
2021-11-09 16:39:36 +00:00
data = dict(feedback=True, success=True)
2021-11-08 23:23:07 +00:00
for field in ("event", "name", "address", "contact"):
data[field] = bottle.request.forms.get(field, "")
data["success"] = data["success"] and data[field]
if data["success"]:
2021-11-10 21:17:21 +00:00
cursor.execute(
"""
2021-11-08 23:23:07 +00:00
INSERT INTO visitors (event, time, name, address, contact)
VALUES (:event, datetime('now'), :name, :address, :contact);
2021-11-10 21:17:21 +00:00
""",
data,
)
2021-11-08 23:23:07 +00:00
connection.commit()
return data
@application.get("/cleanup")
def cleanup():
2021-11-10 21:17:21 +00:00
cursor.execute(
"""
2021-11-08 23:23:07 +00:00
DELETE FROM visitors WHERE time < datetime('now', '-1 month');
2021-11-10 21:17:21 +00:00
"""
)
2021-11-08 23:23:07 +00:00
connection.commit()
if __name__ == "__main__":
bottle.run(host="0.0.0.0", port=8080)
else:
application.catchall = False