SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 989c65ac authored by George Katsikas's avatar George Katsikas :goat:
Browse files

rework email sender to allow for mails -if they exist-

parent 7b71d2b9
No related branches found
No related tags found
No related merge requests found
...@@ -42,7 +42,7 @@ class MailEngine: ...@@ -42,7 +42,7 @@ class MailEngine:
bcc=[], bcc=[],
from_email="", from_email="",
from_name="", from_name="",
**kwargs **kwargs,
): ):
""" """
Start engine with specific mail_code. Any other keyword argument that is passed will Start engine with specific mail_code. Any other keyword argument that is passed will
...@@ -257,17 +257,21 @@ class MailEngine: ...@@ -257,17 +257,21 @@ class MailEngine:
def _validate_email_fields(self): def _validate_email_fields(self):
"""Validate all email addresses in the mail config.""" """Validate all email addresses in the mail config."""
for email_key in self._email_fields: for email_key in self._email_fields:
if email_key in self.mail_data: if emails := self.mail_data.get(email_key, None):
if isinstance(self.mail_data[email_key], list): emails = emails if isinstance(emails, list) else [emails]
for i, email in enumerate(self.mail_data[email_key]): valid_emails = [
self.mail_data[email_key][i] = self._validate_email_addresses( valid_entry
email for entry in emails
) if (valid_entry := self._validate_email_addresses(entry))
else: ]
self.mail_data[email_key] = self._validate_email_addresses(
self.mail_data[email_key] if len(valid_emails) == 0:
raise ConfigurationError(
"No valid email addresses found for %s." % email_key
) )
self.mail_data[email_key] = valid_emails
def _validate_email_addresses(self, entry): def _validate_email_addresses(self, entry):
""" """
Return email address given raw email, email prefix or database relation given in `entry`. Return email address given raw email, email prefix or database relation given in `entry`.
...@@ -280,13 +284,16 @@ class MailEngine: ...@@ -280,13 +284,16 @@ class MailEngine:
return "%s%s" % (entry, get_current_domain()) return "%s%s" % (entry, get_current_domain())
elif self.template_variables["object"]: elif self.template_variables["object"]:
mail_to = self.template_variables["object"] mail_to = self.template_variables["object"]
for attr in entry.split("."): for attr in entry.split("|")[0].split("."):
try: try:
mail_to = getattr(mail_to, attr) mail_to = getattr(mail_to, attr)
if inspect.ismethod(mail_to): if inspect.ismethod(mail_to):
mail_to = mail_to() mail_to = mail_to()
except AttributeError: except AttributeError:
# Invalid property/mail # Invalid property/mail
if entry.endswith("|None"):
# Allow None values
return None
raise KeyError("The property (%s) does not exist." % entry) raise KeyError("The property (%s) does not exist." % entry)
return mail_to return mail_to
raise KeyError("Neither an email adress nor db instance is given.") raise KeyError("Neither an email adress nor db instance is given.")
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"subject": "Invitation to become a Fellow at SciPost", "subject": "Invitation to become a Fellow at SciPost",
"recipient_list": [ "recipient_list": [
"nomination.profile.email", "nomination.profile.email",
"nomination.profile.contributor.user.email" "nomination.profile.contributor.user.email|None"
], ],
"bcc": [ "bcc": [
"edadmin@" "edadmin@"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment