~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-15 09:16:49 UTC
  • Revision ID: grantw@unimelb.edu.au-20100215091649-qrp5emn7wjt2jlmj
Add 'Create new offering' link to /subjects, and rebrand the 'Manage subjects' link to include semesters too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import urllib
28
28
import urlparse
29
29
import cgi
30
 
import datetime
31
30
 
32
31
from storm.locals import Desc, Store
33
32
import genshi
34
33
from genshi.filters import HTMLFormFiller
35
 
from genshi.template import Context
 
34
from genshi.template import Context, TemplateLoader
36
35
import formencode
37
36
import formencode.validators
38
37
 
39
 
from ivle.webapp.base.forms import (BaseFormView, URLNameValidator,
40
 
                                    DateTimeValidator)
 
38
from ivle.webapp.base.forms import BaseFormView
41
39
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
42
40
from ivle.webapp.base.xhtml import XHTMLView
43
 
from ivle.webapp.base.text import TextView
44
 
from ivle.webapp.errors import BadRequest
45
41
from ivle.webapp import ApplicationRoot
46
42
 
47
43
from ivle.database import Subject, Semester, Offering, Enrolment, User,\
49
45
from ivle import util
50
46
import ivle.date
51
47
 
 
48
from ivle.webapp.admin.projectservice import ProjectSetRESTView
 
49
from ivle.webapp.admin.offeringservice import OfferingRESTView
52
50
from ivle.webapp.admin.publishing import (root_to_subject, root_to_semester,
53
51
            subject_to_offering, offering_to_projectset, offering_to_project,
54
 
            offering_to_enrolment, subject_url, semester_url, offering_url,
55
 
            projectset_url, project_url, enrolment_url)
 
52
            subject_url, semester_url, offering_url, projectset_url,
 
53
            project_url)
56
54
from ivle.webapp.admin.breadcrumbs import (SubjectBreadcrumb,
57
 
            OfferingBreadcrumb, UserBreadcrumb, ProjectBreadcrumb,
58
 
            ProjectsBreadcrumb, EnrolmentBreadcrumb)
 
55
            OfferingBreadcrumb, UserBreadcrumb, ProjectBreadcrumb)
59
56
from ivle.webapp.core import Plugin as CorePlugin
60
57
from ivle.webapp.groups import GroupsView
61
58
from ivle.webapp.media import media_url
65
62
    '''The view of the list of subjects.'''
66
63
    template = 'templates/subjects.html'
67
64
    tab = 'subjects'
68
 
    breadcrumb_text = "Subjects"
69
65
 
70
66
    def authorize(self, req):
71
67
        return req.user is not None
75
71
        ctx['user'] = req.user
76
72
        ctx['semesters'] = []
77
73
 
78
 
        for semester in req.store.find(Semester).order_by(
79
 
            Desc(Semester.year), Desc(Semester.display_name)):
 
74
        for semester in req.store.find(Semester).order_by(Desc(Semester.year),
 
75
                                                     Desc(Semester.semester)):
80
76
            if req.user.admin:
81
77
                # For admins, show all subjects in the system
82
78
                offerings = list(semester.offerings.find())
98
94
    def populate(self, req, ctx):
99
95
        ctx['req'] = req
100
96
        ctx['mediapath'] = media_url(req, CorePlugin, 'images/')
101
 
        ctx['SubjectView'] = SubjectView
102
97
        ctx['SubjectEdit'] = SubjectEdit
103
98
        ctx['SemesterEdit'] = SemesterEdit
104
99
 
105
100
        ctx['subjects'] = req.store.find(Subject).order_by(Subject.name)
106
101
        ctx['semesters'] = req.store.find(Semester).order_by(
107
 
            Semester.year, Semester.display_name)
108
 
 
109
 
 
110
 
class SubjectUniquenessValidator(formencode.FancyValidator):
111
 
    """A FormEncode validator that checks that a subject attribute is unique.
 
102
            Semester.year, Semester.semester)
 
103
 
 
104
 
 
105
class SubjectShortNameUniquenessValidator(formencode.FancyValidator):
 
106
    """A FormEncode validator that checks that a subject name is unused.
112
107
 
113
108
    The subject referenced by state.existing_subject is permitted
114
109
    to hold that name. If any other object holds it, the input is rejected.
115
 
 
116
 
    :param attribute: the name of the attribute to check.
117
 
    :param display: a string to identify the field in case of error.
118
110
    """
119
 
 
120
 
    def __init__(self, attribute, display):
121
 
        self.attribute = attribute
122
 
        self.display = display
 
111
    def __init__(self, matching=None):
 
112
        self.matching = matching
123
113
 
124
114
    def _to_python(self, value, state):
125
 
        if (state.store.find(Subject, **{self.attribute: value}).one() not in
 
115
        if (state.store.find(
 
116
                Subject, short_name=value).one() not in
126
117
                (None, state.existing_subject)):
127
118
            raise formencode.Invalid(
128
 
                '%s already taken' % self.display, value, state)
 
119
                'Short name already taken', value, state)
129
120
        return value
130
121
 
131
122
 
132
123
class SubjectSchema(formencode.Schema):
133
124
    short_name = formencode.All(
134
 
        SubjectUniquenessValidator('short_name', 'URL name'),
135
 
        URLNameValidator(not_empty=True))
136
 
    name = formencode.validators.UnicodeString(not_empty=True)
137
 
    code = formencode.All(
138
 
        SubjectUniquenessValidator('code', 'Subject code'),
 
125
        SubjectShortNameUniquenessValidator(),
139
126
        formencode.validators.UnicodeString(not_empty=True))
 
127
    name = formencode.validators.UnicodeString(not_empty=True)
 
128
    code = formencode.validators.UnicodeString(not_empty=True)
140
129
 
141
130
 
142
131
class SubjectFormView(BaseFormView):
153
142
    def validator(self):
154
143
        return SubjectSchema()
155
144
 
 
145
    def get_return_url(self, obj):
 
146
        return '/subjects'
 
147
 
156
148
 
157
149
class SubjectNew(SubjectFormView):
158
150
    """A form to create a subject."""
200
192
    """
201
193
    def _to_python(self, value, state):
202
194
        if (state.store.find(
203
 
                Semester, year=value['year'], url_name=value['url_name']
 
195
                Semester, year=value['year'], semester=value['semester']
204
196
                ).one() not in (None, state.existing_semester)):
205
197
            raise formencode.Invalid(
206
198
                'Semester already exists', value, state)
208
200
 
209
201
 
210
202
class SemesterSchema(formencode.Schema):
211
 
    year = URLNameValidator()
212
 
    code = formencode.validators.UnicodeString()
213
 
    url_name = URLNameValidator()
214
 
    display_name = formencode.validators.UnicodeString()
 
203
    year = formencode.validators.UnicodeString()
 
204
    semester = formencode.validators.UnicodeString()
215
205
    state = formencode.All(
216
206
        formencode.validators.OneOf(["past", "current", "future"]),
217
207
        formencode.validators.UnicodeString())
246
236
    def save_object(self, req, data):
247
237
        new_semester = Semester()
248
238
        new_semester.year = data['year']
249
 
        new_semester.code = data['code']
250
 
        new_semester.url_name = data['url_name']
251
 
        new_semester.display_name = data['display_name']
 
239
        new_semester.semester = data['semester']
252
240
        new_semester.state = data['state']
253
241
 
254
242
        req.store.add(new_semester)
265
253
    def get_default_data(self, req):
266
254
        return {
267
255
            'year': self.context.year,
268
 
            'code': self.context.code,
269
 
            'url_name': self.context.url_name,
270
 
            'display_name': self.context.display_name,
 
256
            'semester': self.context.semester,
271
257
            'state': self.context.state,
272
258
            }
273
259
 
274
260
    def save_object(self, req, data):
275
261
        self.context.year = data['year']
276
 
        self.context.code = data['code']
277
 
        self.context.url_name = data['url_name']
278
 
        self.context.display_name = data['display_name']
 
262
        self.context.semester = data['semester']
279
263
        self.context.state = data['state']
280
264
 
281
265
        return self.context
282
266
 
283
 
class SubjectView(XHTMLView):
284
 
    '''The view of the list of offerings in a given subject.'''
285
 
    template = 'templates/subject.html'
286
 
    tab = 'subjects'
287
 
 
288
 
    def authorize(self, req):
289
 
        return req.user is not None
290
 
 
291
 
    def populate(self, req, ctx):
292
 
        ctx['context'] = self.context
293
 
        ctx['req'] = req
294
 
        ctx['user'] = req.user
295
 
        ctx['offerings'] = list(self.context.offerings)
296
 
        ctx['permissions'] = self.context.get_permissions(req.user,req.config)
297
 
        ctx['SubjectEdit'] = SubjectEdit
298
 
        ctx['SubjectOfferingNew'] = SubjectOfferingNew
299
 
 
300
267
 
301
268
class OfferingView(XHTMLView):
302
269
    """The home page of an offering."""
314
281
        ctx['format_datetime'] = ivle.date.make_date_nice
315
282
        ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
316
283
        ctx['OfferingEdit'] = OfferingEdit
317
 
        ctx['OfferingCloneWorksheets'] = OfferingCloneWorksheets
318
284
        ctx['GroupsView'] = GroupsView
319
 
        ctx['EnrolmentsView'] = EnrolmentsView
320
 
        ctx['Project'] = ivle.database.Project
321
285
 
322
286
        # As we go, calculate the total score for this subject
323
287
        # (Assessable worksheets only, mandatory problems only)
324
288
 
325
289
        ctx['worksheets'], problems_total, problems_done = (
326
290
            ivle.worksheet.utils.create_list_of_fake_worksheets_and_stats(
327
 
                req.config, req.store, req.user, self.context,
328
 
                as_of=self.context.worksheet_cutoff))
 
291
                req.store, req.user, self.context))
329
292
 
330
293
        ctx['exercises_total'] = problems_total
331
294
        ctx['exercises_done'] = problems_done
372
335
            year = semester = None
373
336
 
374
337
        semester = state.store.find(
375
 
            Semester, year=year, url_name=semester).one()
 
338
            Semester, year=year, semester=semester).one()
376
339
        if semester:
377
340
            return semester
378
341
        else:
402
365
    description = formencode.validators.UnicodeString(
403
366
        if_missing=None, not_empty=False)
404
367
    url = formencode.validators.URL(if_missing=None, not_empty=False)
405
 
    worksheet_cutoff = DateTimeValidator(if_missing=None, not_empty=False)
406
 
    show_worksheet_marks = formencode.validators.StringBoolean(
407
 
        if_missing=False)
408
368
 
409
369
 
410
370
class OfferingAdminSchema(OfferingSchema):
432
392
        super(OfferingEdit, self).populate(req, ctx)
433
393
        ctx['subjects'] = req.store.find(Subject).order_by(Subject.name)
434
394
        ctx['semesters'] = req.store.find(Semester).order_by(
435
 
            Semester.year, Semester.display_name)
436
 
        ctx['force_subject'] = None
 
395
            Semester.year, Semester.semester)
437
396
 
438
397
    def populate_state(self, state):
439
398
        state.existing_offering = self.context
442
401
        return {
443
402
            'subject': self.context.subject.short_name,
444
403
            'semester': self.context.semester.year + '/' +
445
 
                        self.context.semester.url_name,
 
404
                        self.context.semester.semester,
446
405
            'url': self.context.url,
447
406
            'description': self.context.description,
448
 
            'worksheet_cutoff': self.context.worksheet_cutoff,
449
 
            'show_worksheet_marks': self.context.show_worksheet_marks,
450
407
            }
451
408
 
452
409
    def save_object(self, req, data):
455
412
            self.context.semester = data['semester']
456
413
        self.context.description = data['description']
457
414
        self.context.url = unicode(data['url']) if data['url'] else None
458
 
        self.context.worksheet_cutoff = data['worksheet_cutoff']
459
 
        self.context.show_worksheet_marks = data['show_worksheet_marks']
460
415
        return self.context
461
416
 
462
417
 
476
431
        super(OfferingNew, self).populate(req, ctx)
477
432
        ctx['subjects'] = req.store.find(Subject).order_by(Subject.name)
478
433
        ctx['semesters'] = req.store.find(Semester).order_by(
479
 
            Semester.year, Semester.display_name)
480
 
        ctx['force_subject'] = None
 
434
            Semester.year, Semester.semester)
481
435
 
482
436
    def populate_state(self, state):
483
437
        state.existing_offering = None
491
445
        new_offering.semester = data['semester']
492
446
        new_offering.description = data['description']
493
447
        new_offering.url = unicode(data['url']) if data['url'] else None
494
 
        new_offering.worksheet_cutoff = data['worksheet_cutoff']
495
 
        new_offering.show_worksheet_marks = data['show_worksheet_marks']
496
448
 
497
449
        req.store.add(new_offering)
498
450
        return new_offering
499
451
 
500
 
class SubjectOfferingNew(OfferingNew):
501
 
    """A form to create an offering for a given subject."""
502
 
    # Identical to OfferingNew, except it forces the subject to be the subject
503
 
    # in context
504
 
    def populate(self, req, ctx):
505
 
        super(SubjectOfferingNew, self).populate(req, ctx)
506
 
        ctx['force_subject'] = self.context
507
 
 
508
 
class OfferingCloneWorksheetsSchema(formencode.Schema):
509
 
    subject = formencode.All(
510
 
        SubjectValidator(), formencode.validators.UnicodeString())
511
 
    semester = formencode.All(
512
 
        SemesterValidator(), formencode.validators.UnicodeString())
513
 
 
514
 
 
515
 
class OfferingCloneWorksheets(BaseFormView):
516
 
    """A form to clone worksheets from one offering to another."""
517
 
    template = 'templates/offering-clone-worksheets.html'
518
 
    tab = 'subjects'
519
 
 
520
 
    def authorize(self, req):
521
 
        return req.user is not None and req.user.admin
522
 
 
523
 
    @property
524
 
    def validator(self):
525
 
        return OfferingCloneWorksheetsSchema()
526
 
 
527
 
    def populate(self, req, ctx):
528
 
        super(OfferingCloneWorksheets, self).populate(req, ctx)
529
 
        ctx['subjects'] = req.store.find(Subject).order_by(Subject.name)
530
 
        ctx['semesters'] = req.store.find(Semester).order_by(
531
 
            Semester.year, Semester.display_name)
532
 
 
533
 
    def get_default_data(self, req):
534
 
        return {}
535
 
 
536
 
    def save_object(self, req, data):
537
 
        if self.context.worksheets.count() > 0:
538
 
            raise BadRequest(
539
 
                "Cannot clone to target with existing worksheets.")
540
 
        offering = req.store.find(
541
 
            Offering, subject=data['subject'], semester=data['semester']).one()
542
 
        if offering is None:
543
 
            raise BadRequest("No such offering.")
544
 
        if offering.worksheets.count() == 0:
545
 
            raise BadRequest("Source offering has no worksheets.")
546
 
 
547
 
        self.context.clone_worksheets(offering)
548
 
        return self.context
549
 
 
550
452
 
551
453
class UserValidator(formencode.FancyValidator):
552
454
    """A FormEncode validator that turns a username into a user.
599
501
    template = 'templates/enrolments.html'
600
502
    tab = 'subjects'
601
503
    permission = 'edit'
602
 
    breadcrumb_text = 'Enrolments'
603
504
 
604
505
    def populate(self, req, ctx):
605
 
        ctx['req'] = req
606
506
        ctx['offering'] = self.context
607
 
        ctx['mediapath'] = media_url(req, CorePlugin, 'images/')
608
 
        ctx['offering_perms'] = self.context.get_permissions(
609
 
            req.user, req.config)
610
 
        ctx['EnrolView'] = EnrolView
611
 
        ctx['EnrolmentEdit'] = EnrolmentEdit
612
 
        ctx['EnrolmentDelete'] = EnrolmentDelete
613
 
 
614
507
 
615
508
class EnrolView(XHTMLView):
616
509
    """A form to enrol a user in an offering."""
641
534
        ctx['offering'] = self.context
642
535
        ctx['roles_auth'] = self.context.get_permissions(req.user, req.config)
643
536
        ctx['errors'] = errors
644
 
        # If all of the fields validated, set the global form error.
645
 
        if isinstance(errors, basestring):
646
 
            ctx['error_value'] = errors
647
 
 
648
 
 
649
 
class EnrolmentEditSchema(formencode.Schema):
650
 
    role = formencode.All(formencode.validators.OneOf(
651
 
                                ["lecturer", "tutor", "student"]),
652
 
                          RoleEnrolmentValidator(),
653
 
                          formencode.validators.UnicodeString())
654
 
 
655
 
 
656
 
class EnrolmentEdit(BaseFormView):
657
 
    """A form to alter an enrolment's role."""
658
 
    template = 'templates/enrolment-edit.html'
659
 
    tab = 'subjects'
660
 
    permission = 'edit'
661
 
 
662
 
    def populate_state(self, state):
663
 
        state.offering = self.context.offering
664
 
 
665
 
    def get_default_data(self, req):
666
 
        return {'role': self.context.role}
667
 
 
668
 
    @property
669
 
    def validator(self):
670
 
        return EnrolmentEditSchema()
671
 
 
672
 
    def save_object(self, req, data):
673
 
        self.context.role = data['role']
674
 
 
675
 
    def get_return_url(self, obj):
676
 
        return self.req.publisher.generate(
677
 
            self.context.offering, EnrolmentsView)
678
 
 
679
 
    def populate(self, req, ctx):
680
 
        super(EnrolmentEdit, self).populate(req, ctx)
681
 
        ctx['offering_perms'] = self.context.offering.get_permissions(
682
 
            req.user, req.config)
683
 
 
684
 
 
685
 
class EnrolmentDelete(XHTMLView):
686
 
    """A form to alter an enrolment's role."""
687
 
    template = 'templates/enrolment-delete.html'
688
 
    tab = 'subjects'
689
 
    permission = 'edit'
690
 
 
691
 
    def populate(self, req, ctx):
692
 
        # If POSTing, delete delete delete.
693
 
        if req.method == 'POST':
694
 
            self.context.delete()
695
 
            req.store.commit()
696
 
            req.throw_redirect(req.publisher.generate(
697
 
                self.context.offering, EnrolmentsView))
698
 
 
699
 
        ctx['enrolment'] = self.context
700
 
 
701
537
 
702
538
class OfferingProjectsView(XHTMLView):
703
539
    """View the projects for an offering."""
704
540
    template = 'templates/offering_projects.html'
705
541
    permission = 'edit'
706
542
    tab = 'subjects'
707
 
    breadcrumb_text = 'Projects'
708
543
 
709
544
    def populate(self, req, ctx):
710
545
        self.plugin_styles[Plugin] = ["project.css"]
 
546
        self.plugin_scripts[Plugin] = ["project.js"]
711
547
        ctx['req'] = req
712
548
        ctx['offering'] = self.context
713
549
        ctx['projectsets'] = []
 
550
        ctx['OfferingRESTView'] = OfferingRESTView
714
551
 
715
552
        #Open the projectset Fragment, and render it for inclusion
716
553
        #into the ProjectSets page
 
554
        #XXX: This could be a lot cleaner
 
555
        loader = genshi.template.TemplateLoader(".", auto_reload=True)
 
556
 
717
557
        set_fragment = os.path.join(os.path.dirname(__file__),
718
558
                "templates/projectset_fragment.html")
719
559
        project_fragment = os.path.join(os.path.dirname(__file__),
720
560
                "templates/project_fragment.html")
721
561
 
722
 
        for projectset in \
723
 
            self.context.project_sets.order_by(ivle.database.ProjectSet.id):
724
 
            settmpl = self._loader.load(set_fragment)
 
562
        for projectset in self.context.project_sets:
 
563
            settmpl = loader.load(set_fragment)
725
564
            setCtx = Context()
726
565
            setCtx['req'] = req
727
566
            setCtx['projectset'] = projectset
728
567
            setCtx['projects'] = []
729
568
            setCtx['GroupsView'] = GroupsView
730
 
            setCtx['ProjectSetEdit'] = ProjectSetEdit
731
 
            setCtx['ProjectNew'] = ProjectNew
 
569
            setCtx['ProjectSetRESTView'] = ProjectSetRESTView
732
570
 
733
 
            for project in \
734
 
                projectset.projects.order_by(ivle.database.Project.deadline):
735
 
                projecttmpl = self._loader.load(project_fragment)
 
571
            for project in projectset.projects:
 
572
                projecttmpl = loader.load(project_fragment)
736
573
                projectCtx = Context()
737
574
                projectCtx['req'] = req
738
575
                projectCtx['project'] = project
739
 
                projectCtx['ProjectEdit'] = ProjectEdit
740
 
                projectCtx['ProjectDelete'] = ProjectDelete
741
576
 
742
577
                setCtx['projects'].append(
743
578
                        projecttmpl.generate(projectCtx))
751
586
    permission = "view_project_submissions"
752
587
    tab = 'subjects'
753
588
 
 
589
    def build_subversion_url(self, svnroot, submission):
 
590
        princ = submission.assessed.principal
 
591
 
 
592
        if isinstance(princ, User):
 
593
            path = 'users/%s' % princ.login
 
594
        else:
 
595
            path = 'groups/%s_%s_%s_%s' % (
 
596
                    princ.project_set.offering.subject.short_name,
 
597
                    princ.project_set.offering.semester.year,
 
598
                    princ.project_set.offering.semester.semester,
 
599
                    princ.name
 
600
                    )
 
601
        return urlparse.urljoin(
 
602
                    svnroot,
 
603
                    os.path.join(path, submission.path[1:] if
 
604
                                       submission.path.startswith(os.sep) else
 
605
                                       submission.path))
 
606
 
754
607
    def populate(self, req, ctx):
755
608
        self.plugin_styles[Plugin] = ["project.css"]
756
609
 
757
610
        ctx['req'] = req
758
 
        ctx['permissions'] = self.context.get_permissions(req.user,req.config)
759
611
        ctx['GroupsView'] = GroupsView
760
612
        ctx['EnrolView'] = EnrolView
761
 
        ctx['format_datetime'] = ivle.date.make_date_nice
762
 
        ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
763
 
        ctx['project'] = self.context
764
 
        ctx['user'] = req.user
765
 
        ctx['ProjectEdit'] = ProjectEdit
766
 
        ctx['ProjectDelete'] = ProjectDelete
767
 
        ctx['ProjectExport'] = ProjectBashExportView
768
 
 
769
 
class ProjectBashExportView(TextView):
770
 
    """Produce a Bash script for exporting projects"""
771
 
    template = "templates/project-export.sh"
772
 
    content_type = "text/x-sh"
773
 
    permission = "view_project_submissions"
774
 
 
775
 
    def populate(self, req, ctx):
776
 
        ctx['req'] = req
777
 
        ctx['permissions'] = self.context.get_permissions(req.user,req.config)
778
 
        ctx['format_datetime'] = ivle.date.make_date_nice
779
 
        ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
780
 
        ctx['project'] = self.context
781
 
        ctx['user'] = req.user
782
 
        ctx['now'] = datetime.datetime.now()
783
 
        ctx['format_datetime'] = ivle.date.make_date_nice
784
 
        ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
785
 
 
786
 
class ProjectUniquenessValidator(formencode.FancyValidator):
787
 
    """A FormEncode validator that checks that a project short_name is unique
788
 
    in a given offering.
789
 
 
790
 
    The project referenced by state.existing_project is permitted to
791
 
    hold that short_name. If any other project holds it, the input is rejected.
792
 
    """
793
 
    def _to_python(self, value, state):
794
 
        if (state.store.find(
795
 
            Project,
796
 
            Project.short_name == unicode(value),
797
 
            Project.project_set_id == ProjectSet.id,
798
 
            ProjectSet.offering == state.offering).one() not in
799
 
            (None, state.existing_project)):
800
 
            raise formencode.Invalid(
801
 
                "A project with that URL name already exists in this offering."
802
 
                , value, state)
803
 
        return value
804
 
 
805
 
class ProjectSchema(formencode.Schema):
806
 
    name = formencode.validators.UnicodeString(not_empty=True)
807
 
    short_name = formencode.All(
808
 
        URLNameValidator(not_empty=True),
809
 
        ProjectUniquenessValidator())
810
 
    deadline = DateTimeValidator(not_empty=True)
811
 
    url = formencode.validators.URL(if_missing=None, not_empty=False)
812
 
    synopsis = formencode.validators.UnicodeString(not_empty=True)
813
 
 
814
 
class ProjectEdit(BaseFormView):
815
 
    """A form to edit a project."""
816
 
    template = 'templates/project-edit.html'
817
 
    tab = 'subjects'
818
 
    permission = 'edit'
819
 
 
820
 
    @property
821
 
    def validator(self):
822
 
        return ProjectSchema()
823
 
 
824
 
    def populate(self, req, ctx):
825
 
        super(ProjectEdit, self).populate(req, ctx)
826
 
        ctx['projectset'] = self.context.project_set
827
 
 
828
 
    def populate_state(self, state):
829
 
        state.offering = self.context.project_set.offering
830
 
        state.existing_project = self.context
831
 
 
832
 
    def get_default_data(self, req):
833
 
        return {
834
 
            'name':         self.context.name,
835
 
            'short_name':   self.context.short_name,
836
 
            'deadline':     self.context.deadline,
837
 
            'url':          self.context.url,
838
 
            'synopsis':     self.context.synopsis,
839
 
            }
840
 
 
841
 
    def save_object(self, req, data):
842
 
        self.context.name = data['name']
843
 
        self.context.short_name = data['short_name']
844
 
        self.context.deadline = data['deadline']
845
 
        self.context.url = unicode(data['url']) if data['url'] else None
846
 
        self.context.synopsis = data['synopsis']
847
 
        return self.context
848
 
 
849
 
class ProjectNew(BaseFormView):
850
 
    """A form to create a new project."""
851
 
    template = 'templates/project-new.html'
852
 
    tab = 'subjects'
853
 
    permission = 'edit'
854
 
 
855
 
    @property
856
 
    def validator(self):
857
 
        return ProjectSchema()
858
 
 
859
 
    def populate(self, req, ctx):
860
 
        super(ProjectNew, self).populate(req, ctx)
861
 
        ctx['projectset'] = self.context
862
 
 
863
 
    def populate_state(self, state):
864
 
        state.offering = self.context.offering
865
 
        state.existing_project = None
866
 
 
867
 
    def get_default_data(self, req):
868
 
        return {}
869
 
 
870
 
    def save_object(self, req, data):
871
 
        new_project = Project()
872
 
        new_project.project_set = self.context
873
 
        new_project.name = data['name']
874
 
        new_project.short_name = data['short_name']
875
 
        new_project.deadline = data['deadline']
876
 
        new_project.url = unicode(data['url']) if data['url'] else None
877
 
        new_project.synopsis = data['synopsis']
878
 
        req.store.add(new_project)
879
 
        return new_project
880
 
 
881
 
class ProjectDelete(XHTMLView):
882
 
    """A form to delete a project."""
883
 
    template = 'templates/project-delete.html'
884
 
    tab = 'subjects'
885
 
    permission = 'edit'
886
 
 
887
 
    def populate(self, req, ctx):
888
 
        # If post, delete the project, or display a message explaining that
889
 
        # the project cannot be deleted
890
 
        if self.context.can_delete:
891
 
            if req.method == 'POST':
892
 
                self.context.delete()
893
 
                self.template = 'templates/project-deleted.html'
894
 
        else:
895
 
            # Can't delete
896
 
            self.template = 'templates/project-undeletable.html'
897
 
 
898
 
        # If get and can delete, display a delete confirmation page
899
 
 
900
 
        # Variables for the template
901
 
        ctx['req'] = req
902
 
        ctx['project'] = self.context
903
 
        ctx['OfferingProjectsView'] = OfferingProjectsView
904
 
 
905
 
class ProjectSetSchema(formencode.Schema):
906
 
    group_size = formencode.validators.Int(if_missing=None, not_empty=False)
907
 
 
908
 
class ProjectSetEdit(BaseFormView):
909
 
    """A form to edit a project set."""
910
 
    template = 'templates/projectset-edit.html'
911
 
    tab = 'subjects'
912
 
    permission = 'edit'
913
 
 
914
 
    @property
915
 
    def validator(self):
916
 
        return ProjectSetSchema()
917
 
 
918
 
    def populate(self, req, ctx):
919
 
        super(ProjectSetEdit, self).populate(req, ctx)
920
 
 
921
 
    def get_default_data(self, req):
922
 
        return {
923
 
            'group_size': self.context.max_students_per_group,
924
 
            }
925
 
 
926
 
    def save_object(self, req, data):
927
 
        self.context.max_students_per_group = data['group_size']
928
 
        return self.context
929
 
 
930
 
class ProjectSetNew(BaseFormView):
931
 
    """A form to create a new project set."""
932
 
    template = 'templates/projectset-new.html'
933
 
    tab = 'subjects'
934
 
    permission = 'edit'
935
 
    breadcrumb_text = "Projects"
936
 
 
937
 
    @property
938
 
    def validator(self):
939
 
        return ProjectSetSchema()
940
 
 
941
 
    def populate(self, req, ctx):
942
 
        super(ProjectSetNew, self).populate(req, ctx)
943
 
 
944
 
    def get_default_data(self, req):
945
 
        return {}
946
 
 
947
 
    def save_object(self, req, data):
948
 
        new_set = ProjectSet()
949
 
        new_set.offering = self.context
950
 
        new_set.max_students_per_group = data['group_size']
951
 
        req.store.add(new_set)
952
 
        return new_set
 
613
        ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
 
614
        ctx['build_subversion_url'] = self.build_subversion_url
 
615
        ctx['svn_addr'] = req.config['urls']['svn_addr']
 
616
        ctx['project'] = self.context
 
617
        ctx['user'] = req.user
953
618
 
954
619
class Plugin(ViewPlugin, MediaPlugin):
955
620
    forward_routes = (root_to_subject, root_to_semester, subject_to_offering,
956
 
                      offering_to_project, offering_to_projectset,
957
 
                      offering_to_enrolment)
 
621
                      offering_to_project, offering_to_projectset)
958
622
    reverse_routes = (
959
 
        subject_url, semester_url, offering_url, projectset_url, project_url,
960
 
        enrolment_url)
 
623
        subject_url, semester_url, offering_url, projectset_url, project_url)
961
624
 
962
625
    views = [(ApplicationRoot, ('subjects', '+index'), SubjectsView),
963
626
             (ApplicationRoot, ('subjects', '+manage'), SubjectsManage),
964
627
             (ApplicationRoot, ('subjects', '+new'), SubjectNew),
965
628
             (ApplicationRoot, ('subjects', '+new-offering'), OfferingNew),
966
629
             (ApplicationRoot, ('+semesters', '+new'), SemesterNew),
967
 
             (Subject, '+index', SubjectView),
968
630
             (Subject, '+edit', SubjectEdit),
969
 
             (Subject, '+new-offering', SubjectOfferingNew),
970
631
             (Semester, '+edit', SemesterEdit),
971
632
             (Offering, '+index', OfferingView),
972
633
             (Offering, '+edit', OfferingEdit),
973
 
             (Offering, '+clone-worksheets', OfferingCloneWorksheets),
974
634
             (Offering, ('+enrolments', '+index'), EnrolmentsView),
975
635
             (Offering, ('+enrolments', '+new'), EnrolView),
976
 
             (Enrolment, '+edit', EnrolmentEdit),
977
 
             (Enrolment, '+delete', EnrolmentDelete),
978
636
             (Offering, ('+projects', '+index'), OfferingProjectsView),
979
 
             (Offering, ('+projects', '+new-set'), ProjectSetNew),
980
 
             (ProjectSet, '+edit', ProjectSetEdit),
981
 
             (ProjectSet, '+new', ProjectNew),
982
637
             (Project, '+index', ProjectView),
983
 
             (Project, '+edit', ProjectEdit),
984
 
             (Project, '+delete', ProjectDelete),
985
 
             (Project, ('+export', 'project-export.sh'),
986
 
                ProjectBashExportView),
 
638
 
 
639
             (Offering, ('+projectsets', '+new'), OfferingRESTView, 'api'),
 
640
             (ProjectSet, ('+projects', '+new'), ProjectSetRESTView, 'api'),
987
641
             ]
988
642
 
989
643
    breadcrumbs = {Subject: SubjectBreadcrumb,
990
644
                   Offering: OfferingBreadcrumb,
991
645
                   User: UserBreadcrumb,
992
646
                   Project: ProjectBreadcrumb,
993
 
                   Enrolment: EnrolmentBreadcrumb,
994
647
                   }
995
648
 
996
649
    tabs = [