125 lines
4.1 KiB
Python

import os
import pickle
import json
from flask import Flask, redirect, url_for, render_template, request, flash, session, g
from .src import host_lookup
from dotenv import load_dotenv
from markupsafe import escape
from pprint import pprint
import logging
# 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")):
# Clean this up with less loops and more (imported) functions
user_input = escape(request.form.get("host").strip())
hosts, errors = host_lookup.process_input(user_input)
logging.debug("hosts are: ", hosts, "errors are: ", errors)
# for host in hosts:
# result = host_lookup.Lookedup(host)
# results.append(result)
results = build_results(hosts)
test = 7
session["test"] = test
session["results"] = pickle.dumps(results)
### TESTING
build_history(results)
# session["results"] = pickle.dumps(results)
# if "history" in session:
# print("HISTORY IS IN SESSION")
# current_history = pickle.loads(session["history"])
# for result in results:
# if result not in current_history:
# current_history.append(result)
# session["history"] = pickle.dumps(current_history)
# elif "history" not in session:
# print("HISTORY IS NOT IN SESSION")
# session["history"] = pickle.dumps(results)
### END TESTING
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")
def build_results(hosts):
results = []
for host in hosts:
result = host_lookup.Lookedup(host)
results.append(result)
return results
def build_history(results):
if "history" in session:
current_history = pickle.loads(session["history"])
for result in results:
if result.host not in current_history:
current_history.append(result)
session["history"] = pickle.dumps(current_history)
elif "history" not in session:
session["history"] = pickle.dumps(results)
@app.route("/history")
def history():
e = session.get("results", None)
print(type(e))
hosts = pickle.loads(e)
session_history = pickle.loads(session["history"])
return render_template(
"history.html", hosts=hosts, session_history=session_history
)
@app.route("/history", methods=["POST"])
def clear_history():
if request.method == "POST" and request.form.get("clear_history"):
logging.debug("button was pressed")
session.clear()
return render_template("history.html")
def add_to_history():
pass
return app