"""Job-related URL handlers.
URL handlers for listing jobs and viewing individual jobs.
"""
from operator import attrgetter
from ll.xist.ns import html
from uw.web.html.format import make_table_format, format_datetime, format_return
from uw.web.wsgi import status
from uw.web.wsgi.delegate import delegate_get_post
from uw.web.wsgi.function import return_html, return_pdf
from ...util.identity import use_remote_identity
from ..bin.requisition_received import complete_filename
@delegate_get_post
@return_html
def job_index (cursor):
"""Job index URL handler.
Displays a list of all jobs in the system.
"""
result = [format_return ('Main Menu')]
jobs = cursor.all_jobs ()
result.append (format_jobs (jobs, "../"))
return 'Job Index', result
@delegate_get_post
@use_remote_identity
@return_html
def job_view (cursor, remote_identity, job):
"""Job view URL handler.
Displays information about the in-context job, including a link to the
related requisition if any.
"""
result = [format_return ('Main Menu', 'Jobs')]
if job.requisition_num is None:
result.append (html.p ('This job is not associated with a requisition.'))
else:
result.append (html.p (
'This job is associated with Requisition ',
html.a ('R%06d' % job.requisition_num, href="../../req/R%06d/pdf" % job.requisition_num),
', which arrived ',
format_datetime (job.requisition_arrived),
'.'
))
result.append (html.h2 ('Form Values'))
result.append (make_table_format (
('Field', attrgetter ('field_code')),
('Value', attrgetter ('field_value')),
) (cursor.execute_tuples ("select * from nms_requisition_form_value where job_id = %(job_id)s order by field_code", job_id = job.job_id)))
result.append (html.h2 ('Files'))
if cursor.check_perm_admin (person_id=remote_identity.person_id):
format_file = lambda r: html.a (r.file_code, href="file/%s" % r.file_code)
else:
format_file = attrgetter ('file_code')
result.append (make_table_format (
('File', format_file),
('Size', attrgetter ('file_size')),
) (cursor.execute_tuples ("select file_code, length (file_contents) as file_size from nms_requisition_file where job_id = %(job_id)s order by file_code", job_id = job.job_id)))
title = 'Job %d' % job.job_id
if job.requisition_title is not None:
title += ': ' + job.requisition_title
return title, result
@return_pdf
def job_pdf (cursor, job, file_code):
"""Job file PDF download URL handler.
Serves the appropriate job PDF file to the client.
"""
row = cursor.file_by_ids (job_id=job.job_id, file_code=file_code)
if row is None:
raise status.HTTPNotFound ()
return (row.file_contents, complete_filename (row))