41 Commits

Author SHA1 Message Date
Beverly Nguyen
8a41de8f07 Using StrEnum to drop .value 2025-07-04 17:22:21 -07:00
Beverly Nguyen
b73b4ac73e PART 1: Created and implemented enums throughout the codebase to replace hardcoded status strings, improving type safety and reducing the risk of typos. 2025-07-04 16:37:04 -07:00
Kenneth Kehl
8c9721d8e2 notify-api-412 use black to enforce python coding style 2023-08-25 09:12:23 -07:00
Ryan Ahearn
de668d7aba Remove contact-list references from code 2023-04-12 15:35:14 -04:00
stvnrlly
944715ac46 big commit with letters removal 2022-12-05 15:33:44 -05:00
stvnrlly
e12e780a05 code cleanup 2022-11-28 15:53:56 -05:00
stvnrlly
8bd1285bfa more test fixes 2022-11-28 10:48:51 -05:00
stvnrlly
a2b58c926e more test fixes & letter trimming 2022-11-22 22:50:47 -05:00
stvnrlly
ca1897973a test time fixes and bonus letter removal 2022-11-22 17:03:42 -05:00
stvnrlly
46723b6c11 initial timezone pass, which breaks many tests 2022-11-22 12:00:29 -05:00
Leo Hemsted
a602ffceb9 fix bug where job reports showed before jobs finished
for a job to be finished there are two requirements:

* the status to be "finished"
* the percentage complete to be 100%

The job status is set to finished when the process_job task finishes
(even though not all process_row may have finished). The
percentage_complete is calculated by comparing the number of
notifications in the database with the number of rows in the
spreadsheet.

This was inadvertently changed from an "and" to an "or" clause two years
ago. This meant that people could download a report when the status was
finished but not all notifications were present in the database. Lets
change it back.

7d52ac97f1 (diff-44b012cad205379c481bed244ddb2294bae5ee85dcd01f4aee932a2bd85b67b2L86-R100)
2022-01-14 15:48:04 +00:00
Ben Thorner
c17d438de8 DRY-up email revalidation check
Previously this was duplicated between the "two_factor" and the
"webauthn" views, and required more test setup. This DRYs up the
check and tests it once, using mocks to simplify the view tests.

As part of DRYing up the check into a util module, I've also moved
the "is_less_than_days_ago" function it uses.
2021-06-14 12:52:54 +01:00
Ben Thorner
2a4aa8b4e1 Extract letter utility code into own module
This provides more room for expansion, and reduces the amount of
arbitrary code in the __init__.py file for the new package.
2021-06-09 13:59:06 +01:00
Chris Hill-Scott
b579f68411 Limit jobs sent from contact list by data retention
On the uploads page we only show jobs which are within a service’s data
retention.

This commit does the same for when we’re listing the jobs for a contact
list. This matches the UI, which says a contact list has been ‘used
`<count_of_jobs>` in the last <data_retention> days’
2020-12-01 13:57:56 +00:00
Chris Hill-Scott
423875011c Show jobs on contact list
It’s a bit unintuitive that starting a job from a contact list makes a
copy of the file, which has no relationship to the list it was copied
from. This is more of an implementation detail, rather than something
that comes from people’s mental models of what is going on. Or at least
that’s what I hypothesise.

I think it’s clearer to show jobs that come from contact lists within
the lists that they were created from. By naming the jobs by template
this gives a clearer view of what messages have been sent to the group
over time.
2020-11-30 13:54:54 +00:00
Chris Hill-Scott
8f5c07336d Add method to get jobs for a contact list
Because the API lets us query jobs by contact list now, let’s add the
model and client layer code that will let us display them in the admin
app.
2020-11-30 13:54:53 +00:00
Chris Hill-Scott
fc1ca3ab2f Refactor to make pagination reusable
The responses we get to paginated queries from the API are fairly
consistent, so we should be able to reuse the code that takes JSON
from the API and turns it into Python objects. This commits factors out
that code so that it is reusable (by inheriting from it).
2020-11-30 13:54:53 +00:00
Chris Hill-Scott
38e9e84f77 Raise exception when overriding a custom property
If a subclass of `JSONModel` defines a property then we shouldn’t try
to override it with the value from the underlying dictionary.

Rather than silently fail we should raise an exception because it will
help keep our list of `ALLOWED_PROPERTIES` nice and tidy.
2020-10-28 10:08:45 +00:00
Chris Hill-Scott
89c63e3243 Remove custom getattr implementation from JSONModel
We wrote custom `__getattr__` and `__getitem__` implementation to make
it easier to find code that was accessing fields in the dictionary that
we weren’t aware of. See this commit for details:
b48305c50d

Now that no view-layer code accesses the service dictionary directly we
don’t need to support this behaviour any more. By removing it we can
make the model code simpler, and closer to the `SerialisedModel` it
inherits from.

It still needs some custom implementation because:
- a lot of our test fixtures are lazy and don’t set up all the expected
  fields, so we need to account for fields sometimes being present in
  the underlying dictionary and sometimes not
- we often implement a property that has the same name as one of the
  fields in the JSON, so we have to be careful not to try to override
  this property with the value from the underlying JSON
2020-10-19 15:52:51 +01:00
Chris Hill-Scott
5c2469b24e Refactor to use shared date comparison function
This means we don’t have to repeatedly do timezone conversions or string
to datetime conversions in our business logic.
2020-09-29 13:38:00 +01:00
Chris Hill-Scott
86ec9430a2 Fix incorrect processing started time on jobs
We were doing the UTC to BST conversion twice, meaning the job was shown
as being started 2 hours later than UTC, and 1 hour later than actual
BST.
2020-09-28 14:53:42 +01:00
Chris Hill-Scott
34f5417844 Group uploaded letters by day of printing
Some teams have started uploading quite a lot of letters (in the
hundreds per week). They’re also uploading CSVs of emails. This means
the uploads page ends up quite jumbled.

This is because:
- there’s just a lot of items to scan through
- conceptually it’s a bit odd to have batches of things displayed
  alongside individual things on the same page

So instead we’re going to start grouping together uploaded letters. This
will be by the date on which we ‘start’ printing them, or in other
words the time at which they can no longer be cancelled.

This feels like a natural grouping, and it matches what we know about
people’s mental models of ‘batches’ and ‘runs’ when talking about
printing.

This grouping will be done in the API, so all this commit need to do is:
- be ready to display this new type of pseudo-job
- link to the page that displays all the uploaded letters for a given
  print day
2020-05-11 14:29:03 +01:00
Chris Hill-Scott
ee8436ca85 Differentiate between different kinds of uploads
Knowing what kind of upload a thing is is useful.

And the information that is useful to show about each upload depends on
what kind of upload it is.
2020-02-27 17:34:51 +00:00
Chris Hill-Scott
f9310dd2ed Use template_type property directly from API
It’s included in the job/upload response now[1]. So we don’t need to
fetch the template every time we want to access it.

1. https://github.com/alphagov/notifications-api/pull/2728
2020-02-27 16:46:51 +00:00
Pea Tyczynska
9ebd18ea85 Add upload_type property to Job class 2020-02-04 15:36:55 +00:00
Chris Hill-Scott
291734b0c4 Merge branch 'master' into fix-new-jobs-showing-as-deleted 2020-01-21 14:24:40 +00:00
Chris Hill-Scott
f6a263a7b2 Rename property to more accurately describe purpose
`recently_created` says it would just be looking at the `created_at`
field to see if it's been created recently. Technically this method
isn't doing that, whilst its behaviour would be similar, it's actually
different and maybe therefore a bit misleading.
2020-01-21 14:07:23 +00:00
Chris Hill-Scott
d93866bc7e Use utils function to parse datetime strings
Rather than hard-coding a format string in a bunch of different places
we can use the function we already have in utils.

This commit also refactors some logic around password resets to put the
date-parsing changes in the most sensible bit of the codebase, so it’s
clearer what the intention of the view-layer code is.
2020-01-21 13:55:57 +00:00
Chris Hill-Scott
0202f73f9a Remove job_status from allowed properties
We can’t guarantee it’s always present, so shouldn’t allow direct access
to it.
2020-01-20 16:47:09 +00:00
Chris Hill-Scott
32105b3328 Don’t assume jobs status will be present
The API response for jobs includes a field called `job_status`. The API
response for uploads doesn’t.

The `Job` mode handles uploads and jobs, so it needs to account for the
possibility of the field not being there.
2020-01-20 15:25:47 +00:00
Chris Hill-Scott
87b2686875 Use time to determine why notifications don’t exist
Notifications won’t exist for a job if:
- it’s just started
- it started a long time ago (older than the retention period)

We have a bug where:
1. Job starts processing, puts notifications on queue
2. Job finishes processing, sets status to `finished`
3. First notification gets picked up off the queue and put in the
   database

In between 2. and 3. it’s possible for a job to be finished, but also to
have no notifications. We’re saying this is because the notifications
have been deleted, whereas really it’s because they haven’t been created
yet.

This commit fixes that bug by introducing the concept of recency for
jobs.

‘Recent’ is defined as 1 day, which is:
- a lot longer than it takes to create any notifications
- a bit shorter than anyone’s retention time

N.B. `processing_started` is defined here:
879ba1d5f0/app/models.py (L1194)

It can be `None` for scheduled jobs that haven’t started yet.
2020-01-17 13:27:20 +00:00
Chris Hill-Scott
a5fe50ce72 Merge pull request #3262 from alphagov/rename-model-property
Rename `client` property on `ModelLists`
2020-01-17 11:08:08 +00:00
Chris Hill-Scott
fa4fd1c896 Account for missing scheduled for field
Jobs have a `scheduled_for` field. Single letter uploads don’t.

At the moment we treat both of them as `Job`s. So the `Job` model needs
to account for when the `scheduled_for` field is missing.
2020-01-17 10:01:39 +00:00
Chris Hill-Scott
c4818eb7f2 Rename property on ModelLists
The property doesn’t represent the whole client, but just one method on
it. So this commit renames the property to better describe what it is
designed to store.
2020-01-16 16:31:20 +00:00
Chris Hill-Scott
cbf0d905e3 Remove duplicative notifications_processed property
It returned the same value (and had the same code as
`.notifications_sent`).

I think `.notifications_sent` is a better name because it’s closer to
the language (‘sending’ and ‘sent’) that we use in the interface.
2020-01-16 15:40:49 +00:00
Chris Hill-Scott
fda979a5f2 Refactor again 2020-01-13 15:10:21 +00:00
Chris Hill-Scott
2f6a8a5864 Refactor stats calcualtions 2020-01-13 15:10:19 +00:00
Chris Hill-Scott
7d52ac97f1 Move stats to the model
The API clients should just deal with calling the API and returning the
data from it.

Inferring things from the data is more logically done at the model
layer. But we couldn’t do that before, because we didn’t have a model
layer for jobs.
2020-01-13 15:10:16 +00:00
Chris Hill-Scott
340cb33fdd Refactor ‘finished’ to the model layer
By moving it from the view we reduce the complexity of the methods in
the view layer, so it’s easier to see what they do.

This also renames the variable `finished` to the property
`processing_finished` to disambiguate from the `job_status` field in the
JSON, which can also have a value of `finished`.
2020-01-13 15:10:14 +00:00
Chris Hill-Scott
25464a141b Use a ModelList for lists of jobs
This follows the pattern of what we’ve done with services, users and
events.

It gives us a way of neatly instantiating a model for each item in the
list we get back from the API and reduces the complexity of the view
layer code.

Now is a good time to do this because we’re going to be making a bunch
of changes to the jobs pages, and those changes will be easier to code
and understand with a sensible model behind them.
2020-01-13 15:10:10 +00:00
Chris Hill-Scott
5e7ec3e30d Make a job model for individual jobs
This follows the pattern of what we’ve done with services, users and
events.

It gives us a better interface to the data we get back from the API than
dealing with the raw JSON directly.

Now is a good time to do this because we’re going to be making a bunch
of changes to the jobs pages, and those changes will be easier to code
and understand with a sesnsible model behind them.
2020-01-13 13:05:35 +00:00