Skip to content

Commit

Permalink
use flask caching instead of reading csv again
Browse files Browse the repository at this point in the history
  • Loading branch information
tobru committed Sep 19, 2024
1 parent dbe0374 commit ba7a957
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
31 changes: 16 additions & 15 deletions contactform/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -18,7 +19,6 @@
Configuration,
ServerConfiguration,
PrinterConfiguration,
Font,
LabelConfiguration,
WebsiteConfiguration,
)
Expand All @@ -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"
Expand Down Expand Up @@ -81,27 +84,22 @@ 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"])
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",
Expand All @@ -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:
Expand Down
28 changes: 27 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit ba7a957

Please sign in to comment.