From fe996eb0acd126b16b635f1007713dda048e6c90 Mon Sep 17 00:00:00 2001 From: Joost Agterhoek Date: Tue, 10 Jun 2025 21:03:44 +0200 Subject: [PATCH] added history function, now need to deduplicate history items --- README.md | 4 +-- flask_soc_site/__init__.py | 70 ++++++++++++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d319a9f..019cc58 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/flask_soc_site/__init__.py b/flask_soc_site/__init__.py index 4135ac4..2b51f12 100644 --- a/flask_soc_site/__init__.py +++ b/flask_soc_site/__init__.py @@ -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