From 5f69ef38dd71ba2278ab13438cd68a1e7ada7428 Mon Sep 17 00:00:00 2001 From: Andrew Dinh Date: Tue, 8 Jun 2021 00:00:01 -0700 Subject: [PATCH] Remove reliance on JavaScript to function Add LocalStorage to cache message contents --- README.md | 4 +++- main.py | 6 +++--- templates/index.html | 24 +++++++++++++++++++++++- templates/message.html | 29 +++++++++++++++++++++++++---- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 54875e7..37013ec 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Simple Contact -Extremely simple contact form with a CAPTCHA. Entries are sent to the specified HTTP endpoint. +Extremely simple contact form with a CAPTCHA. Entries are sent to the specified HTTP endpoint. + +JavaScript is not required to fill out the form, but if it is, then form fields are cached in localStorage. ## Building diff --git a/main.py b/main.py index 78fab7a..1de30e3 100644 --- a/main.py +++ b/main.py @@ -29,15 +29,15 @@ def index(): return render_template('message.html', message = "Failed captcha", attempts_left = trials_left) message = request.form.get('message') if ESCAPE_HTML: - message = message.replace("<", "<").replace(">", ">") + message = message.replace("<", "<").replace(">", ">").replace("&", "&") if message != "": requests.post(HTTP_ENDPOINT, data={'subject': 'New Simple Contact message', 'message': message}) - return render_template('message.html', message = "Your message was sent successfully") + return render_template('message.html', message = "Your message was sent successfully", success=True) else: raise TypeError("Invalid method") except Exception as e: print(e) - return render_template('message.html', message="Error occurred") + return render_template('message.html', message="Error occurred"), 500 def captcha_get(max_tries: int = 3, ttl: int = 120, difficulty: str = "medium") -> List[str]: """ Creates a captcha and returns [id, base64 encoded png] """ diff --git a/templates/index.html b/templates/index.html index b8f00a3..c67c2c3 100644 --- a/templates/index.html +++ b/templates/index.html @@ -20,17 +20,39 @@
- +
+

Source code available on GitHub and Gitea

Licensed under AGPL 3.0 | Styling with water.css

+ + diff --git a/templates/message.html b/templates/message.html index 3b07710..faeb523 100644 --- a/templates/message.html +++ b/templates/message.html @@ -9,16 +9,37 @@

{{ message }}

{% if attempts_left %} -

You have {{ attempts_left }} attempt(s) left

- +

You have {{ attempts_left }} attempt(s) left

+ + {% elif attempts_left == 0 %} -

Captcha is now invalid (reload page after you go back)

- +

Captcha is now invalid

+ Go back to homepage {% endif %} + + {% if success %} + + {% endif %} + + \ No newline at end of file