Source code for uw.local.teaching.webui.admin_account_edit

"""Course account maintenance.

Web UI for maintaining course account information.
"""

from ll.xist.ns import html

from uw.web.html.format import format_return

from uw.web.wsgi import status
from uw.web.wsgi.form import use_form_param
from uw.web.wsgi.function import return_html

[docs]def render_admin_account (cursor, admin, editlink): """Determine current course account and format as HTML. :param cursor: DB connection cursor :param admin: In-context admin unit :param editlink: HTML for link to editing form, or None :return: 2-tuple of an HTML paragraph showing the current course account together with the course account userid :rtype: (HTML paragraph <p>, str) """ userid = cursor.execute_optional_value ("select username from teaching_admin_username_current where admin_id = %(admin_id)s", admin_id=admin.admin_id) if userid is None: result = (html.p ('No course account is assigned.', editlink)) else: result = (html.p ('The course account is ', html.tt (userid), '.', editlink)) return result, userid
[docs]def render_admin_account_section (cursor, admin, roles): """Render display of course account information. :param cursor: DB connection cursor :param admin: In-context admin unit :param roles: the set of roles possessed by the user :return: HTML for course account section of admin unit display :rtype: list """ result = [html.h2 ('Course Account Authorization')] if 'ISC' in roles: editlink = [' ', html.a ('Update…', href="account")] else: editlink = None render, _ = render_admin_account (cursor, admin, editlink) result.append (render) return result
@return_html def account_get_handler (cursor, admin, roles): """Course account update GET URL handler. :param cursor: DB connection cursor :param admin: In-context admin unit :param roles: the set of roles possessed by the user :return: tuple of (current course account information, provides form to update it) :rtype: (str, list) """ if not 'ISC' in roles: raise status.HTTPForbidden () result = [format_return ('Main Menu', 'Admin Units by UW Unit', None, dot="Admin Unit")] render, userid = render_admin_account (cursor, admin, None) result.append (render) if userid is None: form = [html.p ( 'Authorize the course account with username ', html.input (name="userid", type="text", maxlength=8, size=9), ' to have access to this admin unit. ', html.input (type="submit", name="!update", value="Authorize!"), )] else: form = [html.p ( 'If ', html.tt (userid), ' should no longer have access, then ', html.input (type="submit", name="!update", value="Revoke!"), )] result.append (html.form (form, method="post", action="")) return '%s: Edit Course Account' % admin.admin_description, result @use_form_param @return_html def account_post_handler (cursor, admin, roles, form): """Course account update POST URL handler. :param cursor: DB connection cursor :param admin: In-context admin unit :param roles: the set of roles possessed by the user :param form: CGI form results Handles form submissions for editing the course account. """ if not 'ISC' in roles: raise status.HTTPForbidden () if "!update" in form: userid = form.optional_field_value ("userid") if userid is not None: userid = userid.strip () inuse_admin = cursor.execute_optional_tuple ("select admin_id, admin_description from teaching_admin_username_current natural join teaching_admin where username = %(userid)s", userid=userid) if inuse_admin is not None: return 'Error — Course account in use', html.p ( 'The proposed course account ', html.tt (userid), ' is in use by ', inuse_admin.admin_description, ' (', inuse_admin.admin_id, ').' ) valid = cursor.execute_optional_value ("select (uw_id, hr_id) is null from watiam_entry where userid = %(userid)s", userid=userid) if valid is None: return 'Error — No such user', html.p ( 'The proposed course account ', html.tt (userid), ' does not exist in WatIAM.' ) elif not valid: return 'Error — Personal account', html.p ( 'The proposed course account ', html.tt (userid), ' appears to be the account of a person.' ) cursor.callproc_none ("teaching_admin_set_username", admin.admin_id, userid) raise status.HTTPFound ("")