import os import pickle from flask import Flask, redirect, url_for, render_template, request, flash from .src import host_lookup from dotenv import load_dotenv from markupsafe import escape from pprint import pprint # 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(): # return render_template("index.html") return redirect(url_for("lookup")) @app.route("/lookup", methods=(["GET", "POST"])) def lookup(): hosts = [] results = [] errors_present = False results_present = False 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) 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, ) else: flash("Nothing entered! Try again?", "error") return render_template("lookup.html") @app.route("/history") def history(): return render_template("history.html") return app