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

« back to all changes in this revision

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

  • Committer: William Grant
  • Date: 2009-05-26 02:38:09 UTC
  • Revision ID: grantw@unimelb.edu.au-20090526023809-mqffswolwmvk9m00
Rename to to_home_path, and use it in ivle.interpret.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
# Author: Nick Chadwick
19
19
 
20
 
import datetime
21
 
 
22
 
import formencode
23
 
import formencode.validators
24
 
 
25
20
import ivle.database
26
21
from ivle.database import ProjectSet, Project, Subject, Semester, Offering
27
22
 
28
 
from ivle.webapp.base.forms import VALID_URL_NAME
29
23
from ivle.webapp.base.rest import (XHTMLRESTView, named_operation,
30
24
                                   require_permission)
31
 
from ivle.webapp.errors import NotFound, BadRequest
 
25
from ivle.webapp.errors import NotFound
32
26
 
33
27
class ProjectSetRESTView(XHTMLRESTView):
34
28
    """Rest view for a projectset.
35
29
    
36
30
    Add new, delete, edit functionality is given here."""
37
31
    template = "templates/projectset_fragment.html"
 
32
    
 
33
    def __init__(self, req, subject, year, semester, projectset):
 
34
        self.context = req.store.find(ProjectSet,
 
35
            ProjectSet.id == int(projectset),
 
36
            ProjectSet.offering_id == Offering.id,
 
37
            Offering.subject_id == Subject.id,
 
38
            Subject.short_name == unicode(subject),
 
39
            Offering.semester_id == Semester.id,
 
40
            Semester.year == unicode(year),
 
41
            Semester.semester == unicode(semester)).one()
 
42
        
 
43
        if self.context is None:
 
44
            raise NotFound()
38
45
 
39
46
    def _project_url(self, project):
40
47
        return "/subjects/%s/%s/%s/+projects/%s" % \
44
51
                 project.short_name)
45
52
 
46
53
    @named_operation('edit')
47
 
    def add_project(self, req, name, short_name, deadline, synopsis, url):
 
54
    def add_project(self, req, name, short_name, synopsis):
48
55
        """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
56
        new_project = Project()
70
57
        new_project.name = unicode(name)
71
58
        new_project.short_name = unicode(short_name)
72
 
        new_project.url = unicode(url)
73
59
        new_project.synopsis = unicode(synopsis)
74
 
        try:
75
 
            new_project.deadline = datetime.datetime.strptime(deadline, '%Y-%m-%d %H:%M:%S')
76
 
        except ValueError:
77
 
            raise BadRequest("deadline must be in YYYY-MM-DD HH:MM:ss")
78
60
        new_project.project_set = self.context
79
61
 
80
62
        req.store.add(new_project)
81
63
        req.store.flush()
82
64
    
83
65
        self.template = "templates/project_fragment.html"
84
 
        self.ctx['req'] = req
85
66
        self.ctx['project'] = new_project
 
67
        self.ctx['project_url'] = self._project_url(new_project)
86
68
 
87
69
        return {'success': True, 'projectset_id': self.context.id}
 
70
 
 
71
class ProjectRESTView(XHTMLRESTView):
 
72
    """Rest view for a project."""