diff --git a/contactform/app.py b/contactform/app.py index c56fabf..2d3341a 100644 --- a/contactform/app.py +++ b/contactform/app.py @@ -10,6 +10,7 @@ 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 @@ -18,7 +19,6 @@ Configuration, ServerConfiguration, PrinterConfiguration, - Font, LabelConfiguration, WebsiteConfiguration, ) @@ -32,6 +32,9 @@ 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" @@ -81,18 +84,14 @@ class ConfigForm(FlaskForm): submit = SubmitField("Save Changes") -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 +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 @app.route("/", methods=["GET", "POST"]) @@ -100,8 +99,7 @@ def index(): form = LeadForm() if form.validate_on_submit(): - # Check if the form submission is a duplicate - if is_duplicate_submission(form.email.data, config.CSV_FILE_PATH): + if is_duplicate_submission(form.email.data): flash( "You have already submitted the form. Duplicate submissions are not allowed.", "warning", @@ -128,6 +126,9 @@ 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 cf00ce8..a4d8e1d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -154,6 +154,17 @@ 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" @@ -436,6 +447,21 @@ 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" @@ -1377,4 +1403,4 @@ testing = ["coverage (>=5.0.3)", "zope.event", "zope.testing"] [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "5a0e727c7bc99cabbf2dc55518ec05c6c439bbf74b972bee1a30ee0081fb6426" +content-hash = "bac70c74344ab0114f672ec99f7f2ff3a5e2ac5863de402f9cf014bcd73e3e89" diff --git a/pyproject.toml b/pyproject.toml index 715e3e0..1c49460 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,6 +21,7 @@ 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]