~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/admin/subject.py

  • Committer: William Grant
  • Date: 2010-02-12 03:52:59 UTC
  • Revision ID: grantw@unimelb.edu.au-20100212035259-ecqjo7tl53xenva3
Semester creation UI.

Show diffs side-by-side

added added

removed removed

Lines of Context:
195
195
        return self.context
196
196
 
197
197
 
 
198
class SemesterUniquenessValidator(formencode.FancyValidator):
 
199
    """A FormEncode validator that checks that a semester is unique.
 
200
 
 
201
    There cannot be more than one semester for the same year and semester.
 
202
    """
 
203
    def _to_python(self, value, state):
 
204
        if (state.store.find(
 
205
                Semester, year=value['year'], semester=value['semester']
 
206
                ).count() > 0):
 
207
            raise formencode.Invalid(
 
208
                'Semester already exists', value, state)
 
209
        return value
 
210
 
 
211
 
 
212
class SemesterSchema(formencode.Schema):
 
213
    year = formencode.validators.UnicodeString()
 
214
    semester = formencode.validators.UnicodeString()
 
215
    chained_validators = [SemesterUniquenessValidator()]
 
216
 
 
217
 
 
218
class SemesterNew(BaseFormView):
 
219
    """A form to create a semester."""
 
220
    template = 'templates/semester-new.html'
 
221
    tab = 'subjects'
 
222
 
 
223
    def authorize(self, req):
 
224
        return req.user is not None and req.user.admin
 
225
 
 
226
    @property
 
227
    def validator(self):
 
228
        return SemesterSchema()
 
229
 
 
230
    def get_default_data(self, req):
 
231
        return {}
 
232
 
 
233
    def save_object(self, req, data):
 
234
        new_semester = Semester()
 
235
        new_semester.year = data['year']
 
236
        new_semester.semester = data['semester']
 
237
 
 
238
        req.store.add(new_semester)
 
239
        return new_semester
 
240
 
 
241
    def get_return_url(self, obj):
 
242
        return '/subjects'
 
243
 
 
244
 
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),