From c8a854a003a326ed36d6a59b92dd70e1f7c251a1 Mon Sep 17 00:00:00 2001 From: Fredrik Svantes Date: Wed, 16 Oct 2024 15:46:17 +0200 Subject: [PATCH] Adding optional reference field --- server.py | 18 +++++++++++++----- static/js/app.js | 4 +++- templates/index.html | 3 +++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/server.py b/server.py index 3c41068..6d3f732 100644 --- a/server.py +++ b/server.py @@ -40,10 +40,11 @@ def sanitize_filename(filename): def parse_form(form): """ - Parses the form data to extract the message, recipient, and attachments. + Parses the form data to extract the message, recipient, reference, and attachments. """ text = form['message'] recipient = form['recipient'] + reference = form.get('reference', '') all_attachments = [] for i in range(Config.NUMBER_OF_ATTACHMENTS): @@ -53,7 +54,7 @@ def parse_form(form): continue sanitized_filename = sanitize_filename(filename) all_attachments.append((sanitized_filename, attachment)) - return text, recipient, all_attachments + return text, recipient, reference, all_attachments def valid_recipient(recipient): """ @@ -72,15 +73,19 @@ def get_identifier(recipient, now=None, randint=None): randint = Random().randint(1000, 9999) return f'{recipient}:{now.strftime("%Y:%m:%d:%H:%M:%S")}:{randint}' -def create_email(to_email, identifier, text, all_attachments): +def create_email(to_email, identifier, text, all_attachments, reference=''): """ Creates an email message with attachments. """ plain_text = text.replace('
', '\n') + subject = f'Secure Form Submission {identifier}' + if reference: + subject = f'{reference} {subject}' + message = Mail( from_email=FROMEMAIL, to_emails=to_email, - subject=f'Secure Form Submission {identifier}', + subject=subject, plain_text_content=plain_text) for item in all_attachments: @@ -156,6 +161,7 @@ def submit(): # Extract fields from JSON data message = data['message'] recipient = data['recipient'] + reference = data.get('reference', '') files = data['files'] if not message: @@ -173,9 +179,11 @@ def submit(): identifier = get_identifier(recipient) log_data = f"{date} - message to: {recipient}, identifier: {identifier}, length: {message_length}, file count: {file_count}" + if reference: + log_data += f", reference: {reference}" logging.info(log_data) - message = create_email(to_email, identifier, message, files) + message = create_email(to_email, identifier, message, files, reference) if Config.DEBUG_MODE: print(f"Attempt to send email to {to_email}") diff --git a/static/js/app.js b/static/js/app.js index f58bdd8..36e0706 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -87,9 +87,11 @@ function acceptEncryptedData(data) { console.log('all chunks received, submitting form'); const gRecaptchaBlock = document.getElementById('gRecaptcha'); const recipient = document.getElementById("recipientSelect"); + const reference = document.getElementById("reference"); dataArray['g-recaptcha-response'] = gRecaptchaBlock ? grecaptcha.getResponse() : null; dataArray['recipient'] = recipient.value; + dataArray['reference'] = reference.value; postData('/submit-encrypted-data', dataArray) .then(response => { @@ -216,4 +218,4 @@ function displayResult(status, message) { const formElement = document.getElementById("submission-form"); const statusText = (status == "success") ? "Success!" : "Error"; formElement.innerHTML = `
${statusText}${message}

Send one more submission
` -} \ No newline at end of file +} diff --git a/templates/index.html b/templates/index.html index 2a6b572..d04b8e7 100644 --- a/templates/index.html +++ b/templates/index.html @@ -26,6 +26,9 @@ {% if 0 %}{% endif %}
+ + +