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.
This commit is contained in:
Chris Hill-Scott
2020-01-08 14:29:56 +00:00
parent 5e7ec3e30d
commit 25464a141b
13 changed files with 106 additions and 76 deletions

View File

@@ -20,12 +20,11 @@ from app import (
current_service,
format_date_numeric,
format_datetime_numeric,
job_api_client,
service_api_client,
template_statistics_client,
)
from app.main import main
from app.statistics_utils import add_rate_to_job, get_formatted_percentage
from app.statistics_utils import get_formatted_percentage
from app.utils import (
DELIVERED_STATUSES,
FAILURE_STATUSES,
@@ -281,14 +280,6 @@ def get_dashboard_partials(service_id):
all_statistics = template_statistics_client.get_template_statistics_for_service(service_id, limit_days=7)
template_statistics = aggregate_template_usage(all_statistics)
scheduled_jobs, immediate_jobs = [], []
if job_api_client.has_jobs(service_id):
scheduled_jobs = job_api_client.get_scheduled_jobs(service_id)
immediate_jobs = [
add_rate_to_job(job)
for job in job_api_client.get_immediate_jobs(service_id)
]
stats = aggregate_notifications_stats(all_statistics)
column_width, max_notifiction_count = get_column_properties(3)
@@ -310,7 +301,6 @@ def get_dashboard_partials(service_id):
return {
'upcoming': render_template(
'views/dashboard/_upcoming.html',
scheduled_jobs=scheduled_jobs
),
'inbox': render_template(
'views/dashboard/_inbox.html',
@@ -337,9 +327,8 @@ def get_dashboard_partials(service_id):
),
'jobs': render_template(
'views/dashboard/_jobs.html',
jobs=immediate_jobs
jobs=current_service.immediate_jobs,
),
'has_jobs': bool(immediate_jobs),
'usage': render_template(
'views/dashboard/_usage.html',
column_width=column_width,

View File

@@ -19,14 +19,12 @@ from app import (
current_service,
format_datetime_short,
format_thousands,
job_api_client,
notification_api_client,
service_api_client,
)
from app.main import main
from app.main.forms import SearchNotificationsForm
from app.models.job import Job
from app.statistics_utils import add_rate_to_job
from app.utils import (
generate_next_dict,
generate_notifications_csv,
@@ -44,31 +42,25 @@ from app.utils import (
@main.route("/services/<uuid:service_id>/jobs")
@user_has_permissions()
def view_jobs(service_id):
page = int(request.args.get('page', 1))
jobs_response = job_api_client.get_page_of_jobs(service_id, page=page)
jobs = [
add_rate_to_job(job) for job in jobs_response['data']
]
jobs = current_service.get_page_of_jobs(page=request.args.get('page'))
prev_page = None
if jobs_response['links'].get('prev', None):
prev_page = generate_previous_dict('main.view_jobs', service_id, page)
if jobs.prev_page:
prev_page = generate_previous_dict('main.view_jobs', service_id, jobs.current_page)
next_page = None
if jobs_response['links'].get('next', None):
next_page = generate_next_dict('main.view_jobs', service_id, page)
if jobs.next_page:
next_page = generate_next_dict('main.view_jobs', service_id, jobs.current_page)
scheduled_jobs = ''
if not current_user.has_permissions('view_activity') and page == 1:
if not current_user.has_permissions('view_activity') and jobs.current_page == 1:
scheduled_jobs = render_template(
'views/dashboard/_upcoming.html',
scheduled_jobs=job_api_client.get_scheduled_jobs(service_id),
hide_heading=True,
)
return render_template(
'views/jobs/jobs.html',
jobs=jobs,
page=page,
prev_page=prev_page,
next_page=next_page,
scheduled_jobs=scheduled_jobs,

View File

@@ -15,12 +15,7 @@ from notifications_utils.pdf import pdf_page_count
from PyPDF2.utils import PdfReadError
from requests import RequestException
from app import (
current_service,
job_api_client,
notification_api_client,
service_api_client,
)
from app import current_service, notification_api_client, service_api_client
from app.extensions import antivirus_client
from app.main import main
from app.main.forms import LetterUploadPostageForm, PDFUploadForm
@@ -47,20 +42,18 @@ MAX_FILE_UPLOAD_SIZE = 2 * 1024 * 1024 # 2MB
def uploads(service_id):
# No tests have been written, this has been quickly prepared for user research.
# It's also very like that a new view will be created to show uploads.
page = int(request.args.get('page', 1))
uploads_response = job_api_client.get_uploads(service_id, page=page)
uploads = current_service.get_page_of_uploads(page=request.args.get('page'))
prev_page = None
if uploads_response['links'].get('prev', None):
prev_page = generate_previous_dict('main.uploads', service_id, page)
if uploads.next_page:
prev_page = generate_previous_dict('main.uploads', service_id, uploads.current_page)
next_page = None
if uploads_response['links'].get('next', None):
next_page = generate_next_dict('main.uploads', service_id, page)
if uploads.prev_page:
next_page = generate_next_dict('main.uploads', service_id, uploads.current_page)
return render_template(
'views/jobs/jobs.html',
jobs=uploads_response['data'],
page=page,
jobs=uploads,
prev_page=prev_page,
next_page=next_page,
scheduled_jobs='',