From 234ad6fa0deecfa6d67103c760b873dcee27de25 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Thu, 19 Sep 2024 11:16:19 +0200 Subject: [PATCH] Revert "use flask caching instead of reading csv again" This reverts commit ba7a95736de7c59feb9e5dfea6cfa09b1a214210. --- contactform/app.py | 31 +++++++++++++++---------------- poetry.lock | 28 +--------------------------- pyproject.toml | 1 - 3 files changed, 16 insertions(+), 44 deletions(-) diff --git a/contactform/app.py b/contactform/app.py index 2d3341a..c56fabf 100644 --- a/contactform/app.py +++ b/contactform/app.py @@ -10,7 +10,6 @@ from wtforms.fields import * from flask_wtf import CSRFProtect, FlaskForm from flask_bootstrap import Bootstrap5 -from flask_caching import Cache from label_voucher import print_voucher from label_raffle import print_raffle @@ -19,6 +18,7 @@ Configuration, ServerConfiguration, PrinterConfiguration, + Font, LabelConfiguration, WebsiteConfiguration, ) @@ -32,9 +32,6 @@ csrf = CSRFProtect(app) bootstrap = Bootstrap5(app) -# Initialize a cache to suppress duplicates -cache = Cache(app, config={"CACHE_TYPE": "SimpleCache", "CACHE_DEFAULT_TIMEOUT": 300}) - # Basic styling app.config["BOOTSTRAP_BOOTSWATCH_THEME"] = "sandstone" app.config["BOOTSTRAP_BTN_SIZE"] = "lg" @@ -84,14 +81,18 @@ class ConfigForm(FlaskForm): submit = SubmitField("Save Changes") -def cache_submitted_email(email): - """Cache the submitted email to avoid duplicates.""" - cache.set(email, True) - - -def is_duplicate_submission(email): - """Check if the email has already been submitted using cache.""" - return cache.get(email) is not None +def is_duplicate_submission(email, csv_file_path): + """Check if the email has already been submitted.""" + try: + with open(csv_file_path, mode="r") as csvfile: + reader = csv.DictReader(csvfile) + for row in reader: + if row["Email"] == email: + return True + except FileNotFoundError: + # If the CSV file does not exist, treat as no duplicates + pass + return False @app.route("/", methods=["GET", "POST"]) @@ -99,7 +100,8 @@ def index(): form = LeadForm() if form.validate_on_submit(): - if is_duplicate_submission(form.email.data): + # Check if the form submission is a duplicate + if is_duplicate_submission(form.email.data, config.CSV_FILE_PATH): flash( "You have already submitted the form. Duplicate submissions are not allowed.", "warning", @@ -126,9 +128,6 @@ def index(): } append_to_csv(csv_data, config.CSV_FILE_PATH) - # Cache the submitted email to prevent future duplicates - cache_submitted_email(form.email.data) - if config.ODOO_CREATELEAD_ENABLED: # Create lead in Odoo try: diff --git a/poetry.lock b/poetry.lock index a4d8e1d..cf00ce8 100644 --- a/poetry.lock +++ b/poetry.lock @@ -154,17 +154,6 @@ Pillow = ">=8" dev = ["black", "codespell", "flake8", "flake8-bugbear", "importlib-resources", "pep8-naming", "requests"] mypy = ["mypy", "types-Pillow", "types-requests"] -[[package]] -name = "cachelib" -version = "0.9.0" -description = "A collection of cache libraries in the same API interface." -optional = false -python-versions = ">=3.7" -files = [ - {file = "cachelib-0.9.0-py3-none-any.whl", hash = "sha256:811ceeb1209d2fe51cd2b62810bd1eccf70feba5c52641532498be5c675493b3"}, - {file = "cachelib-0.9.0.tar.gz", hash = "sha256:38222cc7c1b79a23606de5c2607f4925779e37cdcea1c2ad21b8bae94b5425a5"}, -] - [[package]] name = "cachetools" version = "5.5.0" @@ -447,21 +436,6 @@ Werkzeug = ">=3.0.0" async = ["asgiref (>=3.2)"] dotenv = ["python-dotenv"] -[[package]] -name = "flask-caching" -version = "2.3.0" -description = "Adds caching support to Flask applications." -optional = false -python-versions = ">=3.8" -files = [ - {file = "Flask_Caching-2.3.0-py3-none-any.whl", hash = "sha256:51771c75682e5abc1483b78b96d9131d7941dc669b073852edfa319dd4e29b6e"}, - {file = "flask_caching-2.3.0.tar.gz", hash = "sha256:d7e4ca64a33b49feb339fcdd17e6ba25f5e01168cf885e53790e885f83a4d2cf"}, -] - -[package.dependencies] -cachelib = ">=0.9.0,<0.10.0" -Flask = "*" - [[package]] name = "flask-sse" version = "1.0.0" @@ -1403,4 +1377,4 @@ testing = ["coverage (>=5.0.3)", "zope.event", "zope.testing"] [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "bac70c74344ab0114f672ec99f7f2ff3a5e2ac5863de402f9cf014bcd73e3e89" +content-hash = "5a0e727c7bc99cabbf2dc55518ec05c6c439bbf74b972bee1a30ee0081fb6426" diff --git a/pyproject.toml b/pyproject.toml index 1c49460..715e3e0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,6 @@ blinkstick = "^1.2.0" gevent = "^24.2.1" html2image = "^2.0.4.3" segno = "^1.6.1" -flask-caching = "^2.3.0" [tool.poetry.group.dev.dependencies]