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

« back to all changes in this revision

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

  • Committer: Matt Giuca
  • Date: 2010-02-25 06:11:59 UTC
  • mto: This revision was merged to the branch mainline in revision 1731.
  • Revision ID: matt.giuca@gmail.com-20100225061159-1dcnsv1zm2i2nveo
Added 'New Project' non-AJAX UI to replace old AJAX one. Currently not linked anywhere; AJAX one still works.

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, URLNameValidator
 
38
from ivle.webapp.base.forms import (BaseFormView, URLNameValidator,
 
39
                                    DateTimeValidator)
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
761
762
 
 
763
class ProjectUniquenessValidator(formencode.FancyValidator):
 
764
    """A FormEncode validator that checks that a project short_name is unique
 
765
    in a given offering.
 
766
 
 
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.
 
769
    """
 
770
    def _to_python(self, value, state):
 
771
        # TODO: Allow short_name to be equal to existing_project
 
772
        if state.store.find(
 
773
            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."
 
779
                , value, state)
 
780
        return value
 
781
 
 
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)
 
790
 
 
791
class ProjectNew(BaseFormView):
 
792
    """A form to create a new project."""
 
793
    template = 'templates/project-new.html'
 
794
    tab = 'subjects'
 
795
    permission = 'edit'
 
796
 
 
797
    @property
 
798
    def validator(self):
 
799
        return ProjectSchema()
 
800
 
 
801
    def populate(self, req, ctx):
 
802
        super(ProjectNew, self).populate(req, ctx)
 
803
 
 
804
    def populate_state(self, state):
 
805
        state.offering = self.context.offering
 
806
        state.existing_project = None
 
807
 
 
808
    def get_default_data(self, req):
 
809
        return {}
 
810
 
 
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)
 
820
        return new_project
 
821
 
762
822
class ProjectSetSchema(formencode.Schema):
763
823
    group_size = formencode.validators.Int(if_missing=None, not_empty=False)
764
824
 
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),
839
900
 
840
901
             (ProjectSet, ('+projects', '+new'), ProjectSetRESTView, 'api'),