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

« back to all changes in this revision

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

  • Committer: Matt Giuca
  • Date: 2010-02-25 06:26:40 UTC
  • mto: This revision was merged to the branch mainline in revision 1731.
  • Revision ID: matt.giuca@gmail.com-20100225062640-tvmtmpeepm3mjc2k
Project page: Replace AJAX project creation UI with link to ProjectNew view. Removed project creation UI including JSON API.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# IVLE - Informatics Virtual Learning Environment
2
 
# Copyright (C) 2007-2009 The University of Melbourne
3
 
#
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; either version 2 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
 
 
18
 
# Author: Nick Chadwick
19
 
 
20
 
import datetime
21
 
 
22
 
import formencode
23
 
import formencode.validators
24
 
 
25
 
import ivle.database
26
 
from ivle.database import ProjectSet, Project, Subject, Semester, Offering
27
 
 
28
 
from ivle.webapp.base.forms import VALID_URL_NAME
29
 
from ivle.webapp.base.rest import (XHTMLRESTView, named_operation,
30
 
                                   require_permission)
31
 
from ivle.webapp.errors import NotFound, BadRequest
32
 
 
33
 
class ProjectSetRESTView(XHTMLRESTView):
34
 
    """Rest view for a projectset.
35
 
    
36
 
    Add new, delete, edit functionality is given here."""
37
 
    template = "templates/projectset_fragment.html"
38
 
 
39
 
    def _project_url(self, project):
40
 
        return "/subjects/%s/%s/%s/+projects/%s" % \
41
 
                (self.context.offering.subject.short_name,
42
 
                 self.context.offering.semester.year,
43
 
                 self.context.offering.semester.semester,
44
 
                 project.short_name)
45
 
 
46
 
    @named_operation('edit')
47
 
    def add_project(self, req, name, short_name, deadline, synopsis, url):
48
 
        """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
 
        new_project = Project()
70
 
        new_project.name = unicode(name)
71
 
        new_project.short_name = unicode(short_name)
72
 
        new_project.url = unicode(url)
73
 
        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
 
        new_project.project_set = self.context
79
 
 
80
 
        req.store.add(new_project)
81
 
        req.store.flush()
82
 
    
83
 
        self.template = "templates/project_fragment.html"
84
 
        self.ctx['req'] = req
85
 
        self.ctx['project'] = new_project
86
 
 
87
 
        return {'success': True, 'projectset_id': self.context.id}