Source code for uw.local.print_.bin.submit_requisitions

from pathlib import Path
from urllib.parse import urlencode
import urllib.request, urllib.error, urllib.parse

from uw.sql.wrap import open_psycopg2_db_service_cursor

request_url = "https://info.uwaterloo.ca/newmediaservices/newreq/index.php"
save_file_dir = Path.home () / "spool" / "print" / "job"
save_file_pattern = "%d"

[docs]def submit_requisition (cursor, job_id): req_params = dict (cursor.execute_tuples ("select field_code, field_value from nms_requisition_form_value where job_id = %(job_id)s", job_id=job_id)) # Fields that must be set to specific values # *** TODO: use odyssey@uwaterloo.ca, use procmail to split from other email req_params['email'] = "print-requisitions@odyssey.uwaterloo.ca" # In order to receive PDF req_params['Submit'] = 'Send to New Media Services' # Fields that are required (?) in order to submit form # Not sure about this but so far form submission doesn't result in a # requisition being generated. We speculate that the process needs to # see some or all of these fields set. So here we set them to the values # sent by an (almost-) empty form. required = [ # Customer Information 'contactname', 'customer', 'phone', 'paytype', # Job Information 'jobtitle', 'date', 'enduse', ] missing_fields = [] for field_name in required: if field_name not in req_params: missing_fields.append (field_name) if missing_fields: raise ValueError ('Required fields %s missing from job %d' % (', '.join (missing_fields), job_id)) defaults = [ # Customer Information 'custno', 'address', 'fax', # Job Information ('esub', 'none'), 'filename', ('shiptype', 'none'), 'shipaddress', # Copying/Printing 'pages', 'copies', ('sides', 'single'), 'parts', 'otherstock', 'otherpapersize', 'otherpapercolour', 'otherinkcolour', # Binding/Finishing ('bindingtype', 'none'), ('staplingtype', 'none'), ('foldingtype', 'none'), 'padqty', 'cuttingsize', # Digital Archiving 'archother', # CD/DVD Duplicating ('cdtype', 'none'), 'othercdtype', ('cdimprint', 'no'), ('cdcase', 'envelope'), # Special Instructions 'instructions', ] for d in defaults: if type (d) is tuple: field_name, field_value = d else: field_name, field_value = d, '' if field_name not in req_params: req_params[field_name] = field_value query = urlencode (req_params, True) request = urllib.request.Request (request_url) request.data = (query.encode ()) request.add_header ("Referer", request_url) response = urllib.request.urlopen (request) save_file_dir.mkdir (parents=True, exist_ok=True) outfile = (save_file_dir / (save_file_pattern % job_id)).open ("w") outfile.write ('URL: %s\n' % response.geturl ()) outfile.write ('Status: %s\n' % response.getcode ()) outfile.write ('-' * 80 + '\n') outfile.write ('Headers:\n') for item in response.info ().items (): outfile.write ('%s: %s\n' % item) outfile.write ('-' * 80 + '\n') outfile.write ('Contents:\n') for line in response: outfile.write (line.decode ()) outfile.write ('-' * 80 + '\n')
[docs]def main (): cursor = open_psycopg2_db_service_cursor () job_ids = cursor.execute_values ("select job_id from nms_requisition where (job_ready is null) < (job_submitted is null)") for job_id in job_ids: cursor.callproc_none ("nms_requisition_submitted", job_id) cursor.connection.commit () submit_requisition (cursor, job_id)