195
195
return self.context
198
class SemesterUniquenessValidator(formencode.FancyValidator):
199
"""A FormEncode validator that checks that a semester is unique.
201
There cannot be more than one semester for the same year and semester.
203
def _to_python(self, value, state):
204
if (state.store.find(
205
Semester, year=value['year'], semester=value['semester']
207
raise formencode.Invalid(
208
'Semester already exists', value, state)
212
class SemesterSchema(formencode.Schema):
213
year = formencode.validators.UnicodeString()
214
semester = formencode.validators.UnicodeString()
215
chained_validators = [SemesterUniquenessValidator()]
218
class SemesterNew(BaseFormView):
219
"""A form to create a semester."""
220
template = 'templates/semester-new.html'
223
def authorize(self, req):
224
return req.user is not None and req.user.admin
228
return SemesterSchema()
230
def get_default_data(self, req):
233
def save_object(self, req, data):
234
new_semester = Semester()
235
new_semester.year = data['year']
236
new_semester.semester = data['semester']
238
req.store.add(new_semester)
241
def get_return_url(self, obj):
198
245
class OfferingView(XHTMLView):
199
246
"""The home page of an offering."""
200
247
template = 'templates/offering.html'
550
597
views = [(ApplicationRoot, ('subjects', '+index'), SubjectsView),
551
598
(ApplicationRoot, ('subjects', '+new'), SubjectNew),
552
599
(ApplicationRoot, ('subjects', '+new-offering'), OfferingNew),
600
(ApplicationRoot, ('subjects', '+new-semester'), SemesterNew),
553
601
(Subject, '+edit', SubjectEdit),
554
602
(Offering, '+index', OfferingView),
555
603
(Offering, '+edit', OfferingEdit),