Commit Graph

53 Commits

Author SHA1 Message Date
Rebecca Law
68d28aa83b The update of SQLAlchemy 1.4.10 has caused some conflicts in our code. This PR fixes most of those conflicts.
- sqlalchemy.sql.expression.case must include an else statement.
- clearly define list of columns for inbound_sms_history insert, getting the list from InboundSmsHistory.__table__.c was causing data type errors.
- remove relationships when not needed, the foreign key relationship is established in the creation of the column. This will get rid of the warnings referenced here: http://sqlalche.me/e/14/qzyx.
- update queries now that he user relationship in ServiceUser db model has been removed.
- move the check that a template is archived to the view instead of the dao method. The check was clearing the session before the version history could be done.

Deleting notifications in the night tasks still needs to be
investigated. The raw sql is causing an error.
2021-04-29 13:32:36 +01:00
Rebecca Law
85895a9e8b Revert "Scheduled weekly dependency update for week 16" 2021-04-28 10:17:16 +01:00
Rebecca Law
1b070d69a1 The update of SQLAlchemy 1.4.10 has caused some conflicts in our code. This PR fixes most of those conflicts.
- sqlalchemy.sql.expression.case must include an else statement.
- clearly define list of columns for inbound_sms_history insert, getting the list from InboundSmsHistory.__table__.c was causing data type errors.
- remove relationships when not needed, the foreign key relationship is established in the creation of the column. This will get rid of the warnings referenced here: http://sqlalche.me/e/14/qzyx.
- update queries now that he user relationship in ServiceUser db model has been removed.
- move the check that a template is archived to the view instead of the dao method. The check was clearing the session before the version history could be done.

Deleting notifications in the night tasks still needs to be
investigated. The raw sql is causing an error.
2021-04-26 11:50:30 +01:00
Rebecca Law
d4009ffc52 Rename database management functions.
Rename @transactional to @autocommit.
Rename nested_transaction to tranaction.
2021-04-19 10:56:00 +01:00
Ben Thorner
a91fde2fda Run auto-correct on app/ and tests/ 2021-03-12 11:45:45 +00:00
Leo Hemsted
7ecd7341b0 add broadcast to template_types and add broadcast_data
had to go through the code and change a few places where we filter on
template types. i specifically didn't worry about jobs or notifications.

Also, add braodcast_data - a json column that might contain arbitrary
broadcast data that we'll figure out as we go. We don't know what it'll
look like, but it should be returned by the API
2020-07-06 15:47:13 +01: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 (Malgorzata Tyczynska)
ccfa1c8228 Revert "Ensure dao_create_template doesn't clash with new trigger" 2019-06-26 18:01:20 +01:00
Pea Tyczynska
673cabe6b8 Ensure dao_create_template doesn't clash with new trigger
New trigger creates template_redacted row after insert to template
table

And update tests
2019-06-25 15:40:10 +01:00
Chris Hill-Scott
eeb90bed57 Do all version table writes in one commit
The behaviour of stacking the version decorators does not work as
expected.

What you would expect to happen is that each decorator causes a history
row to be written for its respective model object.

What actually happens is that the first decorator adds history records
to the database session, but then causes the database session to commit.
This means that subsequent uses of this decorator find a clean session,
and therefore no changes to copy to their respective history tables.

This commit changes the intended use of the decorator so that it is only
used once per function, and accepts multiple definitions of what to
record history for. This way it can record everything that needs to go
into the history before doing anything that would risk flushing the
session.
2019-04-05 16:11:46 +01:00
Pea Tyczynska
2b0668c7c8 Fix dao_update_template_reply_to so it maintains postage in TemplateHistory 2019-02-12 10:41:44 +00:00
Pea Tyczynska
3ce0024eec Remove unused functions for getting template statistics 2019-01-15 12:15:20 +00:00
Katie Smith
7f918d7c48 Remove relationship to folder when archiving a template
When a template is archived, it should no longer belong to any folder.

If we don’t do this it will make it very hard to delete folders later
(because folders can only be deleted if they have no templates or folders
inside them).

We originally tried to check if the link between a template and folder
should be removed with

`if template.archived and template.folder:`

instead of using `if template.archived:`. However, this caused issues
because checking `template.folder` flushes the session. Since the
session is no longer dirty, the versioning decorator doesn't work as
expected and doesn't create a new row in `TemplateHistory`.
2018-11-08 17:25:45 +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
Leo Hemsted
9e8b6fd00d refactor template stats endpoint to read from new redis keys
New redis keys are partitioned per service per day. New process is as
follows:

* require a count of days to filter by. Currently admin always gives 7.
* for each day, check and see if there's anything in redis. There won't
  be if either a) redis is/was down or b) the service didn't send any
  notifications that day
  - if there isn't, go to the database and get a count out.
* combine all these stats together
* get the names/template types etc out of the DB at the end.
2018-04-30 11:13:21 +01:00
Ken Tsang
7011b90bd4 Refactor is_precompiled_letter to model 2018-03-07 23:03:03 +00:00
Ken Tsang
564504bf97 Add template hidden field in response 2018-03-06 12:46:06 +00:00
Alexey Bezhan
984a5050db Don't return hidden templates in API service template responses
Brings back filtering hidden templates from the API responses.
2018-02-26 13:28:08 +00:00
Alexey Bezhan
9b3a9a55c4 Revert "Don't return hidden templates in API service template responses"
Filtering out hidden templates requires all existing templates to
have `hidden` flag set, which can only be done by a migration after
the code that sets the flag to `False` by default for new templates
has been released.

This removes the filtering logic until the migration has been released.
2018-02-26 11:44:25 +00:00
Alexey Bezhan
19dbe7da8d Don't return hidden templates in API service template responses
Removes hidden templates from the service templates list and returns
404 when searching for a hidden template or template versions by ID.
2018-02-23 17:52:25 +00:00
Rebecca Law
03deb804b0 -Fix typo
- return template from dao not the history
2018-01-10 13:32:54 +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
Chris Hill-Scott
b2ba2afbcd List templates in alphabetical order
Currently templates are ordered by the newest created first. This made
sense when, after creating a new template, you were landed on the page
that listed all the templates. In other words, you needed to see
confirmation of the thing that you’ve just done.

Now (since https://github.com/alphagov/notifications-admin/pull/1195)
you get landed on the page for just that template. So you always see
the template you’ve just created, no matter how the list of templates is
ordered. So we are free to change the order of the templates.

Ordering by time created is not great, because it gives users no control
over which templates appear first. For example, our research reported
this from one team:

> One frustration they have is that when you add a new template it
> automatically goes to the top of the list. To get round this, whenever
> they add a new template they delete all of the existing ones and start
> again because they want to keep their templates in numerical order.
> They'd like to be able to control the order of templates in the list.

We _could_ give people some sort of drag-and-drop template ordering
thing. But this feels like overkill. I think that alphabetical order is
better because:

- it’s easily discoverable – anyone who wants to know how a list is
  ordered can quickly tell just by looking at it
- it’s universal – everyone knows how alphabetical ordering works
- it’s familiar – this is how people documents on their computer are
  ordered; there’s no new UI to learn
- it’s what users are doing already – from the same service as above:
  > They number their templates 1,2a, 2b, 3a etc

So this commit changes the ordering from newest created first to
alphabetical.

Previous changes to template order and navigation:
- https://github.com/alphagov/notifications-admin/pull/1163
- https://github.com/alphagov/notifications-admin/pull/1195
- https://github.com/alphagov/notifications-admin/pull/1330

Implementation notes
---

I refactored some of the tests here. I still don’t think they’re great
tests, but they’re a little more Pythonic now at least.

I also added a sort by template type, so that the order is deterministic
when you have, for example, an email template and a text message
template with the same name. If you have two text message templates with
the same name you’re on your own.
2017-11-23 15:11:07 +00:00
Leo Hemsted
29fc81090e add template personalisation redaction
If passing in `redact_personalisation` to the template update endpoint,
we should mark that template permanently as redacted - this means that
we won't ever return the personalisation for any notifications for it.

This is to be used with templates containing one time passwords, 2FA
codes or other sensitive information that you may not want service
workers to be able to see.

This is implemented via a separate table, `template_redacted`, which
just contains when the template was redacted.
2017-06-28 15:53:08 +01:00
Ken Tsang
fc0cfa5dbf Corrected code to handle template types 2017-04-19 10:51:12 +01:00
Imdad Ahad
a4dc614ef7 Restore dao_create_template and use custom template fixture instead 2017-03-08 13:03:44 +00:00
Imdad Ahad
73d5ce4f8b Add tests to verify correctness of the switching provider task 2017-02-24 12:23:39 +00:00
Rebecca Law
53b7ad0961 Moved the cache key to the utils module.
Renamed the dao method.
2017-02-14 14:22:52 +00:00
Rebecca Law
1c6cfb9bc8 Got the SqlAlchemy query to work 2017-02-14 11:05:23 +00:00
Rebecca Law
681f52b691 Missed some print statements and unused imports. 2017-02-14 09:59:33 +00:00
Rebecca Law
2346634fec Some code clean up, removed print statements. 2017-02-14 09:54:37 +00:00
Rebecca Law
458adefcb8 Added a redis cache for the template usage stats.
Cache expires every 10 minutes, but will help with the every 2 second query, especially when a job is running.
There is some clean up and qa to do for this yet
2017-02-13 18:47:29 +00:00
Leo Hemsted
049514d4b2 remove history-meta for templates, replace with hand-made history table
history-meta's dynamic magic is insufficient for templates, where we
need to be able to refer to the specific history table to take
advantage of sqlalchemy's relationship management (2/3rds of an ORM).
So replace it with a custom made version table.

Had to change the version decorator slightly for this
2016-08-09 16:56:43 +01:00
Chris Hill-Scott
c2ae4773da Order templates by newest created first
When you add a new template, it’s probably the one that you want to do
subsequent stuff with. But it’s also helpful to see the template in
context (with its siblings) to understand that there are multiple
templates. So we don’t want to do what we do in
https://github.com/alphagov/notifications-admin/pull/648
for adding a new template.

But we _can_ make your brand-new template appear first by always
ordering by when the template was created.

This also removes the confusion caused by having `updated_at` affecting
order, and causing the templates to move around all the time.
2016-06-06 10:44:40 +01:00
Leo Hemsted
5ebae9c66c only return unarchived templates when getting all templates
get_specific_template functions happily return archived templates
2016-05-23 14:20:02 +01:00
Rebecca Law
992f9d78f9 There is a problem where columns on the templates table were not set.
It is also discovered that columns that have a default value and use the version mixin must set the value when creating the db object before the insert otherwise the history table will be missing the default value.
Updated the templates_history.created_by_id with a value where missing, using the current version of the template for this value.
Update templates_history.archived to false. This is okay as we do not yet have a way to set this value to true.
Removed the versions attribute from the TemplateSchema, there is not a need for this column.
2016-05-16 16:16:14 +01:00
Rebecca Law
917110870d Use the template version at the time the notification is created or at the time the job is created.
Update notifications/sms|email endpoint to send the template version to the queue.
Update the process_job celery talk to send the template version to the queue.
When the send_sms|send_email task runs it will get the template by id and version.

Created a data migration script to add the template_vesion column for jobs and notifications.
The existing jobs and notifications are given the template_version of the current template.
There is a chance this is the wrong template version, but deemed okay since the application is not live.

Create unit test for the dao_get_template_versions method.
Rename /template/<id>/version to /template/<id>/versions which returns all versions for that template id and service id.
2016-05-13 16:25:05 +01:00
Nicholas Staples
b28474d74c Get all versions for a template endpoint added. 2016-05-09 15:59:34 +01:00
Nicholas Staples
9b3d4a6087 Template history endpoint added. All tests passing.
Code quality fix.
2016-05-06 15:47:13 +01:00
Nicholas Staples
b56e324a4c Working tests and provider stats table.
Fix for tests and import error.

Added tests and updated for code review comments.
2016-04-25 12:20:06 +01:00
Chris Hill-Scott
29b9e50952 Order templates by last updated date
The template you want to send/edit is probably one of the ones you
recently sent from or edited.

https://www.pivotaltracker.com/story/show/117159263
2016-04-11 17:39:49 +01:00
Rebecca Law
123b0ead3a Replaced first queries with one queries, which throws a NoResultFound.
Able to remove many of the None checks as a result of this.
Fixed the tests were needed.
2016-03-11 15:34:20 +00:00
Rebecca Law
e055590b07 Changed db queries to use one, which throws NoResultFound exception, this exception is dealt with in our error handlers.
Now a lot of the if none checks can be removed.
2016-03-11 12:39:55 +00:00
Martyn Inglis
1667f82df1 Removed some unused template dao methods 2016-02-24 11:51:02 +00:00
Martyn Inglis
443691402f Slight tiny up of templates_dao
- 1 method per get query
- logic around create/update removed
2016-02-22 09:46:16 +00:00
Nicholas Staples
aba1cd2ed5 Sqs queues now populated from all create_notification api calls. Marshmallow schemas added for notification. 2016-02-03 13:16:19 +00:00
Nicholas Staples
2451f7e53d Added support for validation only of put requests. 2016-01-29 11:11:00 +00:00
Nicholas Staples
11c7795396 Merged with master. 2016-01-22 14:46:03 +00:00
Nicholas Staples
a9fe6ad469 Working code and tests. 2016-01-22 14:43:30 +00:00
Nicholas Staples
e657958af4 Templates fix with tests working. 2016-01-22 10:44:34 +00:00