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

« back to all changes in this revision

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

  • Committer: Matt Giuca
  • Date: 2009-12-08 12:01:31 UTC
  • Revision ID: matt.giuca@gmail.com-20091208120131-1mz8aj9t2w56ij6y
Sample data: Added a new user, Terry Tutor, enrolled as a tutor in one of the subject, to test tutor permissions as distinct from lecturer permissions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# IVLE
 
2
# Copyright (C) 2007-2008 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
# App: subjects
 
19
# Author: Matt Giuca
 
20
# Date: 29/2/2008
 
21
 
 
22
# This is an IVLE application.
 
23
# A sample / testing application for IVLE.
 
24
 
 
25
import os
 
26
import os.path
 
27
import urllib
 
28
import urlparse
 
29
import cgi
 
30
 
 
31
from storm.locals import Desc, Store
 
32
import genshi
 
33
from genshi.filters import HTMLFormFiller
 
34
from genshi.template import Context, TemplateLoader
 
35
import formencode
 
36
 
 
37
from ivle.webapp.base.xhtml import XHTMLView
 
38
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
 
39
from ivle.webapp import ApplicationRoot
 
40
 
 
41
from ivle.database import Subject, Semester, Offering, Enrolment, User,\
 
42
                          ProjectSet, Project, ProjectSubmission
 
43
from ivle import util
 
44
import ivle.date
 
45
 
 
46
from ivle.webapp.admin.projectservice import ProjectSetRESTView,\
 
47
                                             ProjectRESTView
 
48
from ivle.webapp.admin.offeringservice import OfferingRESTView
 
49
from ivle.webapp.admin.publishing import (root_to_subject,
 
50
            subject_to_offering, offering_to_projectset, offering_to_project,
 
51
            subject_url, offering_url, projectset_url, project_url)
 
52
from ivle.webapp.admin.breadcrumbs import (SubjectBreadcrumb,
 
53
            OfferingBreadcrumb, UserBreadcrumb, ProjectBreadcrumb)
 
54
from ivle.webapp.groups import GroupsView
 
55
 
 
56
class SubjectsView(XHTMLView):
 
57
    '''The view of the list of subjects.'''
 
58
    template = 'templates/subjects.html'
 
59
    tab = 'subjects'
 
60
 
 
61
    def authorize(self, req):
 
62
        return req.user is not None
 
63
 
 
64
    def populate(self, req, ctx):
 
65
        ctx['user'] = req.user
 
66
        ctx['semesters'] = []
 
67
        for semester in req.store.find(Semester).order_by(Desc(Semester.year),
 
68
                                                     Desc(Semester.semester)):
 
69
            if req.user.admin:
 
70
                # For admins, show all subjects in the system
 
71
                offerings = list(semester.offerings.find())
 
72
            else:
 
73
                offerings = [enrolment.offering for enrolment in
 
74
                                    semester.enrolments.find(user=req.user)]
 
75
            if len(offerings):
 
76
                ctx['semesters'].append((semester, offerings))
 
77
 
 
78
 
 
79
class UserValidator(formencode.FancyValidator):
 
80
    """A FormEncode validator that turns a username into a user.
 
81
 
 
82
    The state must have a 'store' attribute, which is the Storm store
 
83
    to use."""
 
84
    def _to_python(self, value, state):
 
85
        user = User.get_by_login(state.store, value)
 
86
        if user:
 
87
            return user
 
88
        else:
 
89
            raise formencode.Invalid('User does not exist', value, state)
 
90
 
 
91
 
 
92
class NoEnrolmentValidator(formencode.FancyValidator):
 
93
    """A FormEncode validator that ensures absence of an enrolment.
 
94
 
 
95
    The state must have an 'offering' attribute.
 
96
    """
 
97
    def _to_python(self, value, state):
 
98
        if state.offering.get_enrolment(value):
 
99
            raise formencode.Invalid('User already enrolled', value, state)
 
100
        return value
 
101
 
 
102
 
 
103
class RoleEnrolmentValidator(formencode.FancyValidator):
 
104
    """A FormEncode validator that checks permission to enrol users with a
 
105
    particular role.
 
106
 
 
107
    The state must have an 'offering' attribute.
 
108
    """
 
109
    def _to_python(self, value, state):
 
110
        if ("enrol_" + value) not in state.offering.get_permissions(state.user):
 
111
            raise formencode.Invalid('Not allowed to assign users that role',
 
112
                                     value, state)
 
113
        return value
 
114
 
 
115
 
 
116
class EnrolSchema(formencode.Schema):
 
117
    user = formencode.All(NoEnrolmentValidator(), UserValidator())
 
118
    role = formencode.All(formencode.validators.OneOf(
 
119
                                ["lecturer", "tutor", "student"]),
 
120
                          RoleEnrolmentValidator(),
 
121
                          formencode.validators.UnicodeString())
 
122
 
 
123
 
 
124
class EnrolmentsView(XHTMLView):
 
125
    """A page which displays all users enrolled in an offering."""
 
126
    template = 'templates/enrolments.html'
 
127
    permission = 'edit'
 
128
 
 
129
    def populate(self, req, ctx):
 
130
        ctx['offering'] = self.context
 
131
 
 
132
class EnrolView(XHTMLView):
 
133
    """A form to enrol a user in an offering."""
 
134
    template = 'templates/enrol.html'
 
135
    tab = 'subjects'
 
136
    permission = 'enrol'
 
137
 
 
138
    def filter(self, stream, ctx):
 
139
        return stream | HTMLFormFiller(data=ctx['data'])
 
140
 
 
141
    def populate(self, req, ctx):
 
142
        if req.method == 'POST':
 
143
            data = dict(req.get_fieldstorage())
 
144
            try:
 
145
                validator = EnrolSchema()
 
146
                req.offering = self.context # XXX: Getting into state.
 
147
                data = validator.to_python(data, state=req)
 
148
                self.context.enrol(data['user'], data['role'])
 
149
                req.store.commit()
 
150
                req.throw_redirect(req.uri)
 
151
            except formencode.Invalid, e:
 
152
                errors = e.unpack_errors()
 
153
        else:
 
154
            data = {}
 
155
            errors = {}
 
156
 
 
157
        ctx['data'] = data or {}
 
158
        ctx['offering'] = self.context
 
159
        ctx['roles_auth'] = self.context.get_permissions(req.user)
 
160
        ctx['errors'] = errors
 
161
 
 
162
class OfferingProjectsView(XHTMLView):
 
163
    """View the projects for an offering."""
 
164
    template = 'templates/offering_projects.html'
 
165
    permission = 'edit'
 
166
    tab = 'subjects'
 
167
 
 
168
    def populate(self, req, ctx):
 
169
        self.plugin_styles[Plugin] = ["project.css"]
 
170
        self.plugin_scripts[Plugin] = ["project.js"]
 
171
        ctx['req'] = req
 
172
        ctx['offering'] = self.context
 
173
        ctx['projectsets'] = []
 
174
        ctx['OfferingRESTView'] = OfferingRESTView
 
175
 
 
176
        #Open the projectset Fragment, and render it for inclusion
 
177
        #into the ProjectSets page
 
178
        #XXX: This could be a lot cleaner
 
179
        loader = genshi.template.TemplateLoader(".", auto_reload=True)
 
180
 
 
181
        set_fragment = os.path.join(os.path.dirname(__file__),
 
182
                "templates/projectset_fragment.html")
 
183
        project_fragment = os.path.join(os.path.dirname(__file__),
 
184
                "templates/project_fragment.html")
 
185
 
 
186
        for projectset in self.context.project_sets:
 
187
            settmpl = loader.load(set_fragment)
 
188
            setCtx = Context()
 
189
            setCtx['req'] = req
 
190
            setCtx['projectset'] = projectset
 
191
            setCtx['projects'] = []
 
192
            setCtx['GroupsView'] = GroupsView
 
193
            setCtx['ProjectSetRESTView'] = ProjectSetRESTView
 
194
 
 
195
            for project in projectset.projects:
 
196
                projecttmpl = loader.load(project_fragment)
 
197
                projectCtx = Context()
 
198
                projectCtx['req'] = req
 
199
                projectCtx['project'] = project
 
200
 
 
201
                setCtx['projects'].append(
 
202
                        projecttmpl.generate(projectCtx))
 
203
 
 
204
            ctx['projectsets'].append(settmpl.generate(setCtx))
 
205
 
 
206
 
 
207
class ProjectView(XHTMLView):
 
208
    """View the submissions for a ProjectSet"""
 
209
    template = "templates/project.html"
 
210
    permission = "edit"
 
211
    tab = 'subjects'
 
212
 
 
213
    def build_subversion_url(self, svnroot, submission):
 
214
        princ = submission.assessed.principal
 
215
 
 
216
        if isinstance(princ, User):
 
217
            path = 'users/%s' % princ.login
 
218
        else:
 
219
            path = 'groups/%s_%s_%s_%s' % (
 
220
                    princ.project_set.offering.subject.short_name,
 
221
                    princ.project_set.offering.semester.year,
 
222
                    princ.project_set.offering.semester.semester,
 
223
                    princ.name
 
224
                    )
 
225
        return urlparse.urljoin(
 
226
                    svnroot,
 
227
                    os.path.join(path, submission.path[1:] if
 
228
                                       submission.path.startswith(os.sep) else
 
229
                                       submission.path))
 
230
 
 
231
    def populate(self, req, ctx):
 
232
        self.plugin_styles[Plugin] = ["project.css"]
 
233
 
 
234
        ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
 
235
        ctx['build_subversion_url'] = self.build_subversion_url
 
236
        ctx['svn_addr'] = req.config['urls']['svn_addr']
 
237
        ctx['project'] = self.context
 
238
        ctx['user'] = req.user
 
239
 
 
240
class OfferingEnrolmentSet(object):
 
241
    def __init__(self, offering):
 
242
        self.offering = offering
 
243
 
 
244
class Plugin(ViewPlugin, MediaPlugin):
 
245
    forward_routes = (root_to_subject, subject_to_offering,
 
246
                      offering_to_project, offering_to_projectset)
 
247
    reverse_routes = (subject_url, offering_url, projectset_url, project_url)
 
248
 
 
249
    views = [(ApplicationRoot, ('subjects', '+index'), SubjectsView),
 
250
             (Offering, ('+enrolments', '+index'), EnrolmentsView),
 
251
             (Offering, ('+enrolments', '+new'), EnrolView),
 
252
             (Offering, ('+projects', '+index'), OfferingProjectsView),
 
253
             (Project, '+index', ProjectView),
 
254
 
 
255
             (Offering, ('+projectsets', '+new'), OfferingRESTView, 'api'),
 
256
             (ProjectSet, ('+projects', '+new'), ProjectSetRESTView, 'api'),
 
257
             (Project, '+index', ProjectRESTView, 'api'),
 
258
             ]
 
259
 
 
260
    breadcrumbs = {Subject: SubjectBreadcrumb,
 
261
                   Offering: OfferingBreadcrumb,
 
262
                   User: UserBreadcrumb,
 
263
                   Project: ProjectBreadcrumb,
 
264
                   }
 
265
 
 
266
    tabs = [
 
267
        ('subjects', 'Subjects',
 
268
         'View subject content and complete worksheets',
 
269
         'subjects.png', 'subjects', 5)
 
270
    ]
 
271
 
 
272
    media = 'subject-media'