Commit e94bf7d3 authored by Frédéric Escudié's avatar Frédéric Escudié
Browse files

Add placeholders in mail.

parent 53d4e4b9
......@@ -622,11 +622,43 @@ class Workflow(threading.Thread):
def set_message(self, message):
self.__message = message
def _get_cleaned_email_placeholders(self, text):
"""
@summary: Returns the text after replacement of placeholders by the corresponding workflow values (method or attribute).
Placeholders must be an attribute or a method of the workflow between three dollars: ###attribute### or ###method()###.
You can add "|date" after the attribute or the method to convert a timestamp in human readable date.
Examples: ###id### is replaced by wf.id ; ###get_status()### is replaced by wf.get_status() ; ###start_time|date### is rplaced by wf.start_time in date format.
@param text: [str] The text containing placeholders.
@return: [str] The text with placeholders replaced by her real value.
"""
new_text = text
placeholders = re.findall("\#\#\#([^\#]+)\#\#\#", text)
for placeholder in placeholders:
try:
placeholder_value = ""
placeholder_key = placeholder
is_date = False
if placeholder.endswith("|date"):
placeholder_key = placeholder[:-5]
is_date = True
# Get value
if placeholder_key.endswith("()"):
placeholder_value = str(getattr(self, placeholder_key[:-2])())
else:
placeholder_value = str(getattr(self, placeholder_key))
# Apply date format
if is_date:
jflow_date_format = self.jflow_config_reader.get_date_format()
placeholder_value = time.strftime(jflow_date_format + " %H:%M:%S", time.gmtime(float(placeholder_value)))
new_text = new_text.replace("###" + placeholder + "###", placeholder_value)
except:
pass
return new_text
def _send_email(self):
import smtplib
from email.mime.text import MIMEText
from email.mime.text import MIMEText
smtps, smtpp, froma, fromp, toa, subject, message = self.jflow_config_reader.get_email_options()
if self.__to_address: toa = self.__to_address
......@@ -642,10 +674,13 @@ class Workflow(threading.Thread):
# Create a text/plain message
if not message:
message = Workflow.get_status_under_text_format(self, True, True, True)
message = self._get_cleaned_email_placeholders( message )
msg = MIMEText(message, 'html')
me = froma
you = toa
if not subject: subject = "JFlow - Workflow #" + str(self.id) + " is " + self._status
if not subject:
subject = "JFlow - Workflow #" + str(self.id) + " is " + self.get_status()
subject = self._get_cleaned_email_placeholders( subject )
msg['Subject'] = subject
msg['From'] = me
msg['To'] = you
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment