Source code for uw.local.teaching.webui.ta.position

"""Position management UI implementation.

This module implements the Web user interface for managing available job
positions.
"""

from operator import attrgetter
from itertools import groupby

from ll.xist.ns import html

from uw.web.html.form import render_select, render_hidden
from uw.web.html.format import make_table_format, format_return

from uw.web.wsgi.delegate import delegate_get_post
from uw.web.wsgi.form import use_form_param
from uw.web.wsgi.function import return_html
from uw.web.wsgi.status import HTTPFound, HTTPForbidden

[docs]def format_positions (cursor, prefix, admins, roles): result = html.table ( html.colgroup ( html.col (style="width: 13em;"), html.col (style="width: 15em;"), html.col (style="width: 5em;"), html.col (style="width: 10em;"), ), html.tr ( html.th ('Course'), html.th ('Job'), html.th ('Effective'), html.th ('Actions') if 'TAMAN' in roles else None, ) ) for admin_id, positions in admins: positions = list (positions) jobs = [html.tr ( html.td (p.job_description), html.td (p.position_effective), ) for p in positions] p = positions[0] jobs[0].insert (0, html.td (p.admin_description, rowspan=len (jobs))) if 'TAMAN' in roles: jobs[0].append (html.td (html.a ('Edit…', href=prefix + "%s/" % admin_id), rowspan=len (jobs))) result.append (jobs) return result
@use_form_param @return_html def position_index_get_handler (cursor, unit, form, term, roles): if "add-admin" in form: raise HTTPFound ("%s/" % form.required_field_value ("add-admin")) result = [format_return ('Term')] result.append (html.p ('This page allows you to activate and deactivate jobs, which will have the effect of making them appear and disappear from the term job list when the corresponding courses are being offered. Do not deactivate a job unless it is going away for the foreseeable future; if it is just not being filled this term set the planned units for the job to 0.')) positions = groupby (cursor.execute_tuples ("select unit_code, admin_id, job_code, position_effective::text, term_id, position_active, job_description, admin_description from ta_job join (select * from ta_position_term_activation where (unit_code, term_id) = (%(unit_code)s, %(term_id)s)) tpta using (job_code) join teaching_admin using (admin_id) where position_active order by admin_description, job_description", unit_code=unit.unit_code, term_id=term.code ()), attrgetter ('admin_id')) result.append (html.h2 ('Active Positions')) result.append (format_positions (cursor, "", positions, roles)) if 'TAMAN' in roles: result.append (html.h2 ('All Active Courses')) result.append (html.form ( html.p ( 'Activate positions for: ', render_select ("add-admin", cursor.execute_tuples ("select admin_id, admin_description from teaching_admin_term natural join teaching_admin where term_id=%(term_id)s order by admin_description", term_id=term.code ())), ' ', html.input (type="submit", value="Edit Positions…"), ), method="get", action="" )) return 'Activate/Deactivate Job Positions', result @use_form_param @return_html def position_index_post_handler (cursor, form, term, roles): pass position_index_handler = delegate_get_post (position_index_get_handler, position_index_post_handler) @return_html def position_edit_get_handler (cursor, unit, term, admin, roles): result = [format_return ('Term', 'Edit Positions')] positions = cursor.execute_tuples ("select job_code, job_description, position_effective::text, position_active from ta_job left join (select * from ta_position_term_activation where (unit_code, term_id, admin_id) = (%(unit_code)s, %(term_id)s, %(admin_id)s)) tp using (job_code) where job_effective @> 'now'::text::date order by job_description", term_id=term.code (), unit_code=unit.unit_code, admin_id=admin.admin_id) table_columns = ( ('Job', attrgetter ('job_description')), ('Effective', attrgetter ('position_effective')), ('Active', attrgetter ('position_active')), ('Actions', lambda r: html.form (render_hidden ("job", r.job_code), html.input (type="submit", name="!drop", value="Deactivate!") if r.position_active else html.input (type="submit", name="!add", value="Activate!"), method="post", action="")), ) result.append (make_table_format (*table_columns) (positions)) return 'Activate/Deactivate Positions for %s' % admin.admin_description, result @use_form_param @return_html def position_edit_post_handler (cursor, form, unit, term, admin, roles): if not 'TAMAN' in roles: raise HTTPForbidden () job_code = form.optional_field_value ("job") if "!add" in form: cursor.callproc_none ("ta_activate_position", unit.unit_code, admin.admin_id, job_code, term.code ()) elif "!drop" in form: cursor.callproc_none ("ta_deactivate_position", unit.unit_code, admin.admin_id, job_code, term.code ()) raise HTTPFound ("") position_edit_handler = delegate_get_post (position_edit_get_handler, position_edit_post_handler)