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

1165.2.3 by Nick Chadwick
Added a new Admin view, which allows for the administration of projects
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
1316.1.2 by David Coles
Allow specification of a deadline when creating new projects.
20
import datetime
1165.2.3 by Nick Chadwick
Added a new Admin view, which allows for the administration of projects
21
import ivle.database
1165.3.2 by Nick Chadwick
Created a new view for IVLE, allowing lecturers and tutors to
22
from ivle.database import ProjectSet, Project, Subject, Semester, Offering
1165.2.3 by Nick Chadwick
Added a new Admin view, which allows for the administration of projects
23
1606.1.6 by William Grant
Project addition now fails prettily and descriptively.
24
from ivle.webapp.base.forms import VALID_URL_NAME
1165.2.3 by Nick Chadwick
Added a new Admin view, which allows for the administration of projects
25
from ivle.webapp.base.rest import (XHTMLRESTView, named_operation,
26
                                   require_permission)
1316.1.2 by David Coles
Allow specification of a deadline when creating new projects.
27
from ivle.webapp.errors import NotFound, BadRequest
1165.2.3 by Nick Chadwick
Added a new Admin view, which allows for the administration of projects
28
1165.3.2 by Nick Chadwick
Created a new view for IVLE, allowing lecturers and tutors to
29
class ProjectSetRESTView(XHTMLRESTView):
30
    """Rest view for a projectset.
1165.2.3 by Nick Chadwick
Added a new Admin view, which allows for the administration of projects
31
    
32
    Add new, delete, edit functionality is given here."""
1165.3.2 by Nick Chadwick
Created a new view for IVLE, allowing lecturers and tutors to
33
    template = "templates/projectset_fragment.html"
34
35
    def _project_url(self, project):
1165.3.15 by William Grant
Remove the ProjectSet from Project URLs.
36
        return "/subjects/%s/%s/%s/+projects/%s" % \
37
                (self.context.offering.subject.short_name,
1165.3.11 by Nick Chadwick
Fixed a broken link when adding a new project.
38
                 self.context.offering.semester.year,
39
                 self.context.offering.semester.semester,
40
                 project.short_name)
1165.3.2 by Nick Chadwick
Created a new view for IVLE, allowing lecturers and tutors to
41
42
    @named_operation('edit')
1316.1.2 by David Coles
Allow specification of a deadline when creating new projects.
43
    def add_project(self, req, name, short_name, deadline, synopsis):
1165.3.2 by Nick Chadwick
Created a new view for IVLE, allowing lecturers and tutors to
44
        """Add a Project to this ProjectSet"""
1606.1.6 by William Grant
Project addition now fails prettily and descriptively.
45
        if not VALID_URL_NAME.match(short_name):
46
            raise BadRequest(
47
                "Project names must consist of an alphanumeric character "
48
                "followed by any number of alphanumerics, ., + or -.")
49
1606.1.8 by William Grant
Fail nicely if a project with the same name already exists.
50
        if req.store.find(
51
            Project,
52
            Project.short_name == unicode(short_name),
53
            Project.project_set_id == ProjectSet.id,
54
            ProjectSet.offering == self.context.offering).one():
55
            raise BadRequest(
56
                "A project with that URL name already exists in this offering."
57
                )
58
1165.3.2 by Nick Chadwick
Created a new view for IVLE, allowing lecturers and tutors to
59
        new_project = Project()
60
        new_project.name = unicode(name)
61
        new_project.short_name = unicode(short_name)
62
        new_project.synopsis = unicode(synopsis)
1316.1.2 by David Coles
Allow specification of a deadline when creating new projects.
63
        try:
64
            new_project.deadline = datetime.datetime.strptime(deadline, '%Y-%m-%d %H:%M:%S')
65
        except ValueError:
66
            raise BadRequest("deadline must be in YYYY-MM-DD HH:MM:ss")
1165.3.2 by Nick Chadwick
Created a new view for IVLE, allowing lecturers and tutors to
67
        new_project.project_set = self.context
68
69
        req.store.add(new_project)
70
        req.store.flush()
1165.2.3 by Nick Chadwick
Added a new Admin view, which allows for the administration of projects
71
    
1165.3.2 by Nick Chadwick
Created a new view for IVLE, allowing lecturers and tutors to
72
        self.template = "templates/project_fragment.html"
1358 by William Grant
Use the publishing framework to generate URLs to projectsets.
73
        self.ctx['req'] = req
1165.3.2 by Nick Chadwick
Created a new view for IVLE, allowing lecturers and tutors to
74
        self.ctx['project'] = new_project
75
76
        return {'success': True, 'projectset_id': self.context.id}