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("/")
|
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:
|
|
|
|
bottle.redirect("event/" + event)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
@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"]:
|
|
|
|
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
|