added history function, now need to deduplicate history items

This commit is contained in:
Joost Agterhoek 2025-06-10 21:03:44 +02:00
parent 9d082e4264
commit fe996eb0ac
2 changed files with 65 additions and 9 deletions

View File

@ -16,6 +16,6 @@ This is a Python project to learn working with Flask. To make it useful for my d
- [x] Follow the example of the working 'packaging-flask-soc-site'-folder ánd incorporate config.py 'from config import config')
- [ ] Implement caching across the entire website (now only for AbuseIPDB API requests)
- [x] Rewrite lookup.html with grid and flex like in v3
- [ ] Add lookup history and note taking page (clickable options at the top)
- [ ] Investigate single-page-app (AJAX) f.e. https://iq.opengenus.org/single-page-application-with-flask-ajax/
- [x] Add lookup history and note taking page (clickable options at the top)
- [-] Investigate single-page-app (AJAX) f.e. https://iq.opengenus.org/single-page-application-with-flask-ajax/
- [ ] Think about and handle security risks (research rate limiting in Nginx)

View File

@ -1,10 +1,12 @@
import os
import pickle
from flask import Flask, redirect, url_for, render_template, request, flash
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?
@ -33,22 +35,42 @@ def create_app(test_config=None):
@app.route("/lookup", methods=(["GET", "POST"]))
def lookup():
hosts = []
results = []
# 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)
for host in hosts:
result = host_lookup.Lookedup(host)
results.append(result)
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,
@ -61,8 +83,42 @@ def create_app(test_config=None):
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():
return render_template("history.html")
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