98
95
def populate(self, req, ctx):
100
97
ctx['mediapath'] = media_url(req, CorePlugin, 'images/')
101
ctx['SubjectView'] = SubjectView
102
98
ctx['SubjectEdit'] = SubjectEdit
103
99
ctx['SemesterEdit'] = SemesterEdit
105
101
ctx['subjects'] = req.store.find(Subject).order_by(Subject.name)
106
102
ctx['semesters'] = req.store.find(Semester).order_by(
107
Semester.year, Semester.display_name)
110
class SubjectUniquenessValidator(formencode.FancyValidator):
111
"""A FormEncode validator that checks that a subject attribute is unique.
103
Semester.year, Semester.semester)
106
class SubjectShortNameUniquenessValidator(formencode.FancyValidator):
107
"""A FormEncode validator that checks that a subject name is unused.
113
109
The subject referenced by state.existing_subject is permitted
114
110
to hold that name. If any other object holds it, the input is rejected.
116
:param attribute: the name of the attribute to check.
117
:param display: a string to identify the field in case of error.
120
def __init__(self, attribute, display):
121
self.attribute = attribute
122
self.display = display
112
def __init__(self, matching=None):
113
self.matching = matching
124
115
def _to_python(self, value, state):
125
if (state.store.find(Subject, **{self.attribute: value}).one() not in
116
if (state.store.find(
117
Subject, short_name=value).one() not in
126
118
(None, state.existing_subject)):
127
119
raise formencode.Invalid(
128
'%s already taken' % self.display, value, state)
120
'Short name already taken', value, state)
132
124
class SubjectSchema(formencode.Schema):
133
125
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'),
126
SubjectShortNameUniquenessValidator(),
139
127
formencode.validators.UnicodeString(not_empty=True))
128
name = formencode.validators.UnicodeString(not_empty=True)
129
code = formencode.validators.UnicodeString(not_empty=True)
142
132
class SubjectFormView(BaseFormView):
641
579
ctx['offering'] = self.context
642
580
ctx['roles_auth'] = self.context.get_permissions(req.user, req.config)
643
581
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
649
class EnrolmentEditSchema(formencode.Schema):
650
role = formencode.All(formencode.validators.OneOf(
651
["lecturer", "tutor", "student"]),
652
RoleEnrolmentValidator(),
653
formencode.validators.UnicodeString())
656
class EnrolmentEdit(BaseFormView):
657
"""A form to alter an enrolment's role."""
658
template = 'templates/enrolment-edit.html'
662
def populate_state(self, state):
663
state.offering = self.context.offering
665
def get_default_data(self, req):
666
return {'role': self.context.role}
670
return EnrolmentEditSchema()
672
def save_object(self, req, data):
673
self.context.role = data['role']
675
def get_return_url(self, obj):
676
return self.req.publisher.generate(
677
self.context.offering, EnrolmentsView)
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)
685
class EnrolmentDelete(XHTMLView):
686
"""A form to alter an enrolment's role."""
687
template = 'templates/enrolment-delete.html'
691
def populate(self, req, ctx):
692
# If POSTing, delete delete delete.
693
if req.method == 'POST':
694
self.context.delete()
696
req.throw_redirect(req.publisher.generate(
697
self.context.offering, EnrolmentsView))
699
ctx['enrolment'] = self.context
702
583
class OfferingProjectsView(XHTMLView):
703
584
"""View the projects for an offering."""
704
585
template = 'templates/offering_projects.html'
705
586
permission = 'edit'
707
breadcrumb_text = 'Projects'
709
589
def populate(self, req, ctx):
710
590
self.plugin_styles[Plugin] = ["project.css"]
591
self.plugin_scripts[Plugin] = ["project.js"]
712
593
ctx['offering'] = self.context
713
594
ctx['projectsets'] = []
595
ctx['OfferingRESTView'] = OfferingRESTView
715
597
#Open the projectset Fragment, and render it for inclusion
716
598
#into the ProjectSets page
599
#XXX: This could be a lot cleaner
600
loader = genshi.template.TemplateLoader(".", auto_reload=True)
717
602
set_fragment = os.path.join(os.path.dirname(__file__),
718
603
"templates/projectset_fragment.html")
719
604
project_fragment = os.path.join(os.path.dirname(__file__),
720
605
"templates/project_fragment.html")
723
self.context.project_sets.order_by(ivle.database.ProjectSet.id):
724
settmpl = self._loader.load(set_fragment)
607
for projectset in self.context.project_sets:
608
settmpl = loader.load(set_fragment)
725
609
setCtx = Context()
726
610
setCtx['req'] = req
727
611
setCtx['projectset'] = projectset
728
612
setCtx['projects'] = []
729
613
setCtx['GroupsView'] = GroupsView
730
setCtx['ProjectSetEdit'] = ProjectSetEdit
731
setCtx['ProjectNew'] = ProjectNew
614
setCtx['ProjectSetRESTView'] = ProjectSetRESTView
734
projectset.projects.order_by(ivle.database.Project.deadline):
735
projecttmpl = self._loader.load(project_fragment)
616
for project in projectset.projects:
617
projecttmpl = loader.load(project_fragment)
736
618
projectCtx = Context()
737
619
projectCtx['req'] = req
738
620
projectCtx['project'] = project
739
projectCtx['ProjectEdit'] = ProjectEdit
740
projectCtx['ProjectDelete'] = ProjectDelete
742
622
setCtx['projects'].append(
743
623
projecttmpl.generate(projectCtx))
751
631
permission = "view_project_submissions"
634
def build_subversion_url(self, svnroot, submission):
635
princ = submission.assessed.principal
637
if isinstance(princ, User):
638
path = 'users/%s' % princ.login
640
path = 'groups/%s_%s_%s_%s' % (
641
princ.project_set.offering.subject.short_name,
642
princ.project_set.offering.semester.year,
643
princ.project_set.offering.semester.semester,
646
return urlparse.urljoin(
648
os.path.join(path, submission.path[1:] if
649
submission.path.startswith(os.sep) else
754
652
def populate(self, req, ctx):
755
653
self.plugin_styles[Plugin] = ["project.css"]
758
ctx['permissions'] = self.context.get_permissions(req.user,req.config)
759
656
ctx['GroupsView'] = GroupsView
760
657
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
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"
775
def populate(self, req, ctx):
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
786
class ProjectUniquenessValidator(formencode.FancyValidator):
787
"""A FormEncode validator that checks that a project short_name is unique
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.
793
def _to_python(self, value, state):
794
if (state.store.find(
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."
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)
814
class ProjectEdit(BaseFormView):
815
"""A form to edit a project."""
816
template = 'templates/project-edit.html'
822
return ProjectSchema()
824
def populate(self, req, ctx):
825
super(ProjectEdit, self).populate(req, ctx)
826
ctx['projectset'] = self.context.project_set
828
def populate_state(self, state):
829
state.offering = self.context.project_set.offering
830
state.existing_project = self.context
832
def get_default_data(self, req):
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,
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']
849
class ProjectNew(BaseFormView):
850
"""A form to create a new project."""
851
template = 'templates/project-new.html'
857
return ProjectSchema()
859
def populate(self, req, ctx):
860
super(ProjectNew, self).populate(req, ctx)
861
ctx['projectset'] = self.context
863
def populate_state(self, state):
864
state.offering = self.context.offering
865
state.existing_project = None
867
def get_default_data(self, req):
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)
881
class ProjectDelete(XHTMLView):
882
"""A form to delete a project."""
883
template = 'templates/project-delete.html'
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'
896
self.template = 'templates/project-undeletable.html'
898
# If get and can delete, display a delete confirmation page
900
# Variables for the template
902
ctx['project'] = self.context
903
ctx['OfferingProjectsView'] = OfferingProjectsView
905
class ProjectSetSchema(formencode.Schema):
906
group_size = formencode.validators.Int(if_missing=None, not_empty=False)
908
class ProjectSetEdit(BaseFormView):
909
"""A form to edit a project set."""
910
template = 'templates/projectset-edit.html'
916
return ProjectSetSchema()
918
def populate(self, req, ctx):
919
super(ProjectSetEdit, self).populate(req, ctx)
921
def get_default_data(self, req):
923
'group_size': self.context.max_students_per_group,
926
def save_object(self, req, data):
927
self.context.max_students_per_group = data['group_size']
930
class ProjectSetNew(BaseFormView):
931
"""A form to create a new project set."""
932
template = 'templates/projectset-new.html'
935
breadcrumb_text = "Projects"
939
return ProjectSetSchema()
941
def populate(self, req, ctx):
942
super(ProjectSetNew, self).populate(req, ctx)
944
def get_default_data(self, req):
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)
658
ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
659
ctx['build_subversion_url'] = self.build_subversion_url
660
ctx['svn_addr'] = req.config['urls']['svn_addr']
661
ctx['project'] = self.context
662
ctx['user'] = req.user
954
664
class Plugin(ViewPlugin, MediaPlugin):
955
665
forward_routes = (root_to_subject, root_to_semester, subject_to_offering,
956
offering_to_project, offering_to_projectset,
957
offering_to_enrolment)
666
offering_to_project, offering_to_projectset)
958
667
reverse_routes = (
959
subject_url, semester_url, offering_url, projectset_url, project_url,
668
subject_url, semester_url, offering_url, projectset_url, project_url)
962
670
views = [(ApplicationRoot, ('subjects', '+index'), SubjectsView),
963
671
(ApplicationRoot, ('subjects', '+manage'), SubjectsManage),
964
672
(ApplicationRoot, ('subjects', '+new'), SubjectNew),
965
673
(ApplicationRoot, ('subjects', '+new-offering'), OfferingNew),
966
674
(ApplicationRoot, ('+semesters', '+new'), SemesterNew),
967
(Subject, '+index', SubjectView),
968
675
(Subject, '+edit', SubjectEdit),
969
(Subject, '+new-offering', SubjectOfferingNew),
970
676
(Semester, '+edit', SemesterEdit),
971
677
(Offering, '+index', OfferingView),
972
678
(Offering, '+edit', OfferingEdit),
973
679
(Offering, '+clone-worksheets', OfferingCloneWorksheets),
974
680
(Offering, ('+enrolments', '+index'), EnrolmentsView),
975
681
(Offering, ('+enrolments', '+new'), EnrolView),
976
(Enrolment, '+edit', EnrolmentEdit),
977
(Enrolment, '+delete', EnrolmentDelete),
978
682
(Offering, ('+projects', '+index'), OfferingProjectsView),
979
(Offering, ('+projects', '+new-set'), ProjectSetNew),
980
(ProjectSet, '+edit', ProjectSetEdit),
981
(ProjectSet, '+new', ProjectNew),
982
683
(Project, '+index', ProjectView),
983
(Project, '+edit', ProjectEdit),
984
(Project, '+delete', ProjectDelete),
985
(Project, ('+export', 'project-export.sh'),
986
ProjectBashExportView),
685
(Offering, ('+projectsets', '+new'), OfferingRESTView, 'api'),
686
(ProjectSet, ('+projects', '+new'), ProjectSetRESTView, 'api'),
989
689
breadcrumbs = {Subject: SubjectBreadcrumb,
990
690
Offering: OfferingBreadcrumb,
991
691
User: UserBreadcrumb,
992
692
Project: ProjectBreadcrumb,
993
Enrolment: EnrolmentBreadcrumb,