Skip to content

Commit

Permalink
sends email when transfers are rejected, fixes #329
Browse files Browse the repository at this point in the history
  • Loading branch information
helrond committed Jul 16, 2019
1 parent fcbaf8f commit eaa6097
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
6 changes: 6 additions & 0 deletions aurora/bag_transfer/appraise/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.shortcuts import render, redirect

from bag_transfer.lib.files_helper import remove_file_or_dir
from bag_transfer.lib.mailer import Mailer
from bag_transfer.models import Archives, BAGLog
from bag_transfer.mixins.formatmixins import JSONResponseMixin
from bag_transfer.mixins.authmixins import ArchivistMixin
Expand Down Expand Up @@ -56,6 +57,11 @@ def get(self, request, *args, **kwargs):
BAGLog.log_it(('BACPT' if appraisal_decision else 'BREJ'), upload)
if not appraisal_decision:
remove_file_or_dir(upload.machine_file_path)
if upload.user_uploaded:
email = Mailer()
email.to = [upload.user_uploaded.email]
email.setup_message('TRANS_REJECT', upload)
email.send()
upload.save()
rdata['success'] = 1

Expand Down
4 changes: 2 additions & 2 deletions aurora/bag_transfer/lib/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def do(self):
new_arc.setup_save(upload_list)
new_arc.process_status = Archives.INVALID
BAGLog.log_it(upload_list['auto_fail_code'], new_arc)
email.setup_message('TRANS_FAIL_VAL',new_arc)
email.setup_message('TRANS_FAIL_VAL', new_arc)
email.send()
FH.remove_file_or_dir(new_arc.machine_file_path)

Expand All @@ -77,7 +77,7 @@ def do(self):
print "Transfer {} is valid".format(new_arc.machine_file_identifier)
new_arc.process_status = Archives.VALIDATED
new_arc.bag_it_valid = True
BAGLog.log_it('APASS',new_arc)
BAGLog.log_it('APASS', new_arc)
email.setup_message('TRANS_PASS_ALL',new_arc)
email.send()
FH.move_file_or_dir('/data/tmp/{}'.format(new_arc.bag_it_name), '{}{}'.format(settings.STORAGE_ROOT_DIR, new_arc.machine_file_identifier))
Expand Down
51 changes: 30 additions & 21 deletions aurora/bag_transfer/lib/mailer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from django.core.mail import EmailMessage
from django.conf import settings as CF
from django.urls import reverse


class Mailer():
def __init__(self,subject ='',to=[],text_content=''):
def __init__(self, subject='', to=[], text_content=''):
self.subject = subject
self.from_email= CF.EMAIL_HOST_USER
self.to= to
self.text_content = text_content
self.from_email = CF.EMAIL_HOST_USER
self.to = to
footer = "\r\n".join(["Rockefeller Archive Center",
"15 Dayton Avenue, Sleepy Hollow, NY 10591",
"(914) 366-6300", "archive@rockarch.org",
"https://rockarch.org"])
self.text_content = '{}\r\n\r\n{}'.format(text_content, footer)

self.email = {}

Expand All @@ -27,7 +32,7 @@ def send(self):
self.text_content,
self.from_email,
send_to,
reply_to = [self.from_email]
reply_to=[self.from_email]
)

try:
Expand All @@ -39,30 +44,22 @@ def send(self):
return True

def setup_message(self, mess_code, archive_obj={}):
if mess_code == 'TRANS_RECEIPT':
self.subject = 'Your transfer reciept'
self.text_content = """Your transfer has been recieved, and is being processed.
"""
elif mess_code == 'TRANS_PASS_ALL':
self.subject = 'Your transfer passed all validation'
if mess_code == 'TRANS_PASS_ALL':
self.subject = 'Transfer {} passed all validation'.format(archive_obj.bag_or_failed_name())

lcodes = archive_obj.get_bag_validations()

eparts = [
'The transfer {} was received at {} and has passed all automated checks:',
'bag validation\t\t\t{}',
'bag profile validation\t\t{}',
'You can review the status of this transfer at any time by logging into {}'
'The transfer {} was received at {} and has passed all automated validation checks:',
'You can view the current status of this transfer at {}'
]
self.text_content = "\r\n".join(eparts).format(
self.text_content = "\r\n\r\n".join(eparts).format(
archive_obj.bag_or_failed_name(),
archive_obj.machine_file_upload_time,
(lcodes['PBAG'] if lcodes else '--'),
(lcodes['PBAGP'] if lcodes else '--'),
CF.BASE_URL + 'app'
CF.BASE_URL + reverse('transfers:detail', kwargs={'pk': archive_obj.pk})
)
elif mess_code == 'TRANS_FAIL_VAL':
self.subject = 'Your Transfer Failed Validation'
self.subject = 'Transfer {} failed validation'.format(archive_obj.bag_or_failed_name())

eparts = [
'An error occurred for the transfer {} during {} at {}. The transfer has been deleted from our systems.',
Expand All @@ -75,7 +72,7 @@ def setup_message(self, mess_code, archive_obj={}):
archive_obj.bag_or_failed_name(),
(error_obj.code.code_desc if error_obj else '--'),
(error_obj.created_time if error_obj else '--'),
CF.BASE_URL + 'app/transfers/'
CF.BASE_URL + reverse('transfers:detail', kwargs={'pk': archive_obj.pk})
)

# additional errs
Expand All @@ -84,3 +81,15 @@ def setup_message(self, mess_code, archive_obj={}):
self.text_content += '\r\n\r\nAdditional Error Information:\r\n\r\n'
for err in additional_errors:
self.text_content += '{}\r\n\r\n'.format(err)

elif mess_code == 'TRANS_REJECT':
self.subject = 'Transfer {} was rejected'.format(archive_obj.bag_or_failed_name())

eparts = [
'An appraisal archivist rejected transfer {}. The transfer has been deleted from our systems.'.format(archive_obj.bag_or_failed_name())
]

if archive_obj.appraisal_note:
eparts.append('The reason for this action was: {}'.format(archive_obj.appraisal_note))

self.text_content = "\r\n\r\n".join(eparts)

0 comments on commit eaa6097

Please sign in to comment.