Skip to content

Commit

Permalink
Merge pull request #8 from ethereum/reference
Browse files Browse the repository at this point in the history
Adding optional reference field
  • Loading branch information
fredriksvantes authored Oct 16, 2024
2 parents 8177739 + c8a854a commit 2e7437a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
18 changes: 13 additions & 5 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
"""
Expand All @@ -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('<br />', '\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:
Expand Down Expand Up @@ -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:
Expand All @@ -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}")
Expand Down
4 changes: 3 additions & 1 deletion static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -216,4 +218,4 @@ function displayResult(status, message) {
const formElement = document.getElementById("submission-form");
const statusText = (status == "success") ? "Success!" : "Error";
formElement.innerHTML = `<fieldset><legend>${statusText}</legend><span class='pure-form-message'>${message}</span><br><br><span class='pure-form-message'><a href="#" onclick="location.reload()">Send one more submission</a></span></fieldset>`
}
}
3 changes: 3 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
{% if 0 %}<option value="oleh">Oleh</option>{% endif %}
</select>
<br>
<label id="referenceLabel" for="reference">Reference (optional):</label>
<input id="reference" name="reference" type="text" placeholder="Example: FY24-XXXX">
<br>
<label id="messageLabel" for="text">Message:</label>
<textarea id="text" name="text" type="text" placeholder="Write a message. Please mention your name or email and short description of the file contents."></textarea>
<br>
Expand Down

0 comments on commit 2e7437a

Please sign in to comment.