Merge pull request #880 from alphagov/pass-contact-block

Pass through contact block from service to outputted letter
This commit is contained in:
Chris Hill-Scott
2017-04-05 09:45:28 +01:00
committed by GitHub
3 changed files with 40 additions and 21 deletions

View File

@@ -267,18 +267,23 @@ def persist_letter(
def build_dvla_file(self, job_id): def build_dvla_file(self, job_id):
try: try:
if all_notifications_are_created_for_job(job_id): if all_notifications_are_created_for_job(job_id):
notifications = dao_get_all_notifications_for_job(job_id) file_contents = '\n'.join(
file = "" str(LetterDVLATemplate(
for n in notifications: notification.template.__dict__,
t = {"content": n.template.content, "subject": n.template.subject} notification.personalisation,
# This unique id is a 7 digits requested by DVLA, not known if this number needs to be sequential. # This unique id is a 7 digits requested by DVLA, not known
unique_id = int(''.join(map(str, random.sample(range(9), 7)))) # if this number needs to be sequential.
template = LetterDVLATemplate(t, n.personalisation, unique_id) numeric_id=random.randint(1, int('9' * 7)),
file = file + str(template) + "\n" contact_block=notification.service.letter_contact_block,
s3upload(filedata=file, ))
region=current_app.config['AWS_REGION'], for notification in dao_get_all_notifications_for_job(job_id)
bucket_name=current_app.config['DVLA_UPLOAD_BUCKET_NAME'], )
file_location="{}-dvla-job.text".format(job_id)) s3upload(
filedata=file_contents + '\n',
region=current_app.config['AWS_REGION'],
bucket_name=current_app.config['DVLA_UPLOAD_BUCKET_NAME'],
file_location="{}-dvla-job.text".format(job_id)
)
else: else:
current_app.logger.info("All notifications for job {} are not persisted".format(job_id)) current_app.logger.info("All notifications for job {} are not persisted".format(job_id))
self.retry(queue="retry", exc="All notifications for job {} are not persisted".format(job_id)) self.retry(queue="retry", exc="All notifications for job {} are not persisted".format(job_id))

View File

@@ -971,17 +971,30 @@ def test_build_dvla_file(sample_letter_template, mocker):
create_notification(template=job.template, job=job) create_notification(template=job.template, job=job)
create_notification(template=job.template, job=job) create_notification(template=job.template, job=job)
mocked = mocker.patch("app.celery.tasks.s3upload") mocker.patch("app.celery.tasks.random.randint", return_value=999)
mocker.patch("app.celery.tasks.LetterDVLATemplate.__str__", return_value="dvla|string") mocked_upload = mocker.patch("app.celery.tasks.s3upload")
mocked_letter_template = mocker.patch("app.celery.tasks.LetterDVLATemplate")
mocked_letter_template_instance = mocked_letter_template.return_value
mocked_letter_template_instance.__str__.return_value = "dvla|string"
build_dvla_file(job.id) build_dvla_file(job.id)
file = "dvla|string\ndvla|string\n" mocked_upload.assert_called_once_with(
filedata="dvla|string\ndvla|string\n",
region=current_app.config['AWS_REGION'],
bucket_name=current_app.config['DVLA_UPLOAD_BUCKET_NAME'],
file_location="{}-dvla-job.text".format(job.id)
)
assert mocked.called # Template
mocked.assert_called_once_with(filedata=file, assert mocked_letter_template.call_args[0][0]['subject'] == 'Template subject'
region=current_app.config['AWS_REGION'], assert mocked_letter_template.call_args[0][0]['content'] == 'Dear Sir/Madam, Hello. Yours Truly, The Government.'
bucket_name=current_app.config['DVLA_UPLOAD_BUCKET_NAME'],
file_location="{}-dvla-job.text".format(job.id)) # Personalisation
assert mocked_letter_template.call_args[0][1] is None
# Named arguments
assert mocked_letter_template.call_args[1]['numeric_id'] == 999
assert mocked_letter_template.call_args[1]['contact_block'] == 'London,\nSW1A 1AA'
def test_build_dvla_file_retries_if_all_notifications_are_not_created(sample_letter_template, mocker): def test_build_dvla_file_retries_if_all_notifications_are_not_created(sample_letter_template, mocker):

View File

@@ -134,7 +134,8 @@ def sample_service(
'message_limit': limit, 'message_limit': limit,
'restricted': restricted, 'restricted': restricted,
'email_from': email_from, 'email_from': email_from,
'created_by': user 'created_by': user,
'letter_contact_block': 'London,\nSW1A 1AA',
} }
service = Service.query.filter_by(name=service_name).first() service = Service.query.filter_by(name=service_name).first()
if not service: if not service: