mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-17 10:42:25 -05:00
This commit changes the code in post notification endpoint to handle a serialised template (ie a `dict`) rather than a database object. This is the first step towards being able to cache the template and not hit the database on every request. There should be no functional changes here, it’s just refactoring. There are some changes to the tests where the signature of functions has changed. Importing of the template schema has to be done at a function level, otherwise Marshmallow gets weird. This commit also copies the `JSONModel` class from the admin app, which turns serialised data (a dict made from JSON) into an object on which certain predefined properties are allowed. This means we can still do the caching of serialised data, without having to change too much of the code in the app, or make it ugly by sprinkling dict lookups everywhere. We’re not copying all of JSONModel from the admin app, just the bits we need. We don’t need to compare or hash these objects, they’re just used for lookups. And redefining `__getattribute__` scares Leo.
40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
from notifications_utils.postal_address import PostalAddress
|
|
|
|
from app import create_random_identifier
|
|
from app.models import LETTER_TYPE
|
|
from app.notifications.process_notifications import persist_notification
|
|
|
|
|
|
def create_letter_notification(
|
|
letter_data,
|
|
template,
|
|
service,
|
|
api_key,
|
|
status,
|
|
reply_to_text=None,
|
|
billable_units=None,
|
|
updated_at=None,
|
|
):
|
|
notification = persist_notification(
|
|
template_id=template.id,
|
|
template_version=template.version,
|
|
template_postage=template.postage,
|
|
# we only accept addresses_with_underscores from the API (from CSV we also accept dashes, spaces etc)
|
|
recipient=PostalAddress.from_personalisation(letter_data['personalisation']).normalised,
|
|
service=service,
|
|
personalisation=letter_data['personalisation'],
|
|
notification_type=LETTER_TYPE,
|
|
api_key_id=api_key.id,
|
|
key_type=api_key.key_type,
|
|
job_id=None,
|
|
job_row_number=None,
|
|
reference=create_random_identifier(),
|
|
client_reference=letter_data.get('reference'),
|
|
status=status,
|
|
reply_to_text=reply_to_text,
|
|
billable_units=billable_units,
|
|
postage=letter_data.get('postage'),
|
|
updated_at=updated_at
|
|
)
|
|
return notification
|