~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-25 03:18:21 UTC
  • Revision ID: grantw@unimelb.edu.au-20100225031821-mi9a2tm5679fht4d
Shuffle things around so that req.user and req.store only construct when actually retrieved, and ensure they're not retrieved for media files. Saves 50ms of DB connection time per request.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
import formencode
36
36
import formencode.validators
37
37
 
38
 
from ivle.webapp.base.forms import BaseFormView
 
38
from ivle.webapp.base.forms import BaseFormView, URLNameValidator
39
39
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
40
40
from ivle.webapp.base.xhtml import XHTMLView
41
41
from ivle.webapp.errors import BadRequest
64
64
    '''The view of the list of subjects.'''
65
65
    template = 'templates/subjects.html'
66
66
    tab = 'subjects'
 
67
    breadcrumb_text = "Subjects"
67
68
 
68
69
    def authorize(self, req):
69
70
        return req.user is not None
96
97
    def populate(self, req, ctx):
97
98
        ctx['req'] = req
98
99
        ctx['mediapath'] = media_url(req, CorePlugin, 'images/')
 
100
        ctx['SubjectView'] = SubjectView
99
101
        ctx['SubjectEdit'] = SubjectEdit
100
102
        ctx['SemesterEdit'] = SemesterEdit
101
103
 
125
127
class SubjectSchema(formencode.Schema):
126
128
    short_name = formencode.All(
127
129
        SubjectShortNameUniquenessValidator(),
128
 
        formencode.validators.UnicodeString(not_empty=True))
 
130
        URLNameValidator(not_empty=True))
129
131
    name = formencode.validators.UnicodeString(not_empty=True)
130
132
    code = formencode.validators.UnicodeString(not_empty=True)
131
133
 
144
146
    def validator(self):
145
147
        return SubjectSchema()
146
148
 
147
 
    def get_return_url(self, obj):
148
 
        return '/subjects'
149
 
 
150
149
 
151
150
class SubjectNew(SubjectFormView):
152
151
    """A form to create a subject."""
202
201
 
203
202
 
204
203
class SemesterSchema(formencode.Schema):
205
 
    year = formencode.validators.UnicodeString()
206
 
    semester = formencode.validators.UnicodeString()
 
204
    year = URLNameValidator()
 
205
    semester = URLNameValidator()
207
206
    state = formencode.All(
208
207
        formencode.validators.OneOf(["past", "current", "future"]),
209
208
        formencode.validators.UnicodeString())
266
265
 
267
266
        return self.context
268
267
 
 
268
class SubjectView(XHTMLView):
 
269
    '''The view of the list of offerings in a given subject.'''
 
270
    template = 'templates/subject.html'
 
271
    tab = 'subjects'
 
272
 
 
273
    def authorize(self, req):
 
274
        return req.user is not None
 
275
 
 
276
    def populate(self, req, ctx):
 
277
        ctx['context'] = self.context
 
278
        ctx['req'] = req
 
279
        ctx['user'] = req.user
 
280
        ctx['offerings'] = list(self.context.offerings)
 
281
        ctx['permissions'] = self.context.get_permissions(req.user,req.config)
 
282
        ctx['SubjectEdit'] = SubjectEdit
 
283
        ctx['SubjectOfferingNew'] = SubjectOfferingNew
 
284
 
269
285
 
270
286
class OfferingView(XHTMLView):
271
287
    """The home page of an offering."""
292
308
 
293
309
        ctx['worksheets'], problems_total, problems_done = (
294
310
            ivle.worksheet.utils.create_list_of_fake_worksheets_and_stats(
295
 
                req.store, req.user, self.context))
 
311
                req.config, req.store, req.user, self.context))
296
312
 
297
313
        ctx['exercises_total'] = problems_total
298
314
        ctx['exercises_done'] = problems_done
369
385
    description = formencode.validators.UnicodeString(
370
386
        if_missing=None, not_empty=False)
371
387
    url = formencode.validators.URL(if_missing=None, not_empty=False)
 
388
    show_worksheet_marks = formencode.validators.StringBoolean(
 
389
        if_missing=False)
372
390
 
373
391
 
374
392
class OfferingAdminSchema(OfferingSchema):
397
415
        ctx['subjects'] = req.store.find(Subject).order_by(Subject.name)
398
416
        ctx['semesters'] = req.store.find(Semester).order_by(
399
417
            Semester.year, Semester.semester)
 
418
        ctx['force_subject'] = None
400
419
 
401
420
    def populate_state(self, state):
402
421
        state.existing_offering = self.context
408
427
                        self.context.semester.semester,
409
428
            'url': self.context.url,
410
429
            'description': self.context.description,
 
430
            'show_worksheet_marks': self.context.show_worksheet_marks,
411
431
            }
412
432
 
413
433
    def save_object(self, req, data):
416
436
            self.context.semester = data['semester']
417
437
        self.context.description = data['description']
418
438
        self.context.url = unicode(data['url']) if data['url'] else None
 
439
        self.context.show_worksheet_marks = data['show_worksheet_marks']
419
440
        return self.context
420
441
 
421
442
 
436
457
        ctx['subjects'] = req.store.find(Subject).order_by(Subject.name)
437
458
        ctx['semesters'] = req.store.find(Semester).order_by(
438
459
            Semester.year, Semester.semester)
 
460
        ctx['force_subject'] = None
439
461
 
440
462
    def populate_state(self, state):
441
463
        state.existing_offering = None
449
471
        new_offering.semester = data['semester']
450
472
        new_offering.description = data['description']
451
473
        new_offering.url = unicode(data['url']) if data['url'] else None
 
474
        new_offering.show_worksheet_marks = data['show_worksheet_marks']
452
475
 
453
476
        req.store.add(new_offering)
454
477
        return new_offering
455
478
 
 
479
class SubjectOfferingNew(OfferingNew):
 
480
    """A form to create an offering for a given subject."""
 
481
    # Identical to OfferingNew, except it forces the subject to be the subject
 
482
    # in context
 
483
    def populate(self, req, ctx):
 
484
        super(SubjectOfferingNew, self).populate(req, ctx)
 
485
        ctx['force_subject'] = self.context
456
486
 
457
487
class OfferingCloneWorksheetsSchema(formencode.Schema):
458
488
    subject = formencode.All(
590
620
        ctx['offering'] = self.context
591
621
        ctx['roles_auth'] = self.context.get_permissions(req.user, req.config)
592
622
        ctx['errors'] = errors
 
623
        # If all of the fields validated, set the global form error.
 
624
        if isinstance(errors, basestring):
 
625
            ctx['error_value'] = errors
593
626
 
594
627
 
595
628
class EnrolmentEditSchema(formencode.Schema):
740
773
             (ApplicationRoot, ('subjects', '+new'), SubjectNew),
741
774
             (ApplicationRoot, ('subjects', '+new-offering'), OfferingNew),
742
775
             (ApplicationRoot, ('+semesters', '+new'), SemesterNew),
 
776
             (Subject, '+index', SubjectView),
743
777
             (Subject, '+edit', SubjectEdit),
 
778
             (Subject, '+new-offering', SubjectOfferingNew),
744
779
             (Semester, '+edit', SemesterEdit),
745
780
             (Offering, '+index', OfferingView),
746
781
             (Offering, '+edit', OfferingEdit),