mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-06-28 03:12:04 -04:00
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.
This commit is contained in:
@@ -8,6 +8,7 @@ from notifications_utils.timezones import utc_string_to_aware_gmt_datetime
|
||||
from werkzeug.utils import cached_property
|
||||
|
||||
from app.models import JSONModel, ModelList
|
||||
from app.models.job import PaginatedJobs
|
||||
from app.notify_client.contact_list_api_client import contact_list_api_client
|
||||
from app.s3_client.s3_csv_client import (
|
||||
get_csv_metadata,
|
||||
@@ -134,6 +135,13 @@ class ContactList(JSONModel):
|
||||
file_name, extention = path.splitext(self.original_file_name)
|
||||
return f'{file_name}.csv'
|
||||
|
||||
def get_jobs(self, *, page):
|
||||
return PaginatedJobs(
|
||||
self.service_id,
|
||||
contact_list_id=self.id,
|
||||
page=page,
|
||||
)
|
||||
|
||||
|
||||
class ContactLists(ModelList):
|
||||
|
||||
|
||||
@@ -234,8 +234,8 @@ class ScheduledJobs(ImmediateJobs):
|
||||
class PaginatedJobs(PaginatedModelList, ImmediateJobs):
|
||||
client_method = job_api_client.get_page_of_jobs
|
||||
|
||||
def __init__(self, service_id, *, page=None):
|
||||
super().__init__(service_id, page=page)
|
||||
def __init__(self, service_id, *, contact_list_id=None, page=None):
|
||||
super().__init__(service_id, contact_list_id=contact_list_id, page=page)
|
||||
|
||||
|
||||
class PaginatedUploads(PaginatedModelList, ImmediateJobs):
|
||||
|
||||
@@ -24,12 +24,14 @@ class JobApiClient(NotifyAdminAPIClient):
|
||||
|
||||
return job
|
||||
|
||||
def get_jobs(self, service_id, limit_days=None, statuses=None, page=1):
|
||||
def get_jobs(self, service_id, *, limit_days=None, contact_list_id=None, statuses=None, page=1):
|
||||
params = {'page': page}
|
||||
if limit_days is not None:
|
||||
params['limit_days'] = limit_days
|
||||
if statuses is not None:
|
||||
params['statuses'] = ','.join(statuses)
|
||||
if contact_list_id is not None:
|
||||
params['contact_list_id'] = contact_list_id
|
||||
|
||||
return self.get(url='/service/{}/job'.format(service_id), params=params)
|
||||
|
||||
@@ -50,11 +52,12 @@ class JobApiClient(NotifyAdminAPIClient):
|
||||
if job['job_status'] != 'cancelled'
|
||||
)
|
||||
|
||||
def get_page_of_jobs(self, service_id, page):
|
||||
def get_page_of_jobs(self, service_id, *, page, contact_list_id=None):
|
||||
return self.get_jobs(
|
||||
service_id,
|
||||
statuses=self.NON_SCHEDULED_JOB_STATUSES,
|
||||
page=page,
|
||||
contact_list_id=contact_list_id,
|
||||
)
|
||||
|
||||
def get_immediate_jobs(self, service_id):
|
||||
|
||||
@@ -1,9 +1,28 @@
|
||||
from datetime import datetime
|
||||
|
||||
from app.models.contact_list import ContactList
|
||||
from app.models.job import PaginatedJobs
|
||||
|
||||
|
||||
def test_created_at():
|
||||
created_at = ContactList({'created_at': '2016-05-06T07:08:09.061258'}).created_at
|
||||
assert isinstance(created_at, datetime)
|
||||
assert created_at.isoformat() == '2016-05-06T08:08:09.061258+01:00'
|
||||
|
||||
|
||||
def test_get_jobs(mock_get_jobs):
|
||||
contact_list = ContactList({'id': 'a', 'service_id': 'b'})
|
||||
assert isinstance(contact_list.get_jobs(page=123), PaginatedJobs)
|
||||
mock_get_jobs.assert_called_once_with(
|
||||
'b',
|
||||
contact_list_id='a',
|
||||
statuses={
|
||||
'finished',
|
||||
'sending limits exceeded',
|
||||
'ready to send',
|
||||
'sent to dvla',
|
||||
'pending',
|
||||
'in progress',
|
||||
},
|
||||
page=123,
|
||||
)
|
||||
|
||||
@@ -1808,7 +1808,7 @@ def mock_has_no_jobs(mocker):
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_get_jobs(mocker, api_user_active):
|
||||
def _get_jobs(service_id, limit_days=None, statuses=None, page=1):
|
||||
def _get_jobs(service_id, limit_days=None, statuses=None, contact_list_id=None, page=1):
|
||||
if statuses is None:
|
||||
statuses = ['', 'scheduled', 'pending', 'cancelled', 'finished']
|
||||
|
||||
|
||||
Reference in New Issue
Block a user