Skip to content

Commit

Permalink
DD-1546 Create Cron jobs to create deposit reports for SWORD customer…
Browse files Browse the repository at this point in the history
…s and application managers (#60)

* DD-1546-Create-Cron-tab-jobs-to-make-deposits-reports

* Update src/datastation/common/send_mail.py

* Update src/datastation/common/send_mail.py

---------

Co-authored-by: Ali Sheikhi <[email protected]>
Co-authored-by: Jan van Mansum <[email protected]>
  • Loading branch information
3 people authored Jun 13, 2024
1 parent b2b1aec commit e4438fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
16 changes: 12 additions & 4 deletions src/datastation/common/send_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
# https://docs.python.org/3/library/email.html#module-email
# https://docs.python.org/3/library/email.examples.html


class SendMail:
# def __init__(self):
# pass

@staticmethod
def send(to_email, subject, message_body, attachment):
email_template = " -s '{0}' {1}<<EOF {2} \nEOF"
def send(to_email, subject, message_body, attachment, cc_email, bcc_email):
recipients = to_email

if cc_email is not None:
recipients = ' -c {0} {1} '.format(cc_email, recipients)

if bcc_email is not None:
recipients = ' -b {0} {1} '.format(bcc_email, recipients)

if attachment is not None:
os.system("mail" + " -a " + attachment + email_template.format(subject, to_email, message_body))
os.system('echo "{0}" | mail -s "{1}" -a "{2}" {3}'.format(message_body, subject, attachment, recipients))
else:
os.system("mail" + email_template.format(subject, to_email, message_body))
os.system('echo "{0}" | mail -s "{1}" {2}'.format(message_body, subject, recipients))
19 changes: 11 additions & 8 deletions src/datastation/deposit_create_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,36 @@ def handle_request(self):
else:
self.save_report_to_file(report, output_file)

email_to = self.__command_line_args.email_to
if email_to is not None:
self.send_report_mail(email_to, output_file)
if self.__command_line_args.email_to is not None:
self.send_report_mail(output_file)

def save_report_to_file(self, report, output_file):
with open(output_file, 'w') as output:
output.write(report)

def send_report_mail(self, mail_to, attachment):
SendMail.send(mail_to,
def send_report_mail(self, attachment):
SendMail.send(self.__command_line_args.email_to,
"Deposits report",
"Please, find attached the detailed report of deposits.",
attachment)
attachment,
self.__command_line_args.cc_email_to,
self.__command_line_args.bcc_email_to)


def main():
config = init()
parser = argparse.ArgumentParser(prog='deposit_create_report',
description='Create and send reports based on dd-manage-deposit database')
parser.add_argument('-o', '--output-file', dest='output_file', default='-',
help='the file to write the output to or - for stdout')
help='the file to write the output and send recipient to a to or - for stdout')
parser.add_argument('-e', '--enddate', dest='enddate', help='Filter until the record creation of this date')
parser.add_argument('-s', '--startdate', dest='startdate', help='Filter from the record creation of this date')
parser.add_argument('-t', '--state', help='The state of the deposit')
parser.add_argument('-u', '--user', dest='user', help='The depositor name')
parser.add_argument('-f', '--format', dest='file_format', default='text/csv', help='Output data format')
parser.add_argument('--email-to', dest='email_to', help='')
parser.add_argument('--email-to', dest='email_to', help='when more than one recipient: comma separated emails')
parser.add_argument('--cc-email-to', dest='cc_email_to', help='will be sent only if email-to is defined')
parser.add_argument('--bcc-email-to', dest='bcc_email_to', help='will be sent only if email-to is defined')
args = parser.parse_args()

server_url = config['manage_deposit']['service_baseurl'] + '/report'
Expand Down

0 comments on commit e4438fc

Please sign in to comment.