Source code for uw.local.teaching.webui.personnel
'''Personnel display.
Web UI pages for working with personnel for an offering.
'''
from itertools import groupby
from operator import attrgetter
from ll.xist.ns import html
from uw.web.html.form import render_select, render_checkbox
from uw.web.html.format import format_return, format_tabs
from uw.web.wsgi import status
from uw.web.wsgi.delegate import delegate_file_only, delegate_get_post
from uw.web.wsgi.form import use_form_param
from uw.web.wsgi.function import return_html
from uw.local.util.format import person_search_form_url
from .authority import format_personnel_by_user, format_personnel_by_role
from .ta.eval import render_ta_index
[docs]def format_personnel (cursor, term, admin, personnel, roles):
"""Render HTML-formatted personnel page content for an offering.
:param cursor: DB connection cursor.
:param term: Object representing a UW term.
:param admin: DB row representing an admin unit.
:param personnel: DB rows representing personnel with permissions granted.
:param roles: List of user roles for a particular admin unit.
:return: HTML-formatted tabs displaying personnel (by names and roles)
and TA support if term is not None.
"""
result = []
if roles:
result.append (html.p ('Your roles: ', ', '.join (roles)))
else:
result.append (html.p ('You are not authorized for this admin unit.'))
is_admin = 'ADMIN' in roles
is_isc = 'ISC' in roles
remove_admin_id = admin.admin_id if term is None else None
people_by_name = format_personnel_by_user (cursor, personnel, is_admin, is_isc, remove_admin_id)
people_by_role = format_personnel_by_role (cursor, personnel, is_admin, is_isc, remove_admin_id)
if is_admin or is_isc:
personnel_options = sorted (k for k, g in groupby (personnel, attrgetter ('surname', 'givennames', 'userid', 'person_id')))
def available_roles (is_admin):
if is_admin:
return cursor.admin_get_active_roles ()
else:
return [('ISA', 'Instructional Support Assistant')]
def append_admin_options (personnel_table):
personnel_table = [
html.form (
personnel_table,
html.p ('Remove selected role authorizations: ',
html.input (type="submit", name="!remove", value="Remove!")
),
method="post", action=""
),
html.form (
html.h3 ('Add Authorized User'),
html.table (
html.tr (html.th ('Userid:'),
html.td (
render_select ("person_id", [(r[3], '%s, %s (%s)' % r[0:3]) for r in personnel_options] + [('other', 'Other…')], class_="uw-ofs"),
' ',
html.input (name="userid", type="text", maxlength=8, size=9, class_="uw-ofs-person_id-other"),
),
),
html.tr (html.th ('Backup:'), html.td (render_checkbox ("backup"), ' (have access but not use regularly)')),
html.tr (html.th ('Role:'), html.td (render_select ("role", available_roles(is_admin)))),
html.tr (html.td (html.input (type="submit", name="!authorize", value="Authorize!"), colspan=2)),
),
method="post", action=""
)
]
return personnel_table
people_by_name = append_admin_options (people_by_name)
people_by_role = append_admin_options (people_by_role)
tab_lst = [
('People by Name', 'by-person', people_by_name),
('People by Role', 'by-role', people_by_role)
]
if term is not None:
tab_lst.append (('TA Support', 'ta-support',
render_ta_index (cursor, term, admin, roles, 'ta/eval/') or 'There are no TA assignments scheduled.'))
result.append (format_tabs (tab_lst))
return result
@return_html
def personnel_get_handler (cursor, term, admin, roles):
result = [format_return ('Main Menu', None, None, dot='Offering')]
personnel = cursor.personnel_by_offering (term_id=term.code (), admin_id=admin.admin_id)
result.append (format_personnel (cursor, term, admin, personnel, roles))
return "%s (%s) Personnel" % (admin.admin_description, term.description ()), result
@use_form_param
@return_html
def personnel_post_handler (cursor, term, admin, roles, form):
if not {'ADMIN', 'ISC'} & roles:
raise status.HTTPForbidden ()
if "!remove" in form:
for role in form.multiple_field_value ("remove"):
person_id, role_code = role.split ('-')
if not ('ADMIN' in roles or role_code == 'ISA'):
raise status.HTTPForbidden ()
cursor.callproc_none ("auth_offering_manual_authorize", False, term.code (), admin.admin_id, person_id, role_code)
elif "!authorize" in form:
person_id = form.optional_field_value ("person_id")
if person_id == "other":
userid = form.required_field_value ("userid")
person_id = cursor.execute_optional_value ("select person_id from person_identity_complete where userid=%(userid)s", userid=userid)
if person_id is None:
return 'Error: No such person', html.p (
'No person with userid “', userid, '” found. ',
html.a ('WatIAM Search', target="_blank", href=person_search_form_url),
' may be helpful in finding the right person.',
)
role_code = form.required_field_value ("role")
if not role_code:
return 'Error: No role selected', [
html.p ('Please go back and select a role to grant to the selected person.')]
backup = "backup" in form
if not ('ADMIN' in roles or role_code == 'ISA'):
raise status.HTTPForbidden ()
cursor.callproc_none ("auth_offering_manual_authorize", True, term.code (), admin.admin_id, person_id, role_code, backup)
raise status.HTTPFound ("")
personnel_handler = delegate_file_only (delegate_get_post (personnel_get_handler, personnel_post_handler))