36
36
import formencode.validators
38
from ivle.webapp.base.forms import BaseFormView, URLNameValidator
38
from ivle.webapp.base.forms import (BaseFormView, URLNameValidator,
39
40
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
40
41
from ivle.webapp.base.xhtml import XHTMLView
41
42
from ivle.webapp.errors import BadRequest
759
760
ctx['project'] = self.context
760
761
ctx['user'] = req.user
763
class ProjectUniquenessValidator(formencode.FancyValidator):
764
"""A FormEncode validator that checks that a project short_name is unique
767
The project referenced by state.existing_project is permitted to
768
hold that short_name. If any other project holds it, the input is rejected.
770
def _to_python(self, value, state):
771
# TODO: Allow short_name to be equal to existing_project
774
Project.short_name == unicode(value),
775
Project.project_set_id == ProjectSet.id,
776
ProjectSet.offering == state.offering).one():
777
raise formencode.Invalid(
778
"A project with that URL name already exists in this offering."
782
class ProjectSchema(formencode.Schema):
783
name = formencode.validators.UnicodeString(not_empty=True)
784
short_name = formencode.All(
785
URLNameValidator(not_empty=True),
786
ProjectUniquenessValidator())
787
deadline = DateTimeValidator(not_empty=True)
788
url = formencode.validators.URL(if_missing=None, not_empty=False)
789
synopsis = formencode.validators.UnicodeString(not_empty=True)
791
class ProjectNew(BaseFormView):
792
"""A form to create a new project."""
793
template = 'templates/project-new.html'
799
return ProjectSchema()
801
def populate(self, req, ctx):
802
super(ProjectNew, self).populate(req, ctx)
804
def populate_state(self, state):
805
state.offering = self.context.offering
806
state.existing_project = None
808
def get_default_data(self, req):
811
def save_object(self, req, data):
812
new_project = Project()
813
new_project.project_set = self.context
814
new_project.name = data['name']
815
new_project.short_name = data['short_name']
816
new_project.deadline = data['deadline']
817
new_project.url = data['url']
818
new_project.synopsis = data['synopsis']
819
req.store.add(new_project)
762
822
class ProjectSetSchema(formencode.Schema):
763
823
group_size = formencode.validators.Int(if_missing=None, not_empty=False)
835
895
(Offering, ('+projects', '+index'), OfferingProjectsView),
836
896
(Offering, ('+projects', '+new-set'), ProjectSetNew),
837
897
(ProjectSet, '+edit', ProjectSetEdit),
898
(ProjectSet, '+new', ProjectNew),
838
899
(Project, '+index', ProjectView),
840
901
(ProjectSet, ('+projects', '+new'), ProjectSetRESTView, 'api'),