app.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sat Apr 22 17:07:13 2022
  4. @author: Hammerle
  5. """
  6. import time
  7. import json
  8. import requests
  9. from flask import Flask, render_template, request, flash
  10. # FOLDER = "src/" # docker
  11. FOLDER = "" # local
  12. app = Flask(__name__)
  13. app.config["TEMPLATES_AUTO_RELOAD"] = True
  14. with open(f"{FOLDER}secret_key", "rb") as secretKeyFile:
  15. app.secret_key = secretKeyFile.read()
  16. @app.route("/", methods=["GET"])
  17. def index():
  18. welcome_cards = read_database("welcome")
  19. return render_template("index.html", cards=welcome_cards)
  20. @app.route("/karte", methods=["GET"])
  21. def map_():
  22. marker_groups = read_database("map")
  23. return render_template("map.html", marker_groups=marker_groups)
  24. @app.route("/schritte", methods=["GET"])
  25. def steps():
  26. steps = read_database("steps")
  27. return render_template("steps.html", steps=steps)
  28. @app.route("/events", methods=["GET"])
  29. def events():
  30. boudicca_req = requests.get("https://api.boudicca.events/events")
  31. events = []
  32. if boudicca_req.status_code == 200:
  33. events = boudicca_req.json()
  34. return render_template('events.html', events=events)
  35. @app.route("/mitgestalten", methods=["GET", "POST"])
  36. def contribute():
  37. if request.method == "POST":
  38. contribution = {
  39. "fullname": request.form.get("fullname"),
  40. "email": request.form.get("email"),
  41. "message": request.form.get("message"),
  42. "timestamp": time.time()
  43. }
  44. add_to_database("contribute", contribution)
  45. flash("Danke für's Mitmachen!", "success")
  46. return render_template("contribute.html")
  47. def read_database(db_name):
  48. with open(f"{FOLDER}database/db_{db_name}.json") as file:
  49. db_data = json.load(file)
  50. return db_data
  51. def add_to_database(db_name, data_to_add):
  52. db_data = read_database(db_name)
  53. db_data.append(data_to_add)
  54. with open(f"{FOLDER}database/db_{db_name}.json", "w") as file:
  55. json.dump(db_data, file, indent=4)
  56. if __name__ == "__main__":
  57. app.run() # localhost
  58. # app.run(host="0.0.0.0") # in network