29
from common import util
33
"""Handler for the Subjects application. Links to subject home pages."""
35
req.styles = ["media/subjects/subjects.css"]
37
handle_toplevel_menu(req)
39
handle_subject_page(req, req.path)
41
def handle_toplevel_menu(req):
42
# This is represented as a directory. Redirect and add a slash if it is
44
if req.uri[-1] != '/':
45
req.throw_redirect(req.uri + '/')
47
# Get list of subjects
50
enrolled_subjects = db.get_enrolment(req.user.login)
51
all_subjects = db.get_subjects()
55
enrolled_set = set(x['subj_code'] for x in enrolled_subjects)
56
unenrolled_subjects = [x for x in all_subjects
57
if x['subj_code'] not in enrolled_set]
58
enrolled_subjects.sort(key=lambda x: x['subj_code'])
59
unenrolled_subjects.sort(key=lambda x: x['subj_code'])
61
def print_subject(subject):
62
if subject['url'] is None:
63
req.write(' <li>%s (no home page)</li>\n'
64
% cgi.escape(subject['subj_name']))
66
req.write(' <li><a href="%s">%s</a></li>\n'
67
% (cgi.escape(subject['url']),
68
cgi.escape(subject['subj_name'])))
70
req.content_type = "text/html"
71
req.write_html_head_foot = True
72
req.write('<div id="ivle_padding">\n')
73
req.write("<h2>IVLE Subject Homepages</h2>\n")
74
req.write("<h2>Subjects</h2>\n<ul>\n")
75
for subject in enrolled_subjects:
76
print_subject(subject)
78
if len(unenrolled_subjects) > 0:
79
req.write("<h3>Other Subjects</h3>\n")
80
req.write("<p>You are not currently enrolled in these subjects</p>\n")
82
for subject in unenrolled_subjects:
83
print_subject(subject)
87
def handle_subject_page(req, path):
88
req.content_type = "text/html"
89
req.write_html_head_foot = True # Have dispatch print head and foot
91
# Just make the iframe pointing to media/subjects
92
serve_loc = util.make_path(os.path.join('media', 'subjects', path))
93
req.write('<object class="fullscreen" type="text/html" \
94
data="%s"></iframe>'% urllib.quote(serve_loc))
29
from ivle.webapp.base.xhtml import XHTMLView
30
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
31
from ivle.webapp.errors import NotFound
32
from ivle.database import Subject
36
class SubjectsView(XHTMLView):
37
'''The view of the list of subjects.'''
38
template = 'subjects.html'
39
appname = 'subjects' # XXX
41
def authorize(self, req):
42
return req.user is not None
44
def populate(self, req, ctx):
45
enrolled_subjects = req.user.subjects
46
unenrolled_subjects = [subject for subject in
47
req.store.find(Subject)
48
if subject not in enrolled_subjects]
50
ctx['enrolled_subjects'] = []
51
ctx['other_subjects'] = []
53
for subject in enrolled_subjects:
55
new_subj['name'] = subject.name
56
new_subj['url'] = subject.url
57
ctx['enrolled_subjects'].append(new_subj)
59
if len(unenrolled_subjects) > 0:
60
for subject in unenrolled_subjects:
62
new_subj['name'] = subject.name
63
new_subj['url'] = subject.url
64
ctx['other_subjects'].append(new_subj)
67
class Plugin(ViewPlugin, MediaPlugin):
69
('subjects/', SubjectsView),
73
('subjects', 'Subjects', 'Announcements and information about the '
74
'subjects you are enrolled in.', 'subjects.png', 'subjects', 5)
77
media = 'subject-media'