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

« back to all changes in this revision

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

ivle.webapp.console.service#restart_console: Don't double-JSON.

Show diffs side-by-side

added added

removed removed

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