29
from storm.locals import Desc
30
from genshi.filters import HTMLFormFiller
29
33
from ivle.webapp.base.xhtml import XHTMLView
30
34
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
31
35
from ivle.webapp.errors import NotFound
32
from ivle.database import Subject
36
from ivle.database import Subject, Semester, Offering, Enrolment, User
33
37
from ivle import util
42
46
return req.user is not None
44
48
def populate(self, req, ctx):
45
ctx['enrolments'] = req.user.active_enrolments
49
ctx['user'] = req.user
51
for semester in req.store.find(Semester).order_by(Desc(Semester.year),
52
Desc(Semester.semester)):
53
enrolments = semester.enrolments.find(user=req.user)
54
if enrolments.count():
55
ctx['semesters'].append((semester, enrolments))
58
class UserValidator(formencode.FancyValidator):
59
"""A FormEncode validator that turns a username into a user.
61
The state must have a 'store' attribute, which is the Storm store
63
def _to_python(self, value, state):
64
user = User.get_by_login(state.store, value)
68
raise formencode.Invalid('User does not exist', value, state)
71
class NoEnrolmentValidator(formencode.FancyValidator):
72
"""A FormEncode validator that ensures absence of an enrolment.
74
The state must have an 'offering' attribute.
76
def _to_python(self, value, state):
77
if state.offering.get_enrolment(value):
78
raise formencode.Invalid('User already enrolled', value, state)
82
class EnrolSchema(formencode.Schema):
83
user = formencode.All(NoEnrolmentValidator(), UserValidator())
86
class EnrolView(XHTMLView):
87
"""A form to enrol a user in an offering."""
88
template = 'enrol.html'
92
def __init__(self, req, subject, year, semester):
93
"""Find the given offering by subject, year and semester."""
94
self.context = req.store.find(Offering,
95
Offering.subject_id == Subject.id,
96
Subject.short_name == subject,
97
Offering.semester_id == Semester.id,
98
Semester.year == year,
99
Semester.semester == semester).one()
104
def filter(self, stream, ctx):
105
return stream | HTMLFormFiller(data=ctx['data'])
107
def populate(self, req, ctx):
108
if req.method == 'POST':
109
data = dict(req.get_fieldstorage())
111
validator = EnrolSchema()
112
req.offering = self.context # XXX: Getting into state.
113
data = validator.to_python(data, state=req)
114
self.context.enrol(data['user'])
116
req.throw_redirect(req.uri)
117
except formencode.Invalid, e:
118
errors = e.unpack_errors()
123
ctx['data'] = data or {}
124
ctx['offering'] = self.context
125
ctx['errors'] = errors
47
128
class Plugin(ViewPlugin, MediaPlugin):
49
130
('subjects/', SubjectsView),
131
('subjects/:subject/:year/:semester/+enrolments/+new', EnrolView),