Source code for uw.local.quest.webui.format
"""Convenient quest formatting routines.
"""
from ll.xist.ns import html
from uw.local import termtools
from uw.web.html.join import html_join
[docs]def format_instructor (instructor, base_url=""):
"""Format an instructor's identity as HTML.
:param instructor: the relevant instructor.
:param base_url: the base URL for the link to instructor page.
:return: the instructor's name linking to their summary page.
"""
return html.a (instructor.surname, ', ', instructor.givennames, href="%sinstructor/%s/" % (base_url, instructor.userid))
[docs]def format_instructor_list (instructor_list, base_url=""):
"""Format the instructor list on /quest/{user_id}/instructor.
:param instructor_list: a list of all the instructors.
:return: all the instructors names as paragraph elements linked to their summary page.
"""
return (html.p (format_instructor(inst, base_url)) for inst in instructor_list)
[docs]def format_subject (subject, base_url=""):
"""Format a subject as HTML.
:param subject: the relevant subject.
:param base_url: the base URL for the link to subject page.
:return: the subject code and subject short description linking to the subject list page.
"""
return html.p (html.a (subject.subject_code + ' — ' + subject.subject_short_description, href="%ssubject/%s/" % (base_url, subject.subject_code)))
[docs]def format_term (term, base_url=""):
"""Format a term as HTML.
:param term: the relevant term.
:param base_url: the base URL for the link to term page.
:return: the term description linking to the term list page.
"""
return html.p (html.a (termtools.fromCode(term).description (), href="%s%s/" % (base_url, term)))
[docs]def format_courses (cursor, courses, subject=None, term=None, base_url=''):
"""Formats course information to tables
:param cursor: DB connection cursor.
:param courses: list of relevant courses.
:param subject: the relevant subject.
:return: a table of the course information details.
"""
if courses is None:
return "No courses are available."
result = html.tr ([
html.th ('Course'),
html.th ('Units'),
html.th ('Title'),
html.th ('Description'),
html.th ('Requisites')
])
for course in courses:
description = html_join (course.course_description.replace (' [', '\n[').split ('\n'),
html.br ()) if course.course_description else None
requisite = cursor.requisite_by_id (requisite_id=course.requisite_id)
result.append (html.tr (
html.td (html.a (course.title, href="%scatalog/%s" % (base_url, course.catalog))),
html.td (str (course.course_take_units)),
html.td (course.course_title),
html.td (description),
html.td (requisite),
))
return html.table (result)
[docs]def format_pagination_links (cur_page, num_pages, query):
""" Formats links to be used for pagination
:param cur_page: the current page
:param num_pages: total number of pages
:param query: the query string
:return: a list of anchor tags.
"""
paginate_links = []
end = cur_page if cur_page < 4 else 4
for x in range (1, end):
paginate_links.append (html.a (x, href=f"{query}={x}"))
start = cur_page - 2 if cur_page - 2 > end else cur_page
if start > end + 1:
paginate_links.append ('…')
end = cur_page + 3 if cur_page + 3 < num_pages else num_pages
for x in range (start, end):
if x == cur_page:
paginate_links.append (html.b (x))
else:
paginate_links.append (html.a (x, href=f"{query}={x}"))
start = num_pages - 3 if num_pages - 3 > end else end + 1
if start > end + 1:
paginate_links.append ('…')
for x in range (start, num_pages):
paginate_links.append (html.a (x, href=f"{query}={x}"))
return html_join (paginate_links, ' ')