Skip to content

Commit

Permalink
allow user to input email address
Browse files Browse the repository at this point in the history
  • Loading branch information
dale-wahl committed Sep 7, 2023
1 parent 2ec570a commit 98d50a1
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 39 deletions.
3 changes: 1 addition & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ PUBLIC_PORT=80

# Backend API
# API_HOST is used by the frontend; in Docker it should be the backend container name
# (or "localhost" if front and backend are running together in one container)
API_HOST=backend
# (or "localhost" if front and backend are running together in one container
PUBLIC_API_PORT=4444

# Telegram apparently needs its own port
Expand Down
62 changes: 31 additions & 31 deletions backend/lib/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,37 +302,37 @@ def after_process(self):
self.job.finish()

if config.get('mail.server') and self.dataset.get_parameters().get("email-complete", False):
for owner in self.dataset.get_owners_users():
# Check that username is email address
if re.match(r"[^@]+\@.*?\.[a-zA-Z]+", owner):
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from smtplib import SMTPException
import socket
import html2text

self.log.info("Sending email to %s" % owner)
dataset_url = ('https://' if config.get('flask.https') else 'http://') + config.get('flask.server_name') + '/results/' + self.dataset.key
sender = config.get('mail.noreply')
message = MIMEMultipart("alternative")
message["From"] = sender
message["To"] = owner
message["Subject"] = "4CAT dataset completed: %s" % self.dataset.get_label()
mail = """
<p>Hello %s,</p>
<p>4CAT has finished collecting your dataset labeled: %s</p>
<p>You can view your dataset via the following link:</p>
<p><a href="%s">%s</a></p>
<p>Sincerely,</p>
<p>Your 4CAT admins</p>
""" % (owner, self.dataset.get_label(), dataset_url, dataset_url)
html_parser = html2text.HTML2Text()
message.attach(MIMEText(html_parser.handle(mail), "plain"))
message.attach(MIMEText(mail, "html"))
try:
send_email([owner], message)
except (SMTPException, ConnectionRefusedError, socket.timeout) as e:
self.log.error("Sending email to %s" % owner)
owner = self.dataset.get_parameters().get("email-complete", False)
# Check that username is email address
if re.match(r"[^@]+\@.*?\.[a-zA-Z]+", owner):
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from smtplib import SMTPException
import socket
import html2text

self.log.info("Sending email to %s" % owner)
dataset_url = ('https://' if config.get('flask.https') else 'http://') + config.get('flask.server_name') + '/results/' + self.dataset.key
sender = config.get('mail.noreply')
message = MIMEMultipart("alternative")
message["From"] = sender
message["To"] = owner
message["Subject"] = "4CAT dataset completed: %s" % self.dataset.get_label()
mail = """
<p>Hello %s,</p>
<p>4CAT has finished collecting your dataset labeled: %s</p>
<p>You can view your dataset via the following link:</p>
<p><a href="%s">%s</a></p>
<p>Sincerely,</p>
<p>Your friendly neighborhood 4CAT admin</p>
""" % (owner, self.dataset.get_label(), dataset_url, dataset_url)
html_parser = html2text.HTML2Text()
message.attach(MIMEText(html_parser.handle(mail), "plain"))
message.attach(MIMEText(mail, "html"))
try:
send_email([owner], message)
except (SMTPException, ConnectionRefusedError, socket.timeout) as e:
self.log.error("Sending email to %s" % owner)


def remove_files(self):
Expand Down
6 changes: 6 additions & 0 deletions webtool/templates/components/processor-details.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ <h4>{{ processor.title }}</h4>
{% for option in processor_options %}
{% include "components/processor-option.html" %}
{% endfor %}
{% if __user_config("mail.server") and '@' in current_user.get_name() %}
<div class="processor-option">
<label for="email">Email address</label>
<input type="email" name="email" id="email" value="{{ current_user.get_name() }}" placeholder="Email address">
</div>
{% endif %}
</fieldset>
</div>
{% endif %}
Expand Down
9 changes: 4 additions & 5 deletions webtool/templates/create-dataset.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,14 @@ <h2><span>Create new dataset</span></h2>

<p role="tooltip" id="tooltip-dataset-private">This will only hide your dataset from other users. It will NOT encrypt your data and server administrators will still be able to view it. If you are working with sensitive data, you should consider running your own 4CAT instance.</p>
</div>
{% if __user_config("mail.server") and '@' in current_user.get_name() %}
{% if __user_config("mail.server") %}
<div class="form-element">
<label for="data-email-complete">Make private:</label>
<label for="data-email-complete">Receive email on completion:</label>
<div class="filter-parameters">
<label><input type="checkbox" name="email-complete" id="data-email-complete" checked="checked"> Receive email when dataset is complete</label>
<label><input type="checkbox" name="email-complete" id="data-email-complete" checked=""></label>
<label><input id="data-email-user" type="email" name="email-user" value={{ current_user.get_name() }}></label>
<button class="tooltip-trigger" aria-controls="tooltip-dataset-email" aria-label="Extended help for option">?</button>
</div>


<p role="tooltip" id="tooltip-dataset-email">This will only function if your username is your email address.</p>
</div>
{% endif %}
Expand Down
5 changes: 4 additions & 1 deletion webtool/views/api_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ def queue_dataset():
if request.form.to_dict().get("pseudonymise") in ("pseudonymise", "anonymise"):
sanitised_query["pseudonymise"] = request.form.to_dict().get("pseudonymise")

sanitised_query["email-complete"] = request.form.to_dict().get("email-complete", False)
if request.form.to_dict().get("email-complete", False):
sanitised_query["email-complete"] = request.form.to_dict().get("email-user", False)

# unchecked checkboxes do not send data in html forms, so key will not exist if box is left unchecked
is_private = bool(request.form.get("make-private", False))
Expand Down Expand Up @@ -1034,6 +1035,8 @@ def queue_processor(key=None, processor=None):
except QueryParametersException as e:
return error(400, error=str(e))

#TODO: add options['email-complete'] to options... based on something from request.form?

# private or not is inherited from parent dataset
analysis = DataSet(parent=dataset.key,
parameters=options,
Expand Down

0 comments on commit 98d50a1

Please sign in to comment.