29
from storm.locals import Desc
31
29
from ivle.webapp.base.xhtml import XHTMLView
32
30
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
33
31
from ivle.webapp.errors import NotFound
34
from ivle.database import Subject, Semester
32
from ivle.database import Subject
35
33
from ivle import util
38
36
class SubjectsView(XHTMLView):
39
37
'''The view of the list of subjects.'''
40
38
template = 'subjects.html'
39
appname = 'subjects' # XXX
43
41
def authorize(self, req):
44
42
return req.user is not None
46
44
def populate(self, req, ctx):
48
for semester in req.store.find(Semester).order_by(Desc(Semester.year),
49
Desc(Semester.semester)):
50
enrolments = semester.enrolments.find(user=req.user)
51
if enrolments.count():
52
ctx['semesters'].append((semester, enrolments))
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)
54
67
class Plugin(ViewPlugin, MediaPlugin):
60
('subjects', 'Subjects',
61
'View subject content and complete worksheets',
62
'subjects.png', 'subjects', 5)
73
('subjects', 'Subjects', 'Announcements and information about the '
74
'subjects you are enrolled in.', 'subjects.png', 'subjects', 5)
65
77
media = 'subject-media'