app.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. import logging
  10. from flask import Flask, render_template, request, flash
  11. from werkzeug.middleware.proxy_fix import ProxyFix
  12. FOLDER = "src/" # docker
  13. # FOLDER = "" # local
  14. app = Flask(__name__)
  15. app.wsgi_app = ProxyFix(app.wsgi_app)
  16. # w_log = logging.getLogger("werkzeug")
  17. # w_log.disabled = True
  18. app.logger.setLevel(logging.INFO)
  19. # app.config["TEMPLATES_AUTO_RELOAD"] = True
  20. with open(f"{FOLDER}secret_key", "rb") as secretKeyFile:
  21. app.secret_key = secretKeyFile.read()
  22. @app.route("/", methods=["GET"])
  23. def index():
  24. welcome_cards = read_database("welcome")
  25. return render_template("index.html", cards=welcome_cards)
  26. @app.route("/karte", methods=["GET"])
  27. def map_():
  28. marker_groups = read_database("map")
  29. return render_template("map.html", marker_groups=marker_groups)
  30. @app.route("/schritte", methods=["GET"])
  31. def steps():
  32. steps = read_database("steps")
  33. return render_template("steps.html", steps=steps)
  34. @app.route("/events", methods=["GET"])
  35. def events():
  36. boudicca_req = requests.get("https://api.boudicca.events/events")
  37. events = []
  38. if boudicca_req.status_code == 200:
  39. events = boudicca_req.json()
  40. return render_template('events.html', events=events)
  41. @app.route("/mitgestalten", methods=["GET", "POST"])
  42. def contribute():
  43. if request.method == "POST":
  44. contribution = {
  45. "fullname": request.form.get("fullname"),
  46. "email": request.form.get("email"),
  47. "message": request.form.get("message"),
  48. "timestamp": time.time()
  49. }
  50. add_to_database("contribute", contribution)
  51. logging.info(contribution)
  52. app.logger.info(contribution)
  53. flash("Danke für's Mitmachen!", "success")
  54. return render_template("contribute.html")
  55. def read_database(db_name):
  56. with open(f"{FOLDER}database/db_{db_name}.json") as file:
  57. db_data = json.load(file)
  58. return db_data
  59. def add_to_database(db_name, data_to_add):
  60. db_data = read_database(db_name)
  61. db_data.append(data_to_add)
  62. with open(f"{FOLDER}database/db_{db_name}.json", "w") as file:
  63. json.dump(db_data, file, indent=4)
  64. if __name__ == "__main__":
  65. # app.run() # localhost
  66. app.run(host="0.0.0.0") # in network