Skip to content

Commit

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

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

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

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

0 comments on commit 234ad6f

Please sign in to comment.