Added the random string reference to the letter

- uses the reference field on the notifications table to store a 16char random string used to cross reference DVLA letters back to the notification
- used as letter barcode does not have space for a UUID notification id

Depends on https://github.com/alphagov/notifications-utils/pull/149

Renamed the numeric_id to notification_reference in utils and changed validation rules to match this

Note also the persist_notification method set "reference" to be "client_reference" which is confusing and they are different things, so fixed this too.
This commit is contained in:
Martyn Inglis
2017-04-12 17:56:55 +01:00
parent b55b1007d0
commit b0e5062df2
6 changed files with 30 additions and 16 deletions

View File

@@ -1,4 +1,6 @@
import os
import random
import string
import uuid
from flask import Flask, _request_ctx_stack
@@ -204,6 +206,10 @@ def create_uuid():
return str(uuid.uuid4())
def create_random_identifier():
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(16))
def process_user_agent(user_agent_string):
if user_agent_string and user_agent_string.lower().startswith("notify"):
components = user_agent_string.split("/")

View File

@@ -10,6 +10,7 @@ from notifications_utils.template import SMSMessageTemplate, WithSubjectTemplate
from sqlalchemy.exc import SQLAlchemyError
from app import (
create_uuid,
create_random_identifier,
DATETIME_FORMAT,
notify_celery,
encryption
@@ -262,7 +263,8 @@ def persist_letter(
created_at=created_at,
job_id=notification['job'],
job_row_number=notification['row_number'],
notification_id=notification_id
notification_id=notification_id,
reference=create_random_identifier()
)
current_app.logger.info("Letter {} created at {}".format(saved_notification.id, created_at))
@@ -311,10 +313,8 @@ def create_dvla_file_contents(job_id):
str(LetterDVLATemplate(
notification.template.__dict__,
notification.personalisation,
# This unique id is a 7 digits requested by DVLA, not known
# if this number needs to be sequential.
numeric_id=random.randint(1, int('9' * 7)),
contact_block=notification.service.letter_contact_block,
notification_reference=notification.reference,
contact_block=notification.service.letter_contact_block
))
for notification in dao_get_all_notifications_for_job(job_id)
)

View File

@@ -36,6 +36,7 @@ def persist_notification(template_id,
job_id=None,
job_row_number=None,
reference=None,
client_reference=None,
notification_id=None,
simulated=False):
# if simulated create a Notification model to return but do not persist the Notification to the dB
@@ -53,7 +54,8 @@ def persist_notification(template_id,
created_at=created_at or datetime.utcnow(),
job_id=job_id,
job_row_number=job_row_number,
client_reference=reference
client_reference=client_reference,
reference=reference
)
if not simulated:
dao_create_notification(notification)

View File

@@ -47,7 +47,7 @@ def post_notification(notification_type):
notification_type=notification_type,
api_key_id=api_user.id,
key_type=api_user.key_type,
reference=form.get('reference', None),
client_reference=form.get('reference', None),
simulated=simulated)
if not simulated:
queue_name = 'priority' if template.process_type == PRIORITY else None