Source code for uw.local.teaching.bin.submit_cel_coversheets

"""Automatic printing of CEL examination cover sheets.

Python process to create cover sheets and submit print job to New Media
Services.
"""

from pathlib import Path
from subprocess import check_output
from sys import argv
from tempfile import mkdtemp

import os.path

from uw.sql.wrap import open_psycopg2_db_service_cursor

from uw.local import termtools

from ...print_.db.nms_requisition import nms_requisition
from ...util.mail import odyssey_email
from ...outgoing.make_mail import mailout

from ..db.cursor import Cursor

from .daily_notify import message_to_course_staff

colours = {
    'C': 'white',
    'S': 'pink',
    'O': 'green',
}

[docs]def parse_java_output (java_output): split_lines = (line.split () for line in java_output) return dict ((key, int (value)) for key, value in split_lines)
[docs]def find_paper_colour (pagecounts): return ', '.join ('%d %s' % (pagecounts[key], colours[key]) for key in pagecounts if key != 'C')
[docs]def submit_cel_coversheets (cursor, term): java_dir = mkdtemp () print(java_dir) java_output = check_output (['java', 'ca.uwaterloo.odyssey.exams.printExamCoversheets', term.code (), java_dir]) pagecounts = parse_java_output (java_output.decode ().strip ().split ('\n')) total_pages = sum (pagecounts.values ()) # *** TODO: don't hardcode to jcrane person_id = cursor.execute_required_value ("select person_id from person_identity_complete where userid = %(userid)s", userid='jcrane') job = nms_requisition (cursor) print('job_id=%d' % job.job_id) job.set_contact (person_id) # *** TODO: set term field from term # *** TODO: check field values job.set_flexfield ('5270100100000000000065650116000') job.set_title ('CEL Examination Cover Sheets %s' % term.abbreviation ()) job.set_date_required (cursor.execute_required_value ("select (lower (term_exam_dates) - '1 day'::interval)::date from uw_term where term_id = %(term_id)s", term_id=term.code ())) job.set_end_use ('teaching') job.set_courier (person_id) job.set_copyprint ( pages=total_pages, copies=1, simplex=True, paper_bond=True, size_letter=True, paper_white=True, paper_other=find_paper_colour (pagecounts), ink_black=True, ) job.set_instructions ('\n'.join ('%s.pdf on %s' % (key, colours[key]) for key in pagecounts)) files = ['%s.pdf' % key for key in pagecounts] java_dir = Path (java_dir) for file in files: job.attach_file (file, (java_dir / file).read_bytes ()) blank_pdf_content = (java_dir / 'blank.pdf').read_bytes () # Mail blank covers to CEL staff message = mailout (cursor, odyssey_email, 'CEL Blank Examination Cover Pages', 'Please find attached the blank examination cover pages.') message.add_attachment ('application', 'pdf', None, blank_pdf_content, '%s-cel-blank-covers.pdf' % term.code ()) message_to_course_staff (cursor, message, term.code (), 20070, ['ISC']) message.add_recipient ('Cc', odyssey_email) message.done ()
[docs]def main (): term_code = argv[1] cursor = open_psycopg2_db_service_cursor (cursor_class=Cursor) submit_cel_coversheets (cursor, termtools.fromCode (term_code)) cursor.connection.commit ()