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

« back to all changes in this revision

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

Cache worksheet and exercise rST-generated XHTML in the DB, accelerating rendering by up to 2000%.

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
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, URLNameValidator
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
41
from ivle.webapp.errors import BadRequest
45
42
from ivle.webapp import ApplicationRoot
46
43
 
49
46
from ivle import util
50
47
import ivle.date
51
48
 
 
49
from ivle.webapp.admin.projectservice import ProjectSetRESTView
 
50
from ivle.webapp.admin.offeringservice import OfferingRESTView
52
51
from ivle.webapp.admin.publishing import (root_to_subject, root_to_semester,
53
52
            subject_to_offering, offering_to_projectset, offering_to_project,
54
53
            offering_to_enrolment, subject_url, semester_url, offering_url,
55
54
            projectset_url, project_url, enrolment_url)
56
55
from ivle.webapp.admin.breadcrumbs import (SubjectBreadcrumb,
57
56
            OfferingBreadcrumb, UserBreadcrumb, ProjectBreadcrumb,
58
 
            ProjectsBreadcrumb, EnrolmentBreadcrumb)
 
57
            EnrolmentBreadcrumb)
59
58
from ivle.webapp.core import Plugin as CorePlugin
60
59
from ivle.webapp.groups import GroupsView
61
60
from ivle.webapp.media import media_url
75
74
        ctx['user'] = req.user
76
75
        ctx['semesters'] = []
77
76
 
78
 
        for semester in req.store.find(Semester).order_by(
79
 
            Desc(Semester.year), Desc(Semester.display_name)):
 
77
        for semester in req.store.find(Semester).order_by(Desc(Semester.year),
 
78
                                                     Desc(Semester.semester)):
80
79
            if req.user.admin:
81
80
                # For admins, show all subjects in the system
82
81
                offerings = list(semester.offerings.find())
104
103
 
105
104
        ctx['subjects'] = req.store.find(Subject).order_by(Subject.name)
106
105
        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.
 
106
            Semester.year, Semester.semester)
 
107
 
 
108
 
 
109
class SubjectShortNameUniquenessValidator(formencode.FancyValidator):
 
110
    """A FormEncode validator that checks that a subject name is unused.
112
111
 
113
112
    The subject referenced by state.existing_subject is permitted
114
113
    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
114
    """
119
 
 
120
 
    def __init__(self, attribute, display):
121
 
        self.attribute = attribute
122
 
        self.display = display
 
115
    def __init__(self, matching=None):
 
116
        self.matching = matching
123
117
 
124
118
    def _to_python(self, value, state):
125
 
        if (state.store.find(Subject, **{self.attribute: value}).one() not in
 
119
        if (state.store.find(
 
120
                Subject, short_name=value).one() not in
126
121
                (None, state.existing_subject)):
127
122
            raise formencode.Invalid(
128
 
                '%s already taken' % self.display, value, state)
 
123
                'Short name already taken', value, state)
129
124
        return value
130
125
 
131
126
 
132
127
class SubjectSchema(formencode.Schema):
133
128
    short_name = formencode.All(
134
 
        SubjectUniquenessValidator('short_name', 'URL name'),
 
129
        SubjectShortNameUniquenessValidator(),
135
130
        URLNameValidator(not_empty=True))
136
131
    name = formencode.validators.UnicodeString(not_empty=True)
137
 
    code = formencode.All(
138
 
        SubjectUniquenessValidator('code', 'Subject code'),
139
 
        formencode.validators.UnicodeString(not_empty=True))
 
132
    code = formencode.validators.UnicodeString(not_empty=True)
140
133
 
141
134
 
142
135
class SubjectFormView(BaseFormView):
200
193
    """
201
194
    def _to_python(self, value, state):
202
195
        if (state.store.find(
203
 
                Semester, year=value['year'], url_name=value['url_name']
 
196
                Semester, year=value['year'], semester=value['semester']
204
197
                ).one() not in (None, state.existing_semester)):
205
198
            raise formencode.Invalid(
206
199
                'Semester already exists', value, state)
209
202
 
210
203
class SemesterSchema(formencode.Schema):
211
204
    year = URLNameValidator()
212
 
    code = formencode.validators.UnicodeString()
213
 
    url_name = URLNameValidator()
214
 
    display_name = formencode.validators.UnicodeString()
 
205
    semester = URLNameValidator()
215
206
    state = formencode.All(
216
207
        formencode.validators.OneOf(["past", "current", "future"]),
217
208
        formencode.validators.UnicodeString())
246
237
    def save_object(self, req, data):
247
238
        new_semester = Semester()
248
239
        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']
 
240
        new_semester.semester = data['semester']
252
241
        new_semester.state = data['state']
253
242
 
254
243
        req.store.add(new_semester)
265
254
    def get_default_data(self, req):
266
255
        return {
267
256
            'year': self.context.year,
268
 
            'code': self.context.code,
269
 
            'url_name': self.context.url_name,
270
 
            'display_name': self.context.display_name,
 
257
            'semester': self.context.semester,
271
258
            'state': self.context.state,
272
259
            }
273
260
 
274
261
    def save_object(self, req, data):
275
262
        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']
 
263
        self.context.semester = data['semester']
279
264
        self.context.state = data['state']
280
265
 
281
266
        return self.context
317
302
        ctx['OfferingCloneWorksheets'] = OfferingCloneWorksheets
318
303
        ctx['GroupsView'] = GroupsView
319
304
        ctx['EnrolmentsView'] = EnrolmentsView
320
 
        ctx['Project'] = ivle.database.Project
321
305
 
322
306
        # As we go, calculate the total score for this subject
323
307
        # (Assessable worksheets only, mandatory problems only)
324
308
 
325
309
        ctx['worksheets'], problems_total, problems_done = (
326
310
            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))
 
311
                req.config, req.store, req.user, self.context))
329
312
 
330
313
        ctx['exercises_total'] = problems_total
331
314
        ctx['exercises_done'] = problems_done
372
355
            year = semester = None
373
356
 
374
357
        semester = state.store.find(
375
 
            Semester, year=year, url_name=semester).one()
 
358
            Semester, year=year, semester=semester).one()
376
359
        if semester:
377
360
            return semester
378
361
        else:
402
385
    description = formencode.validators.UnicodeString(
403
386
        if_missing=None, not_empty=False)
404
387
    url = formencode.validators.URL(if_missing=None, not_empty=False)
405
 
    worksheet_cutoff = DateTimeValidator(if_missing=None, not_empty=False)
406
388
    show_worksheet_marks = formencode.validators.StringBoolean(
407
389
        if_missing=False)
408
390
 
432
414
        super(OfferingEdit, self).populate(req, ctx)
433
415
        ctx['subjects'] = req.store.find(Subject).order_by(Subject.name)
434
416
        ctx['semesters'] = req.store.find(Semester).order_by(
435
 
            Semester.year, Semester.display_name)
 
417
            Semester.year, Semester.semester)
436
418
        ctx['force_subject'] = None
437
419
 
438
420
    def populate_state(self, state):
442
424
        return {
443
425
            'subject': self.context.subject.short_name,
444
426
            'semester': self.context.semester.year + '/' +
445
 
                        self.context.semester.url_name,
 
427
                        self.context.semester.semester,
446
428
            'url': self.context.url,
447
429
            'description': self.context.description,
448
 
            'worksheet_cutoff': self.context.worksheet_cutoff,
449
430
            'show_worksheet_marks': self.context.show_worksheet_marks,
450
431
            }
451
432
 
455
436
            self.context.semester = data['semester']
456
437
        self.context.description = data['description']
457
438
        self.context.url = unicode(data['url']) if data['url'] else None
458
 
        self.context.worksheet_cutoff = data['worksheet_cutoff']
459
439
        self.context.show_worksheet_marks = data['show_worksheet_marks']
460
440
        return self.context
461
441
 
476
456
        super(OfferingNew, self).populate(req, ctx)
477
457
        ctx['subjects'] = req.store.find(Subject).order_by(Subject.name)
478
458
        ctx['semesters'] = req.store.find(Semester).order_by(
479
 
            Semester.year, Semester.display_name)
 
459
            Semester.year, Semester.semester)
480
460
        ctx['force_subject'] = None
481
461
 
482
462
    def populate_state(self, state):
491
471
        new_offering.semester = data['semester']
492
472
        new_offering.description = data['description']
493
473
        new_offering.url = unicode(data['url']) if data['url'] else None
494
 
        new_offering.worksheet_cutoff = data['worksheet_cutoff']
495
474
        new_offering.show_worksheet_marks = data['show_worksheet_marks']
496
475
 
497
476
        req.store.add(new_offering)
528
507
        super(OfferingCloneWorksheets, self).populate(req, ctx)
529
508
        ctx['subjects'] = req.store.find(Subject).order_by(Subject.name)
530
509
        ctx['semesters'] = req.store.find(Semester).order_by(
531
 
            Semester.year, Semester.display_name)
 
510
            Semester.year, Semester.semester)
532
511
 
533
512
    def get_default_data(self, req):
534
513
        return {}
708
687
 
709
688
    def populate(self, req, ctx):
710
689
        self.plugin_styles[Plugin] = ["project.css"]
 
690
        self.plugin_scripts[Plugin] = ["project.js"]
711
691
        ctx['req'] = req
712
692
        ctx['offering'] = self.context
713
693
        ctx['projectsets'] = []
 
694
        ctx['OfferingRESTView'] = OfferingRESTView
714
695
 
715
696
        #Open the projectset Fragment, and render it for inclusion
716
697
        #into the ProjectSets page
727
708
            setCtx['projectset'] = projectset
728
709
            setCtx['projects'] = []
729
710
            setCtx['GroupsView'] = GroupsView
730
 
            setCtx['ProjectSetEdit'] = ProjectSetEdit
731
 
            setCtx['ProjectNew'] = ProjectNew
 
711
            setCtx['ProjectSetRESTView'] = ProjectSetRESTView
732
712
 
733
713
            for project in \
734
714
                projectset.projects.order_by(ivle.database.Project.deadline):
736
716
                projectCtx = Context()
737
717
                projectCtx['req'] = req
738
718
                projectCtx['project'] = project
739
 
                projectCtx['ProjectEdit'] = ProjectEdit
740
 
                projectCtx['ProjectDelete'] = ProjectDelete
741
719
 
742
720
                setCtx['projects'].append(
743
721
                        projecttmpl.generate(projectCtx))
751
729
    permission = "view_project_submissions"
752
730
    tab = 'subjects'
753
731
 
 
732
    def build_subversion_url(self, svnroot, submission):
 
733
        princ = submission.assessed.principal
 
734
 
 
735
        if isinstance(princ, User):
 
736
            path = 'users/%s' % princ.login
 
737
        else:
 
738
            path = 'groups/%s_%s_%s_%s' % (
 
739
                    princ.project_set.offering.subject.short_name,
 
740
                    princ.project_set.offering.semester.year,
 
741
                    princ.project_set.offering.semester.semester,
 
742
                    princ.name
 
743
                    )
 
744
        return urlparse.urljoin(
 
745
                    svnroot,
 
746
                    os.path.join(path, submission.path[1:] if
 
747
                                       submission.path.startswith(os.sep) else
 
748
                                       submission.path))
 
749
 
754
750
    def populate(self, req, ctx):
755
751
        self.plugin_styles[Plugin] = ["project.css"]
756
752
 
757
753
        ctx['req'] = req
758
 
        ctx['permissions'] = self.context.get_permissions(req.user,req.config)
759
754
        ctx['GroupsView'] = GroupsView
760
755
        ctx['EnrolView'] = EnrolView
761
756
        ctx['format_datetime'] = ivle.date.make_date_nice
762
757
        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
 
758
        ctx['build_subversion_url'] = self.build_subversion_url
 
759
        ctx['svn_addr'] = req.config['urls']['svn_addr']
 
760
        ctx['project'] = self.context
 
761
        ctx['user'] = req.user
953
762
 
954
763
class Plugin(ViewPlugin, MediaPlugin):
955
764
    forward_routes = (root_to_subject, root_to_semester, subject_to_offering,
976
785
             (Enrolment, '+edit', EnrolmentEdit),
977
786
             (Enrolment, '+delete', EnrolmentDelete),
978
787
             (Offering, ('+projects', '+index'), OfferingProjectsView),
979
 
             (Offering, ('+projects', '+new-set'), ProjectSetNew),
980
 
             (ProjectSet, '+edit', ProjectSetEdit),
981
 
             (ProjectSet, '+new', ProjectNew),
982
788
             (Project, '+index', ProjectView),
983
 
             (Project, '+edit', ProjectEdit),
984
 
             (Project, '+delete', ProjectDelete),
985
 
             (Project, ('+export', 'project-export.sh'),
986
 
                ProjectBashExportView),
 
789
 
 
790
             (Offering, ('+projectsets', '+new'), OfferingRESTView, 'api'),
 
791
             (ProjectSet, ('+projects', '+new'), ProjectSetRESTView, 'api'),
987
792
             ]
988
793
 
989
794
    breadcrumbs = {Subject: SubjectBreadcrumb,