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

« back to all changes in this revision

Viewing changes to ivle/webapp/tutorial/service.py

  • Committer: William Grant
  • Date: 2009-03-26 05:33:03 UTC
  • mto: (1165.3.1 submissions)
  • mto: This revision was merged to the branch mainline in revision 1174.
  • Revision ID: grantw@unimelb.edu.au-20090326053303-t1wsjswhk2sl2gml
Start a submission UI in ivle.webapp.submit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import ivle.database
29
29
from ivle.database import Exercise, ExerciseAttempt, ExerciseSave, Worksheet, \
30
30
                          Offering, Subject, Semester, WorksheetExercise
31
 
import ivle.worksheet
 
31
import ivle.worksheet.utils
32
32
import ivle.conf
33
33
import ivle.webapp.tutorial.test
34
 
 
35
34
from ivle.webapp.base.rest import (JSONRESTView, named_operation,
36
35
                                   require_permission)
37
36
from ivle.webapp.errors import NotFound
52
51
            raise NotFound()
53
52
        
54
53
        self.worksheet_exercise = req.store.find(WorksheetExercise,
55
 
            WorksheetExercise.exercise_id == exercise,
 
54
            WorksheetExercise.exercise_id == unicode(exercise),
56
55
            WorksheetExercise.worksheet_id == Worksheet.id,
57
56
            Worksheet.offering_id == Offering.id,
 
57
            Worksheet.identifier == unicode(worksheet),
58
58
            Offering.subject_id == Subject.id,
59
 
            Subject.code == subject,
 
59
            Subject.short_name == subject,
60
60
            Offering.semester_id == Semester.id,
61
61
            Semester.year == year,
62
62
            Semester.semester == semester).one()
113
113
        # Query the DB to get an updated score on whether or not this problem
114
114
        # has EVER been completed (may be different from "passed", if it has
115
115
        # been completed before), and the total number of attempts.
116
 
        completed, attempts = ivle.worksheet.get_exercise_status(req.store,
117
 
            req.user, self.worksheet_exercise)
 
116
        completed, attempts = ivle.worksheet.utils.get_exercise_status(
 
117
                req.store, req.user, self.worksheet_exercise)
118
118
        test_results["completed"] = completed
119
119
        test_results["attempts"] = attempts
120
120
 
136
136
        except ValueError:
137
137
            raise NotFound()
138
138
 
 
139
        # XXX Hack around Google Code issue #87
 
140
        # Query from the given date +1 secnod.
 
141
        # Date is in seconds (eg. 3:47:12), while the data is in finer time
 
142
        # (eg. 3:47:12.3625). The query "date <= 3:47:12" will fail because
 
143
        # 3:47:12.3625 is greater. Hence we do the query from +1 second,
 
144
        # "date <= 3:47:13", and it finds the correct submission, UNLESS there
 
145
        # are multiple submissions inside the same second.
 
146
        date += datetime.timedelta(seconds=1)
 
147
 
139
148
        worksheet_exercise = req.store.find(WorksheetExercise,
140
149
            WorksheetExercise.exercise_id == exercise,
141
150
            WorksheetExercise.worksheet_id == Worksheet.id,
142
151
            Worksheet.identifier == worksheet,
143
152
            Worksheet.offering_id == Offering.id,
144
153
            Offering.subject_id == Subject.id,
145
 
            Subject.code == subject,
 
154
            Subject.short_name == subject,
146
155
            Offering.semester_id == Semester.id,
147
156
            Semester.year == year,
148
157
            Semester.semester == semester).one()
149
158
            
150
 
        attempt = ivle.worksheet.get_exercise_attempt(req.store, user,
 
159
        attempt = ivle.worksheet.utils.get_exercise_attempt(req.store, user,
151
160
                        worksheet_exercise, as_of=date,
152
161
                        allow_inactive=HISTORY_ALLOW_INACTIVE) 
153
162
 
161
170
        return {'code': self.context.text}
162
171
 
163
172
 
164
 
class ExerciseRESTView(JSONRESTView):
165
 
    '''REST view of an exercise.'''
166
 
 
167
 
    def get_permissions(self, user):
168
 
        # XXX: Do it properly.
169
 
        # XXX: Does any user have the ability to save as themselves?
170
 
        # XXX: Does a user EVER have permission to save as another user?
171
 
        if user is not None:
172
 
            return set(['save'])
173
 
        else:
174
 
            return set()
175
 
 
176
 
    @named_operation('save')
 
173
class WorksheetExerciseRESTView(JSONRESTView):
 
174
    '''REST view of a worksheet exercise.'''
 
175
 
 
176
    def __init__(self, req, subject, year, semester, worksheet, exercise):
 
177
        self.context = req.store.find(WorksheetExercise,
 
178
            WorksheetExercise.exercise_id == exercise,
 
179
            WorksheetExercise.worksheet_id == Worksheet.id,
 
180
            Worksheet.offering_id == Offering.id,
 
181
            Offering.subject_id == Subject.id,
 
182
            Subject.short_name == subject,
 
183
            Offering.semester_id == Semester.id,
 
184
            Semester.year == year,
 
185
            Semester.semester == semester).one()
 
186
        
 
187
        if self.context is None:
 
188
            raise NotFound()
 
189
 
 
190
    @named_operation('view')
177
191
    def save(self, req, text):
178
192
        # Find the appropriate WorksheetExercise to save to. If its not found,
179
193
        # the user is submitting against a non-existant worksheet/exercise
180
 
        worksheet_exercise = req.store.find(WorksheetExercise,
181
 
            WorksheetExercise.exercise_id == self.exercise,
182
 
            WorksheetExercise.worksheet_id == Worksheet.id,
183
 
            Worksheet.offering_id == Offering.id,
184
 
            Offering.subject_id == Subject.id,
185
 
            Subject.code == self.subject,
186
 
            Offering.semester_id == Semester.id,
187
 
            Semester.year == self.year,
188
 
            Semester.semester == self.semester).one()
189
 
        
190
 
        if worksheet_exercise is None:
191
 
            raise NotFound()
192
194
 
193
195
        old_save = req.store.find(ExerciseSave,
194
 
            ExerciseSave.ws_ex_id == worksheet_exercise.id,
 
196
            ExerciseSave.ws_ex_id == self.context.id,
195
197
            ExerciseSave.user == req.user).one()
196
198
        
197
199
        #Overwrite the old, or create a new if there isn't one
201
203
        else:
202
204
            new_save = old_save
203
205
        
204
 
        new_save.worksheet_exercise = worksheet_exercise
 
206
        new_save.worksheet_exercise = self.context
205
207
        new_save.user = req.user
206
208
        new_save.text = unicode(text)
207
209
        new_save.date = datetime.datetime.now()
209
211
        return {"result": "ok"}
210
212
 
211
213
 
212
 
 
213
214
# Note that this is the view of an existing worksheet. Creation is handled
214
215
# by OfferingRESTView (as offerings have worksheets)
215
216
class WorksheetRESTView(JSONRESTView):
216
217
    """View used to update a worksheet."""
217
218
 
218
 
    def get_permissions(self, user):
219
 
        # XXX: Do it properly.
220
 
        # XXX: Lecturers should be allowed to add worksheets Only to subjects
221
 
        #      under their control
222
 
        if user is not None:
223
 
            if user.admin:
224
 
                return set(['save'])
225
 
            else:
226
 
                return set()
227
 
        else:
228
 
            return set()    
229
 
 
230
219
    def __init__(self, req, **kwargs):
231
220
    
232
221
        self.worksheet = kwargs['worksheet']
238
227
            Worksheet.identifier == self.worksheet,
239
228
            Worksheet.offering_id == Offering.id,
240
229
            Offering.subject_id == Subject.id,
241
 
            Subject.code == self.subject,
 
230
            Subject.short_name == self.subject,
242
231
            Offering.semester_id == Semester.id,
243
232
            Semester.year == self.year,
244
233
            Semester.semester == self.semester).one()
246
235
        if self.context is None:
247
236
            raise NotFound()
248
237
    
249
 
    @named_operation('save')
 
238
    @named_operation('edit')
250
239
    def save(self, req, name, assessable, data, format):
251
240
        """Takes worksheet data and saves it."""
252
241
        self.context.name = unicode(name)
253
242
        self.context.assessable = self.convert_bool(assessable)
254
243
        self.context.data = unicode(data)
255
244
        self.context.format = unicode(format)
256
 
        ivle.worksheet.update_exerciselist(self.context)
 
245
        ivle.worksheet.utils.update_exerciselist(self.context)
257
246
        
258
247
        return {"result": "ok"}
259
248
 
260
249
class WorksheetsRESTView(JSONRESTView):
261
250
    """View used to update and create Worksheets."""
262
251
    
263
 
    def get_permissions(self, user):
264
 
        # XXX: Do it properly.
265
 
        # XXX: Lecturers should be allowed to add worksheets Only to subjects
266
 
        #      under their control
267
 
        if user is not None:
268
 
            if user.admin:
269
 
                return set(['edit'])
270
 
            else:
271
 
                return set()
272
 
        else:
273
 
            return set()
274
 
 
275
252
    def __init__(self, req, **kwargs):
276
253
    
277
254
        self.subject = kwargs['subject']
280
257
    
281
258
        self.context = req.store.find(Offering,
282
259
            Offering.subject_id == Subject.id,
283
 
            Subject.code == self.subject,
 
260
            Subject.short_name == self.subject,
284
261
            Offering.semester_id == Semester.id,
285
262
            Semester.year == self.year,
286
263
            Semester.semester == self.semester).one()
305
282
        
306
283
        # This call is added for clarity, as the worksheet is implicitly added.        
307
284
        req.store.add(new_worksheet)
308
 
        
309
 
        ivle.worksheet.update_exerciselist(new_worksheet)
310
 
        
 
285
 
 
286
        ivle.worksheet.utils.update_exerciselist(new_worksheet)
 
287
 
311
288
        return {"result": "ok"}
312
289
 
313
290
    @named_operation('edit')