Skip to content

Commit

Permalink
Merge pull request #1 from tekktrik/dev/menorah
Browse files Browse the repository at this point in the history
Add menorah subdomain
  • Loading branch information
tekktrik authored Dec 31, 2023
2 parents 7acb5f6 + 624b5bd commit 3a4346f
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 2 deletions.
3 changes: 3 additions & 0 deletions assets/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"zipcode": "{{ zipcode }}"
}
2 changes: 2 additions & 0 deletions assets/settings.json.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: 2023 Alec Delaney
SPDX-License-Identifier: MIT
38 changes: 36 additions & 2 deletions flask_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<p>Hello, world!</p>"
return "<h1>Hello, world!</h1>"


@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)
19 changes: 19 additions & 0 deletions flask_app/forms.py
Original file line number Diff line number Diff line change
@@ -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...")
15 changes: 15 additions & 0 deletions flask_app/helpers.py
Original file line number Diff line number Diff line change
@@ -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})
25 changes: 25 additions & 0 deletions flask_app/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!--
SPDX-FileCopyrightText: 2023 Alec Delaney
SPDX-License-Identifier: MIT
-->

{% from 'bootstrap5/form.html' import render_form %}

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
{{ bootstrap.load_css() }}
</head>
<body>
<h1>Hello, world!</h1>
<br/>
{{ render_form(input_form) }}
<br/>
{% if download_file %}
{ render_form(download_form) }
{% endif %}
{{ bootstrap.load_js() }}
</body>
</html>

0 comments on commit 3a4346f

Please sign in to comment.