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

« back to all changes in this revision

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

  • Committer: David Coles
  • Date: 2009-12-08 02:10:26 UTC
  • Revision ID: coles.david@gmail.com-20091208021026-3a27ecdzm49y39me
Configuration documentation - fixing a few references

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# Author: Nick Chadwick
19
19
 
20
20
import datetime
21
 
 
22
 
import formencode
23
 
import formencode.validators
24
 
 
25
21
import ivle.database
26
22
from ivle.database import ProjectSet, Project, Subject, Semester, Offering
27
23
 
28
 
from ivle.webapp.base.forms import VALID_URL_NAME
29
24
from ivle.webapp.base.rest import (XHTMLRESTView, named_operation,
30
25
                                   require_permission)
31
26
from ivle.webapp.errors import NotFound, BadRequest
35
30
    
36
31
    Add new, delete, edit functionality is given here."""
37
32
    template = "templates/projectset_fragment.html"
 
33
    
 
34
    def __init__(self, req, subject, year, semester, projectset):
 
35
        self.context = req.store.find(ProjectSet,
 
36
            ProjectSet.id == int(projectset),
 
37
            ProjectSet.offering_id == Offering.id,
 
38
            Offering.subject_id == Subject.id,
 
39
            Subject.short_name == unicode(subject),
 
40
            Offering.semester_id == Semester.id,
 
41
            Semester.year == unicode(year),
 
42
            Semester.semester == unicode(semester)).one()
 
43
        
 
44
        if self.context is None:
 
45
            raise NotFound()
38
46
 
39
47
    def _project_url(self, project):
40
48
        return "/subjects/%s/%s/%s/+projects/%s" % \
44
52
                 project.short_name)
45
53
 
46
54
    @named_operation('edit')
47
 
    def add_project(self, req, name, short_name, deadline, synopsis, url):
 
55
    def add_project(self, req, name, short_name, deadline, synopsis):
48
56
        """Add a Project to this ProjectSet"""
49
 
        if not VALID_URL_NAME.match(short_name):
50
 
            raise BadRequest(
51
 
                "Project names must consist of a lowercase alphanumeric "
52
 
                "character followed by any number of lowercase alphanumerics, "
53
 
                "., +, - or _.")
54
 
 
55
 
        if req.store.find(
56
 
            Project,
57
 
            Project.short_name == unicode(short_name),
58
 
            Project.project_set_id == ProjectSet.id,
59
 
            ProjectSet.offering == self.context.offering).one():
60
 
            raise BadRequest(
61
 
                "A project with that URL name already exists in this offering."
62
 
                )
63
 
 
64
 
        try:
65
 
            formencode.validators.URL().to_python(url)
66
 
        except formencode.Invalid, e:
67
 
            raise BadRequest(str(e))
68
 
 
69
57
        new_project = Project()
70
58
        new_project.name = unicode(name)
71
59
        new_project.short_name = unicode(short_name)
72
 
        new_project.url = unicode(url)
73
60
        new_project.synopsis = unicode(synopsis)
74
61
        try:
75
62
            new_project.deadline = datetime.datetime.strptime(deadline, '%Y-%m-%d %H:%M:%S')
81
68
        req.store.flush()
82
69
    
83
70
        self.template = "templates/project_fragment.html"
84
 
        self.ctx['req'] = req
85
71
        self.ctx['project'] = new_project
 
72
        self.ctx['project_url'] = self._project_url(new_project)
86
73
 
87
74
        return {'success': True, 'projectset_id': self.context.id}
 
75
 
 
76
class ProjectRESTView(XHTMLRESTView):
 
77
    """Rest view for a project."""