From 624b5bd69deb2acfc1b59f6303da7789ed691dc8 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sun, 31 Dec 2023 12:47:10 -0500 Subject: [PATCH] Add menorah subdomain --- assets/settings.json | 3 +++ assets/settings.json.license | 2 ++ flask_app/__init__.py | 38 ++++++++++++++++++++++++++++++++-- flask_app/forms.py | 19 +++++++++++++++++ flask_app/helpers.py | 15 ++++++++++++++ flask_app/templates/index.html | 25 ++++++++++++++++++++++ 6 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 assets/settings.json create mode 100644 assets/settings.json.license create mode 100644 flask_app/forms.py create mode 100644 flask_app/helpers.py create mode 100644 flask_app/templates/index.html diff --git a/assets/settings.json b/assets/settings.json new file mode 100644 index 0000000..1f78d28 --- /dev/null +++ b/assets/settings.json @@ -0,0 +1,3 @@ +{ + "zipcode": "{{ zipcode }}" +} diff --git a/assets/settings.json.license b/assets/settings.json.license new file mode 100644 index 0000000..9a4339e --- /dev/null +++ b/assets/settings.json.license @@ -0,0 +1,2 @@ +SPDX-FileCopyrightText: 2023 Alec Delaney +SPDX-License-Identifier: MIT diff --git a/flask_app/__init__.py b/flask_app/__init__.py index 4b6d2de..9b6eb3f 100644 --- a/flask_app/__init__.py +++ b/flask_app/__init__.py @@ -7,12 +7,46 @@ Author: Alec Delaney """ -from flask import Flask +import json +import tempfile +import io +import jinja2 + +from flask import Flask, render_template, send_file, after_this_request +from flask_bootstrap import Bootstrap5 + +from flask_app.forms import MenorahSetupForm +from flask_app.helpers import generate_settings_json app = Flask(__name__) +with open("/etc/config.json", encoding="utf-8") as jsonfile: + config = json.load(jsonfile) +app.config["SECRET_KEY"] = config["SECRET_KEY"] + +bootstrap = Bootstrap5(app) + @app.route("/") def index(): """Route for index (landing page)""" - return "

Hello, world!

" + return "

Hello, world!

" + + +@app.route("/", subdomain="menorah", methods=["GET", "POST"]) +def menorah_indexindex(): + """Route for index (landing page) for menorah subdomain""" + input_form = MenorahSetupForm() + if input_form.validate_on_submit(): + zipcode = input_form.data["zipcode"] + with open("assets/settings.json", mode="r", encoding="utf-8") as template_file: + template_text = template_file.read() + template = jinja2.Template(template_text) + rendered_temp = template.render(zipcode=zipcode) + file_bytesio = io.BytesIO() + file_bytesio.write(rendered_temp.encode("utf-8")) + file_bytesio.seek(0) + return send_file( + file_bytesio, as_attachment=True, download_name="settings.json" + ) + return render_template("index.html", input_form=input_form) diff --git a/flask_app/forms.py b/flask_app/forms.py new file mode 100644 index 0000000..7550a9d --- /dev/null +++ b/flask_app/forms.py @@ -0,0 +1,19 @@ +# SPDX-FileCopyrightText: 2023 Alec Delaney +# SPDX-License-Identifier: MIT + +""" +WTForms used in the Flask application + +Author: Alec Delaney +""" + +from flask_wtf import FlaskForm +from wtforms import StringField, SubmitField +from wtforms.validators import DataRequired + + +class MenorahSetupForm(FlaskForm): + """Form for menorah information""" + + zipcode = StringField("Zip Code", validators=[DataRequired()]) + submit = SubmitField("Generate file...") diff --git a/flask_app/helpers.py b/flask_app/helpers.py new file mode 100644 index 0000000..6e266d9 --- /dev/null +++ b/flask_app/helpers.py @@ -0,0 +1,15 @@ +# SPDX-FileCopyrightText: 2023 Alec Delaney +# SPDX-License-Identifier: MIT + +""" +Helper functions used by the Flask application + +Author: Alec Delaney +""" + +import json + + +def generate_settings_json(zipcode: str): + """Generate a settings file for the CircuiyPythonukiah""" + return json.dumps({"zipcode": zipcode}) diff --git a/flask_app/templates/index.html b/flask_app/templates/index.html new file mode 100644 index 0000000..954f9f1 --- /dev/null +++ b/flask_app/templates/index.html @@ -0,0 +1,25 @@ + + +{% from 'bootstrap5/form.html' import render_form %} + + + + + + + {{ bootstrap.load_css() }} + + +

Hello, world!

+
+ {{ render_form(input_form) }} +
+ {% if download_file %} + { render_form(download_form) } + {% endif %} + {{ bootstrap.load_js() }} + +