Source code for uw.local.util.format
"""Convenient UW-specific formatting routines.
"""
import re
import urllib.request, urllib.parse, urllib.error
import urllib.parse
from email.utils import formataddr
from ll.xist.ns import html
[docs]def format_mailto_url (cursor, person_ids, subject=None):
"""Format a mailto: URL for some people and optionally a subject line.
:param cursor: DB connection cursor.
:param person_ids: an iterable of Identity person_id values of recipients.
:param str subject: the default subject for the email message.
"""
addresses = []
for person_id in person_ids:
details = cursor.execute_optional_tuple ("select * from person_identity_complete where person_id=%(person_id)s", person_id=person_id)
addresses.append (formataddr ((
("%s, %s" % (details.surname, details.givennames)),
"%s@uwaterloo.ca" % details.userid
)))
if subject is None:
query = None
else:
# According to the following, mailto: URLs do not allow +-encoding:
# http://shadow2531.com/opera/testcases/mailto/modern_mailto_uri_scheme.html
query = urllib.parse.urlencode ([('subject', subject.encode ('utf-8'))]).replace ('+', '%20')
return urllib.parse.urlunparse (['mailto', None, ','.join (addresses), None, query, None])
person_search_base_url = "https://idm.uwaterloo.ca/search"
person_search_form_url = person_search_base_url + "/authen"
[docs]def format_person (cursor, person_id):
"""Format a person's identity as HTML.
:param cursor: DB connection cursor.
:param int person_id: the person_id of the person.
Return HTML of the person's name, which is a link to their WatIAM lookup
page, followed by their userid in parentheses, which is a mailto: link for
the person.
"""
if person_id is None:
return None
details = cursor.execute_optional_tuple ("select * from person_identity_complete where person_id=%(person_id)s", person_id=person_id)
if details is None:
return '[%s]' % person_id
return [
html.a (
details.surname, ', ', details.givennames,
href="%s/results.jsp?uid=%s" % (person_search_base_url, details.userid)),
' (',
html.a (details.userid, href=format_mailto_url (cursor, [person_id])),
')'
]
[docs]def format_aff (aff):
"""Format an "Accounting Flex Field" account number.
:param str aff: the account number.
Formerly, inserted spaces appropriately to separate the various fields.
Now this is done by the database stored procedures so this procedure is
now the identity.
"""
return aff