Commit Graph

98 Commits

Author SHA1 Message Date
Ben Thorner
a91fde2fda Run auto-correct on app/ and tests/ 2021-03-12 11:45:45 +00:00
David McDonald
7b5f4ad156 Fix bug with missing template version param
We were asking for the latest version of a letter template rather than
the version that the notification was sent with. This mean that if you
previewed a letter and had made edits to the template since it was sent
you would be shown an incorrect preview.
2020-08-17 15:30:16 +01:00
Chris Hill-Scott
3ffdb3093b Revert "Revert "Merge pull request #2887 from alphagov/cache-the-serialised-things""
This reverts commit 7e85e37e1d.
2020-06-26 14:10:12 +01:00
Chris Hill-Scott
7e85e37e1d Revert "Merge pull request #2887 from alphagov/cache-the-serialised-things"
This reverts commit b8c2c6b291, reversing
changes made to 351aca2c5a.
2020-06-26 13:42:44 +01:00
Chris Hill-Scott
320bca70f7 Serialise service, API keys and permissions
By serialising these straight away we can:
- not go back to the database later, potentially closing the connection
  sooner
- potentially cache the serialised data, meaning we don’t touch the
  database at all
2020-06-23 16:00:41 +01:00
Chris Hill-Scott
718f8ccad0 Remove content and subject from get all templates
Content and subject are user-submitted so are effectively unbounded in
size. And we’re serialising them for every template when sending the
list of templates to the admin app.

For the service with the most templates this results in a 1.3Mb blob of
JSON going over the wire, and being cached in Redis.

And then the admin app completely ignores these fields, because it does
show template content until you’ve clicked into a single template.

This commit adds a new query parameter, `detailed`, that the admin app
can set to `False`. When it does only the fields needed to render the
`/templates` page are returned.

This is done with a new parameter so as not to break the V1 API.
Although I looked in Kibana and it doesn’t seem like anyone external is
using this endpoint we’ve come this far without breaking the API so…
2020-06-22 13:08:41 +01:00
Katie Smith
72be10c681 Add JSON schema for updating template
We did not have a JSON schema for updating a template. Since we will
remove the postage constraint from the templates table, this adds a JSON
schema for updating a template so that we can use it to check that the
postage is one of the allowed values.
2020-06-19 08:59:27 +01:00
Pea Tyczynska
7a89b1b835 Fix bug where preview for templated letters would not show 2020-04-20 15:35:37 +01:00
Chris Hill-Scott
5ddb5a75da Use new properties of utils Templates
We’ve added some new properties to the templates in utils that we can
use instead of doing weird things like
`WithSubjectTemplate.__str__(another_instance)`
2020-04-15 16:40:42 +01:00
Chris Hill-Scott
025ac3ea89 Use same template to validate and send notification
To be absolutely sure that we can send a message we should also validate
it using the same template class that we use to render it.
2020-04-07 10:41:16 +01:00
David McDonald
483c00b3a2 Undo bug for previewing precompiled letters
https://github.com/alphagov/notifications-api/pull/2742/files#diff-d9a6761afaff4a491e60b64f6063654fL265
introduced a change in behaviour which causes template preview to 500
for a precompiled letter that is invalid but not because the content is
outside the printable area.

The changes it back to the previous behaviour, where we b64encode and
utf-8 decode for a page that is invalid for reasons that are not content
outside the printable area, for example a non portrait a4 letter.

I didn't end up writing a unit test for this because I really can't
figure out the existing behaviour we expect for this use case. This
method is too big and complex but I'm confident by looking at the change
in the commit mentioned above that this puts behaviour back to how it
was. Manual testing also confirms the fix.

I've also taken this opportunity to improve two test names whilst I was
looking at things.
2020-03-11 14:34:12 +00:00
David McDonald
34d571405e Refactor of logic to make code more readable
Functionality remains as it was
2020-03-10 13:38:20 +00:00
David McDonald
d9243b8b8a When requesting a png for a single page, show overlay as appropriate
If the png page is invalid then show the overlay for that page
If the png page is not invalid, even if other pages are invalid, then
don't shown the overlay for that page

These keeps the behaviour were if you get the pdf for a pdf which has
invalid pages then the whole PDF is requested (not just a single page)
and all of the pages include the overlay

Tests have been improved to be more explicit about what we are testing
2020-03-09 14:54:58 +00:00
Leo Hemsted
8f7f99234b use Template.is_message_too_long rather than content_count directly
this function returns false for emails and letters so we can clean up
the code and reduce duplication a little bit
2019-11-18 17:00:32 +00:00
Pea Tyczynska
c2825e10b1 Grab metadata when getting pdf letter preview from S3
Also use this metadata to decide whether preview pages need
overlay or not. So far we have always added overlay when validation
has failed. Now we will only show it when validation failed due to
content being outside of printable area.
2019-10-29 16:19:50 +00:00
Katie Smith
0c1fa3852f Add endpoint for getting / creating a hidden template
We want a way of getting the hidden precompiled template from admin,
so this adds an endpoint which gets the template or creates it if it doesn't
exist. The function to get or create the hidden template already existed
but has been moved to the template DAO now that it is used in more
places.
2019-09-12 09:56:10 +01:00
Pea Tyczynska
454690a008 Fix bug in precompiled preview with overlay
The bug treated all pages of png preview as if they were all first
page, showing overlay for address bar
2019-04-24 18:29:01 +01:00
Pea Tyczynska
2d71aea7a1 Get rid of an unneeded flag - destination endpoint is slimmer now and doesnt need it 2019-04-11 16:33:40 +01:00
Pea Tyczynska
dd9e285684 Refactor preview_letter_template_by_notification_id 2019-04-11 14:27:57 +01:00
Pea Tyczynska
63fe210202 Also show overlay on document download of failed letter
And of course test this new flex
2019-04-11 14:27:12 +01:00
Pea Tyczynska
2f52a8fd64 Call overlaid preview for precompiled letters that fail validation
Also test precompiled letter preview with overlay
2019-04-11 14:26:59 +01:00
Leo Hemsted
afc5c96927 Don't fallback to dvla_organisation if letter branding unset
The template preview app now accepts a null value for the `filename` 
parameter. If a service doesn't have a letter branding option set, 
previously we defaulted to their dvla_organisation (probably HM 
Government). Now, we pass through None, so that we generate letters 
without any logo or branding.
2019-02-13 11:58:54 +00:00
Pea Tyczynska
ccd5a758f0 Set postage to second class when creating new letter template
Also get rid of checks for CHOOSE_POSTAGE permission when creating
and updating a template.
2019-01-30 16:26:49 +00:00
Rebecca Law
e4ea208d06 Use the letter_branding logo if it exists otherwise fall back to the dvla_organisation logo. 2019-01-23 12:51:09 +00:00
Pea Tyczynska
4929a6ac08 Include postage in checking if template changed 2018-12-21 16:37:52 +00:00
Pea Tyczynska
19f7678b05 Don't allow to set postage per template if no service permission 2018-12-21 16:37:52 +00:00
Rebecca Law
4120525568 Update error from ResultNotFound to InvalidRequest 2018-11-07 17:07:04 +00:00
Rebecca Law
1e5b990069 Use query to get TemplateFolder by id and service_id 2018-11-07 16:34:51 +00:00
Rebecca Law
1b0b16fa74 Update model and controller to handle parent_folder_id when creating a template.
If the parent_folder_id then check if the folder exists and is for the same service. If it is add the folder to the template model object, the relationship will be persisted when the template is saved. If the folder does not exist or is for a different service, then return a ResultNotFound error.
2018-11-07 16:17:09 +00:00
Rebecca Law
4849ecdf63 Update the template_schema to include a parent_folder_id.
When creating the Tempalte from_json, the folder is passed in. Since some validation should done, as in the folder exists and is for the same service, the folder is passed through to the Tempalte.from_json method.
When the template is persisted so is the relationship to folders.
TODO: If the folder is invalid a specific message should be returned.
2018-11-07 16:17:09 +00:00
Rebecca Law
39198ed67e Using jsonschema for create_template.
Updated jsonschema to Draft7, this allowed a conditional validation on subject, if template_type == 'email' or 'letter' then subject is required.
This version is backward compatible with Draft4.
When creating TempalteRedacted, I've built the dict depending on if the created_by or created_by_id exists.
2018-11-07 16:17:08 +00:00
Katie Smith
022b5b19ff Stop passing dvla_org_id to template preview
We were passing both dvla_org_id and filename to template-preview
temporarily while we switch to only using filename. Now that
template-preview is set up to use the filename, we can stop sending the
dvla_org_id too.
2018-10-23 15:52:44 +01:00
Katie Smith
4dab4fa8ce Pass letter logo filename to template preview
We now pass `filename`, the filename of the letter logo to use, through
to Template Preview in addition to the `dvla_org_id`. Once Template
Preview has been updated to only use the `filename` we will stop
sending the `dvla_org_id`.
2018-10-19 10:07:11 +01:00
Katie Smith
a87be9b74a Use new value of SMS_CHAR_COUNT_LIMIT from utils
Admin, API and utils were all defining a value for SMS_CHAR_COUNT_LIMIT.
This value has been updated in notifications-utils to allow text
messages to be 4 fragments long and notifications-api now gets the value of
SMS_CHAR_COUNT_LIMIT from notifications-utils instead of defining it in
config.

Also updated some tests to check for the higher limit.
2018-08-16 16:34:34 +01:00
Chris Hill-Scott
8ae800b117 Pass date of letter to template preview
Otherwise all letters will show the current date.

Also beefed up the tests around this part of the code a bit.
2018-04-30 15:47:49 +01:00
Ken Tsang
7524402b56 Only hide the notify tag on precompiled on first page 2018-03-29 14:38:38 +01:00
Richard Chapman
79a6ce8782 * Updated imports to comply with pep8 for better maintainability
* Removed extra log messages so there are not two log messages being
generated per exception, as InvalidRequest also logs, updated the
InvalidRequest log message to include the exception type and exception
information
* Added extra asserts to ensure the exception messages are printed
2018-03-12 11:05:05 +00:00
Richard Chapman
04048aa220 Updated the notification template endpoint to extract the pdf page
before sending it to template preview. This stops the whole pdf file
being sent to template preview for each page which is really inefficient
on network traffic and memory usage.

* Added logic to the endpoint to extract the specific page requested
* Updated tests to add a mock for the new call to utils
* Added a new test case for exceptions in the PDF extraction process
2018-03-09 15:50:43 +00:00
kentsanggds
b0b0062b35 Merge pull request #1732 from alphagov/ken-hidden-in-json-response
Return `is_precompiled_letter` field as part of json for notification by id
2018-03-08 15:06:10 +00:00
Ken Tsang
7011b90bd4 Refactor is_precompiled_letter to model 2018-03-07 23:03:03 +00:00
Richard Chapman
d60e802f35 Removed the superfluous variable and pass through as it pulls it
automatically out of sys.exc_info.
2018-03-07 09:51:58 +00:00
Richard Chapman
77a3397ce5 Added option flag to the _get_png_preview method to determine if the
post method content is data or json format.
2018-03-06 15:35:00 +00:00
Richard Chapman
ed9936bba0 Fixed bug where the content header was not being passed onto the post
request. Changed data => json.

Added extra logging to display the error with more detail
2018-03-06 14:42:53 +00:00
Richard Chapman
d0df85a602 Fixed bug where the content header was not being passed onto the post
request. Changed data => json.

Added extra logging to display the error with more detail
2018-03-06 14:24:30 +00:00
Richard Chapman
e91a0efc43 Refactored code to make it more maintainable and changed an error type
* Rather than an abort 404 returned a 500 and InvalidRequest so that the
error is more easily handled on the admin console. If the file is
missing but expected to be there is actually an internal error for admin
* Refactored the code to remove duplicate code in calls to template
preview by creating a new private method which is called with specific
parameters
2018-03-05 14:54:18 +00:00
Richard Chapman
a4feaba309 Added tests to tests for precompiled flow and refactored a little
* Added is_precompiled_letter method to letter/utils.py
* Added tests for letter/utils.py
* Added tests for the rest endpoint
* Moved the Precompiled name to a central location
* Added hidden field to the test method to create a template
2018-03-05 14:11:37 +00:00
Richard Chapman
a9a67ce542 Updated API to handle pre-compiled pdfs
* added a method to letter/utils.py to get the PDF document from the S3
bucket
* added the logic to return the pdf or to produce a png of the pdf
2018-03-02 14:54:28 +00:00
Richard Chapman
42c1040604 Updated API to get it working with Admin.
* Added missing items from template which are required
* Returned the file as a JSON string with the file as a base64 encoded
string
* Updated tests to match teh desired format
2018-03-01 15:01:46 +00:00
Richard Chapman
4c8bc9f430 Added endpoint to get the letter preview from the template preview app
Is will allow the admin application to call into the api instead of
making the call itself. This will allow the api to make decision for
precompiled pdf without having to update the admin app.

- Added new endpoint
- Added tests for the endpoint
2018-03-01 15:01:46 +00:00
Rebecca Law
e59d6d470e Fix the problem with updating the reply_to or service_letter_contact_id for templates.
The history was not being updated properly, we think this is because the declaritive attribute is not being set propery by the property.
When reply_to: None it will update the service_letter_contact_id, but not the service_letter_contact, we think when the history_meta is build the history class and checking if the value is updated it depends which attribute it is checking first.

In order to fix this issue, there is a new dao method to update the reply_to on the Template and insert a new Template history.
2018-01-10 12:40:14 +00:00