Source code for uw.local.grad.webui.util
"""Miscellaneous utility routines.
These are routines used in various places within Graduate Admissions that are
not general enough to warrant a place in the general library.
"""
from datetime import datetime
import re
from ll.xist.ns import html
from uw.web.html.format import format_return
from uw.web.html.join import html_join
from uw.emailutils import emailaddr
from uw.local import termtools
address_fields = ('address1', 'address2', 'address3', 'address4',
'city', 'prov', 'postal', 'country', 'phone')
address_re = re.compile (r'\(' + ','.join
([r'([^,]*|\"[^"]*\")'] * len (address_fields)) + r'\)')
[docs]def print_address (address):
"""Format a parsed (parse_address) address into HTML.
:param namedtuple address: An address represented as a namedtuple of field
values.
:return: The address formatted as HTML.
"""
result = []
for field in ('address1', 'address2', 'address3', 'address4'):
value = getattr (address, field)
if value:
result.append (value)
result.append (html.br ())
result.append (html_join ((address.city, address.prov, address.postal), sep=' '))
result.append (html.br ())
result.append (address.country)
return result
[docs]def get_email_address (cursor, person_id):
"""Look up a person's email address, given their Person ID.
:param cursor: DB connection cursor.
:param person_id: the identity of the person.
:return: the email address in the form userid@uwaterloo.ca.
"""
# TODO: Shouldn't this go to userid@uwaterloo.ca?
# But not if no email address is set!
result = cursor.execute_optional_tuple ("SELECT * from person_identity_complete where person_id = %(person_id)s", person_id=person_id)
if result is None:
return None
return emailaddr (result.givennames + ' ' + result.surname, result.userid + '@uwaterloo.ca')