# -*- coding: utf-8 -*- """ Created on Sat Apr 22 17:07:13 2022 @author: Hammerle """ import time import json import requests import logging from flask import Flask, render_template, request, flash from werkzeug.middleware.proxy_fix import ProxyFix FOLDER = "src/" # docker # FOLDER = "" # local app = Flask(__name__) app.wsgi_app = ProxyFix(app.wsgi_app) # w_log = logging.getLogger("werkzeug") # w_log.disabled = True app.logger.setLevel(logging.INFO) # app.config["TEMPLATES_AUTO_RELOAD"] = True with open(f"{FOLDER}secret_key", "rb") as secretKeyFile: app.secret_key = secretKeyFile.read() @app.route("/", methods=["GET"]) def index(): welcome_cards = read_database("welcome") return render_template("index.html", cards=welcome_cards) @app.route("/karte", methods=["GET"]) def map_(): marker_groups = read_database("map") return render_template("map.html", marker_groups=marker_groups) @app.route("/schritte", methods=["GET"]) def steps(): steps = read_database("steps") return render_template("steps.html", steps=steps) @app.route("/events", methods=["GET"]) def events(): boudicca_req = requests.get("https://api.boudicca.events/events") events = [] if boudicca_req.status_code == 200: events = boudicca_req.json() return render_template('events.html', events=events) @app.route("/mitgestalten", methods=["GET", "POST"]) def contribute(): if request.method == "POST": contribution = { "fullname": request.form.get("fullname"), "email": request.form.get("email"), "message": request.form.get("message"), "timestamp": time.time() } add_to_database("contribute", contribution) logging.info(contribution) app.logger.info(contribution) flash("Danke für's Mitmachen!", "success") return render_template("contribute.html") def read_database(db_name): with open(f"{FOLDER}database/db_{db_name}.json") as file: db_data = json.load(file) return db_data def add_to_database(db_name, data_to_add): db_data = read_database(db_name) db_data.append(data_to_add) with open(f"{FOLDER}database/db_{db_name}.json", "w") as file: json.dump(db_data, file, indent=4) if __name__ == "__main__": # app.run() # localhost app.run(host="0.0.0.0") # in network