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

1165.1.8 by William Grant
Start a submission UI in ivle.webapp.submit.
1
# IVLE
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: Will Grant
19
20
"""Project submissions user interface."""
21
1165.1.13 by William Grant
Add an initial project selection UI to SubmitView.
22
import os.path
1165.1.17 by William Grant
Disable project radio buttons if they are closed.
23
import datetime
1165.1.13 by William Grant
Add an initial project selection UI to SubmitView.
24
1165.1.22 by William Grant
Detect the permitted offering for the given submission URL and restrict to that.
25
from storm.locals import Store
26
1165.1.8 by William Grant
Start a submission UI in ivle.webapp.submit.
27
from ivle.database import (User, ProjectGroup, Offering, Subject, Semester,
1165.1.22 by William Grant
Detect the permitted offering for the given submission URL and restrict to that.
28
                           ProjectSet, Project, Enrolment)
1165.1.20 by William Grant
Add a POST handler to SubmitView, which does the actual submission.
29
from ivle.webapp.errors import NotFound, BadRequest
1165.1.8 by William Grant
Start a submission UI in ivle.webapp.submit.
30
from ivle.webapp.base.xhtml import XHTMLView
31
from ivle.webapp.base.plugins import ViewPlugin
32
1165.1.16 by William Grant
Show the deadline for each project when submitting.
33
import ivle.date
34
1165.1.8 by William Grant
Start a submission UI in ivle.webapp.submit.
35
36
class SubmitView(XHTMLView):
37
    """A view to submit a Subversion repository path for a project."""
38
    template = 'submit.html'
39
    tab = 'files'
40
    permission = 'submit_project'
41
1180 by Matt Giuca
submit: Fixed unicode and other bugs when submitting the empty path or "/".
42
    def __init__(self, req, name, path=u"/"):
1165.1.8 by William Grant
Start a submission UI in ivle.webapp.submit.
43
        # We need to work out which entity owns the repository, so we look
44
        # at the first two path segments. The first tells us the type.
1165.1.9 by William Grant
Split SubmitView.__init__ into new subclasses, {User,Group}SubmitView.
45
        self.context = self.get_repository_owner(req.store, name)
1244 by William Grant
Ensure that the path is absolute when submitting a project.
46
47
        # Ensure that the path is absolute (required for SVNAuthzFile).
1180 by Matt Giuca
submit: Fixed unicode and other bugs when submitting the empty path or "/".
48
        # XXX Re-convert to unicode (os.path.normpath(u"/") returns a str).
1244 by William Grant
Ensure that the path is absolute when submitting a project.
49
        self.path = os.path.join(u"/", unicode(os.path.normpath(path)))
1165.1.8 by William Grant
Start a submission UI in ivle.webapp.submit.
50
51
        if self.context is None:
52
            raise NotFound()
53
1165.1.28 by William Grant
Don't crash when the principal cannot be determined.
54
        self.offering = self.get_offering()
55
1165.1.9 by William Grant
Split SubmitView.__init__ into new subclasses, {User,Group}SubmitView.
56
    def get_repository_owner(self, store, name):
57
        """Return the owner of the repository given the name and a Store."""
58
        raise NotImplementedError()
59
1165.1.22 by William Grant
Detect the permitted offering for the given submission URL and restrict to that.
60
    def get_offering(self):
61
        """Return the offering that this path can be submitted to."""
62
        raise NotImplementedError()
63
1165.1.8 by William Grant
Start a submission UI in ivle.webapp.submit.
64
    def populate(self, req, ctx):
1165.1.20 by William Grant
Add a POST handler to SubmitView, which does the actual submission.
65
        if req.method == 'POST':
66
            data = dict(req.get_fieldstorage())
1165.1.25 by William Grant
Take the revision to submit from the query string.
67
            if 'revision' not in data:
1165.1.41 by William Grant
Give more human-readable 400 messages for submission.
68
                raise BadRequest('No revision selected.')
1165.1.25 by William Grant
Take the revision to submit from the query string.
69
70
            try:
71
                revision = int(data['revision'])
72
            except ValueError:
1165.1.41 by William Grant
Give more human-readable 400 messages for submission.
73
                raise BadRequest('Revision must be an integer.')
1165.1.25 by William Grant
Take the revision to submit from the query string.
74
1165.1.20 by William Grant
Add a POST handler to SubmitView, which does the actual submission.
75
            if 'project' not in data:
1165.1.41 by William Grant
Give more human-readable 400 messages for submission.
76
                raise BadRequest('No project selected.')
1165.1.20 by William Grant
Add a POST handler to SubmitView, which does the actual submission.
77
78
            try:
79
                projectid = int(data['project'])
80
            except ValueError:
1165.1.41 by William Grant
Give more human-readable 400 messages for submission.
81
                raise BadRequest('Project must be an integer.')
1165.1.20 by William Grant
Add a POST handler to SubmitView, which does the actual submission.
82
83
            project = req.store.find(Project, Project.id == projectid).one()
84
1165.1.24 by William Grant
400 if a non-matching project is POSTed.
85
            # This view's offering will be the sole offering for which the
86
            # path is permissible. We need to check that.
87
            if project.project_set.offering is not self.offering:
88
                raise BadRequest('Path is not permissible for this offering')
89
1165.1.20 by William Grant
Add a POST handler to SubmitView, which does the actual submission.
90
            if project is None:
91
                raise BadRequest('Specified project does not exist')
92
1165.1.42 by William Grant
Record who submitted each submission.
93
            project.submit(self.context, self.path, revision, req.user)
1165.1.20 by William Grant
Add a POST handler to SubmitView, which does the actual submission.
94
1165.1.43 by William Grant
Display an informative view on submission success.
95
            self.template = 'submitted.html'
96
            ctx['project'] = project
97
1165.1.27 by William Grant
Make the submit view significantly more readable.
98
        ctx['req'] = req
1165.1.13 by William Grant
Add an initial project selection UI to SubmitView.
99
        ctx['principal'] = self.context
1165.1.24 by William Grant
400 if a non-matching project is POSTed.
100
        ctx['offering'] = self.offering
1165.1.13 by William Grant
Add an initial project selection UI to SubmitView.
101
        ctx['path'] = self.path
1165.1.17 by William Grant
Disable project radio buttons if they are closed.
102
        ctx['now'] = datetime.datetime.now()
1179 by Matt Giuca
submit: Added tooltip for the due date (which displays the whole date, while
103
        ctx['format_datetime'] = ivle.date.make_date_nice
104
        ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
1165.1.8 by William Grant
Start a submission UI in ivle.webapp.submit.
105
1165.1.9 by William Grant
Split SubmitView.__init__ into new subclasses, {User,Group}SubmitView.
106
107
class UserSubmitView(SubmitView):
108
    def get_repository_owner(self, store, name):
109
        '''Resolve the user name into a user.'''
110
        return User.get_by_login(store, name)
111
1165.1.22 by William Grant
Detect the permitted offering for the given submission URL and restrict to that.
112
    def get_offering(self):
113
        return Store.of(self.context).find(Offering,
114
            Offering.id == Enrolment.offering_id,
115
            Enrolment.user_id == self.context.id,
116
            Offering.semester_id == Semester.id,
117
            Semester.state == u'current',
118
            Offering.subject_id == Subject.id,
1244 by William Grant
Ensure that the path is absolute when submitting a project.
119
            Subject.short_name == self.path.split('/')[1],
1165.1.22 by William Grant
Detect the permitted offering for the given submission URL and restrict to that.
120
            ).one()
121
1165.1.9 by William Grant
Split SubmitView.__init__ into new subclasses, {User,Group}SubmitView.
122
123
class GroupSubmitView(SubmitView):
124
    def get_repository_owner(self, store, name):
125
        '''Resolve the subject_year_semester_group name into a group.'''
126
        namebits = name.split('_', 3)
127
        if len(namebits) != 4:
128
            return None
129
130
        # Find the project group with the given name in any project set in the
131
        # offering of the given subject in the semester with the given year
132
        # and semester.
133
        return store.find(ProjectGroup,
134
            ProjectGroup.name == namebits[3],
135
            ProjectGroup.project_set_id == ProjectSet.id,
136
            ProjectSet.offering_id == Offering.id,
137
            Offering.subject_id == Subject.id,
138
            Subject.short_name == namebits[0],
139
            Offering.semester_id == Semester.id,
140
            Semester.year == namebits[1],
141
            Semester.semester == namebits[2]).one()
142
1165.1.22 by William Grant
Detect the permitted offering for the given submission URL and restrict to that.
143
    def get_offering(self):
144
        return self.context.project_set.offering
145
1165.1.9 by William Grant
Split SubmitView.__init__ into new subclasses, {User,Group}SubmitView.
146
1165.1.8 by William Grant
Start a submission UI in ivle.webapp.submit.
147
class Plugin(ViewPlugin):
148
    urls = [
1176 by Matt Giuca
submit: Fixed URL matching (now allows a path without a trailing '/').
149
        ('+submit/users/:name', UserSubmitView),
150
        ('+submit/groups/:name', GroupSubmitView),
1165.1.9 by William Grant
Split SubmitView.__init__ into new subclasses, {User,Group}SubmitView.
151
        ('+submit/users/:name/*path', UserSubmitView),
152
        ('+submit/groups/:name/*path', GroupSubmitView),
1165.1.8 by William Grant
Start a submission UI in ivle.webapp.submit.
153
    ]