2025-04-15 14:32:48 +02:00
|
|
|
import os
|
2025-05-29 21:15:30 +02:00
|
|
|
import pickle
|
2025-04-15 15:28:48 +02:00
|
|
|
from flask import Flask, redirect, url_for, render_template, request, flash
|
|
|
|
from .src import host_lookup
|
2025-04-15 14:32:48 +02:00
|
|
|
from dotenv import load_dotenv
|
2025-04-15 15:28:48 +02:00
|
|
|
from markupsafe import escape
|
2025-05-09 15:18:19 +02:00
|
|
|
from pprint import pprint
|
2025-04-15 14:32:48 +02:00
|
|
|
|
|
|
|
# Instead of turning 'lookup' into a Blueprint, maybe I could make a history of previous lookups a Blueprint?
|
|
|
|
|
|
|
|
|
|
|
|
def create_app(test_config=None):
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.config.from_pyfile("config.py")
|
|
|
|
load_dotenv()
|
|
|
|
app.secret_key = os.getenv("SECRET_KEY")
|
|
|
|
|
|
|
|
if test_config is None:
|
|
|
|
app.config.from_pyfile("config.py", silent=True)
|
|
|
|
else:
|
|
|
|
app.config.update(test_config)
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.makedirs(app.instance_path)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def index():
|
2025-04-15 15:28:48 +02:00
|
|
|
# return render_template("index.html")
|
|
|
|
return redirect(url_for("lookup"))
|
|
|
|
|
|
|
|
@app.route("/lookup", methods=(["GET", "POST"]))
|
|
|
|
def lookup():
|
|
|
|
hosts = []
|
|
|
|
results = []
|
2025-05-09 15:18:19 +02:00
|
|
|
errors_present = False
|
|
|
|
results_present = False
|
2025-04-15 15:28:48 +02:00
|
|
|
if request.method == "GET":
|
|
|
|
return render_template("lookup.html")
|
|
|
|
elif request.method == "POST" and any(request.form.get("host")):
|
|
|
|
user_input = escape(request.form.get("host").strip())
|
|
|
|
hosts, errors = host_lookup.process_input(user_input)
|
|
|
|
for host in hosts:
|
|
|
|
result = host_lookup.Lookedup(host)
|
|
|
|
results.append(result)
|
2025-05-09 15:18:19 +02:00
|
|
|
if any(results):
|
|
|
|
results_present = True
|
|
|
|
if any(errors):
|
|
|
|
errors_present = True
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"results.html",
|
|
|
|
hosts=results,
|
|
|
|
errors=errors,
|
|
|
|
errors_present=errors_present,
|
|
|
|
results_present=results_present,
|
|
|
|
)
|
2025-05-29 21:15:30 +02:00
|
|
|
|
2025-04-15 15:28:48 +02:00
|
|
|
else:
|
|
|
|
flash("Nothing entered! Try again?", "error")
|
|
|
|
return render_template("lookup.html")
|
|
|
|
|
2025-05-09 15:18:19 +02:00
|
|
|
@app.route("/history")
|
|
|
|
def history():
|
|
|
|
return render_template("history.html")
|
2025-04-15 14:32:48 +02:00
|
|
|
|
|
|
|
return app
|