~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-04-28 07:08:56 UTC
  • Revision ID: grantw@unimelb.edu.au-20090428070856-75yc00g6ea24qfqz
Drop ivle.conf.{subjects,exercises}_base - they were unused.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import datetime
24
24
import genshi
25
25
 
26
 
import ivle.util
27
26
import ivle.console
28
27
import ivle.database
29
28
from ivle.database import Exercise, ExerciseAttempt, ExerciseSave, Worksheet, \
30
29
                          Offering, Subject, Semester, WorksheetExercise
31
 
import ivle.worksheet
 
30
import ivle.worksheet.utils
32
31
import ivle.conf
33
32
import ivle.webapp.tutorial.test
34
 
 
35
33
from ivle.webapp.base.rest import (JSONRESTView, named_operation,
36
34
                                   require_permission)
37
35
from ivle.webapp.errors import NotFound
52
50
            raise NotFound()
53
51
        
54
52
        self.worksheet_exercise = req.store.find(WorksheetExercise,
55
 
            WorksheetExercise.exercise_id == exercise,
 
53
            WorksheetExercise.exercise_id == unicode(exercise),
56
54
            WorksheetExercise.worksheet_id == Worksheet.id,
57
55
            Worksheet.offering_id == Offering.id,
 
56
            Worksheet.identifier == unicode(worksheet),
58
57
            Offering.subject_id == Subject.id,
59
 
            Subject.code == subject,
 
58
            Subject.short_name == subject,
60
59
            Offering.semester_id == Semester.id,
61
60
            Semester.year == year,
62
61
            Semester.semester == semester).one()
113
112
        # Query the DB to get an updated score on whether or not this problem
114
113
        # has EVER been completed (may be different from "passed", if it has
115
114
        # 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)
 
115
        completed, attempts = ivle.worksheet.utils.get_exercise_status(
 
116
                req.store, req.user, self.worksheet_exercise)
118
117
        test_results["completed"] = completed
119
118
        test_results["attempts"] = attempts
120
119
 
131
130
        if user is None:
132
131
            raise NotFound()
133
132
 
134
 
        #try:
135
 
        #    date = datetime.datetime.strptime(date, TIMESTAMP_FORMAT)
136
 
        #except ValueError:
137
 
        #    raise NotFound()
 
133
        try:
 
134
            date = datetime.datetime.strptime(date, TIMESTAMP_FORMAT)
 
135
        except ValueError:
 
136
            raise NotFound()
 
137
 
 
138
        # XXX Hack around Google Code issue #87
 
139
        # Query from the given date +1 secnod.
 
140
        # Date is in seconds (eg. 3:47:12), while the data is in finer time
 
141
        # (eg. 3:47:12.3625). The query "date <= 3:47:12" will fail because
 
142
        # 3:47:12.3625 is greater. Hence we do the query from +1 second,
 
143
        # "date <= 3:47:13", and it finds the correct submission, UNLESS there
 
144
        # are multiple submissions inside the same second.
 
145
        date += datetime.timedelta(seconds=1)
138
146
 
139
147
        worksheet_exercise = req.store.find(WorksheetExercise,
140
148
            WorksheetExercise.exercise_id == exercise,
142
150
            Worksheet.identifier == worksheet,
143
151
            Worksheet.offering_id == Offering.id,
144
152
            Offering.subject_id == Subject.id,
145
 
            Subject.code == subject,
 
153
            Subject.short_name == subject,
146
154
            Offering.semester_id == Semester.id,
147
155
            Semester.year == year,
148
156
            Semester.semester == semester).one()
149
157
            
150
 
        attempt = req.store.find(ExerciseAttempt,
151
 
            ExerciseAttempt.user_id == user.id,
152
 
            ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
153
 
            ExerciseAttempt.date == date
154
 
        ).one()
 
158
        attempt = ivle.worksheet.utils.get_exercise_attempt(req.store, user,
 
159
                        worksheet_exercise, as_of=date,
 
160
                        allow_inactive=HISTORY_ALLOW_INACTIVE) 
155
161
 
156
162
        if attempt is None:
157
163
            raise NotFound()
163
169
        return {'code': self.context.text}
164
170
 
165
171
 
166
 
class ExerciseRESTView(JSONRESTView):
167
 
    '''REST view of an exercise.'''
168
 
 
169
 
    def get_permissions(self, user):
170
 
        # XXX: Do it properly.
171
 
        # XXX: Does any user have the ability to save as themselves?
172
 
        # XXX: Does a user EVER have permission to save as another user?
173
 
        if user is not None:
174
 
            return set(['save'])
175
 
        else:
176
 
            return set()
177
 
 
178
 
    @named_operation('save')
 
172
class WorksheetExerciseRESTView(JSONRESTView):
 
173
    '''REST view of a worksheet exercise.'''
 
174
 
 
175
    def __init__(self, req, subject, year, semester, worksheet, exercise):
 
176
        self.context = req.store.find(WorksheetExercise,
 
177
            WorksheetExercise.exercise_id == exercise,
 
178
            WorksheetExercise.worksheet_id == Worksheet.id,
 
179
            Worksheet.offering_id == Offering.id,
 
180
            Offering.subject_id == Subject.id,
 
181
            Subject.short_name == subject,
 
182
            Offering.semester_id == Semester.id,
 
183
            Semester.year == year,
 
184
            Semester.semester == semester).one()
 
185
        
 
186
        if self.context is None:
 
187
            raise NotFound()
 
188
 
 
189
    @named_operation('view')
179
190
    def save(self, req, text):
180
191
        # Find the appropriate WorksheetExercise to save to. If its not found,
181
192
        # the user is submitting against a non-existant worksheet/exercise
182
 
        worksheet_exercise = req.store.find(WorksheetExercise,
183
 
            WorksheetExercise.exercise_id == self.exercise,
184
 
            WorksheetExercise.worksheet_id == Worksheet.id,
185
 
            Worksheet.offering_id == Offering.id,
186
 
            Offering.subject_id == Subject.id,
187
 
            Subject.code == self.subject,
188
 
            Offering.semester_id == Semester.id,
189
 
            Semester.year == self.year,
190
 
            Semester.semester == self.semester).one()
191
 
        
192
 
        if worksheet_exercise is None:
193
 
            raise NotFound()
194
193
 
195
194
        old_save = req.store.find(ExerciseSave,
196
 
            ExerciseSave.ws_ex_id == worksheet_exercise.id,
 
195
            ExerciseSave.ws_ex_id == self.context.id,
197
196
            ExerciseSave.user == req.user).one()
198
197
        
199
198
        #Overwrite the old, or create a new if there isn't one
203
202
        else:
204
203
            new_save = old_save
205
204
        
206
 
        new_save.worksheet_exercise = worksheet_exercise
 
205
        new_save.worksheet_exercise = self.context
207
206
        new_save.user = req.user
208
207
        new_save.text = unicode(text)
209
208
        new_save.date = datetime.datetime.now()
211
210
        return {"result": "ok"}
212
211
 
213
212
 
214
 
def generate_exerciselist(worksheet, req, worksheetdata):
215
 
    """Runs through the worksheetstream, generating the appropriate
216
 
    WorksheetExercises, and de-activating the old ones."""
217
 
    exercises = []
218
 
    # Turns the worksheet into an xml stream, and then finds all the 
219
 
    # exercise nodes in the stream.
220
 
    worksheetdata = genshi.XML(worksheetdata)
221
 
    for kind, data, pos in worksheetdata:
222
 
        if kind is genshi.core.START:
223
 
            # Data is a tuple of tag name and a list of name->value tuples
224
 
            if data[0] == 'exercise':
225
 
                src = ""
226
 
                optional = False
227
 
                for attr in data[1]:
228
 
                    if attr[0] == 'src':
229
 
                        src = attr[1]
230
 
                    if attr[0] == 'optional':
231
 
                        optional = attr[1] == 'true'
232
 
                if src != "":
233
 
                    exercises.append((src, optional))
234
 
    ex_num = 0
235
 
    # Set all current worksheet_exercises to be inactive
236
 
    db_worksheet_exercises = req.store.find(WorksheetExercise,
237
 
        WorksheetExercise.worksheet_id == worksheet.id)
238
 
    for worksheet_exercise in db_worksheet_exercises:
239
 
        worksheet_exercise.active = False
240
 
    
241
 
    for exerciseid, optional in exercises:
242
 
        worksheet_exercise = req.store.find(WorksheetExercise,
243
 
            WorksheetExercise.worksheet_id == worksheet.id,
244
 
            Exercise.id == WorksheetExercise.exercise_id,
245
 
            Exercise.id == exerciseid).one()
246
 
        if worksheet_exercise is None:
247
 
            exercise = req.store.find(Exercise,
248
 
                Exercise.id == exerciseid
249
 
            ).one()
250
 
            if exercise is None:
251
 
                raise NotFound()
252
 
            worksheet_exercise = WorksheetExercise()
253
 
            worksheet_exercise.worksheet_id = worksheet.id
254
 
            worksheet_exercise.exercise_id = exercise.id
255
 
            req.store.add(worksheet_exercise)
256
 
        worksheet_exercise.active = True
257
 
        worksheet_exercise.seq_no = ex_num
258
 
        worksheet_exercise.optional = optional
259
 
 
260
 
 
261
213
# Note that this is the view of an existing worksheet. Creation is handled
262
214
# by OfferingRESTView (as offerings have worksheets)
263
215
class WorksheetRESTView(JSONRESTView):
264
216
    """View used to update a worksheet."""
265
217
 
266
 
    def get_permissions(self, user):
267
 
        # XXX: Do it properly.
268
 
        # XXX: Lecturers should be allowed to add worksheets Only to subjects
269
 
        #      under their control
270
 
        if user is not None:
271
 
            if user.rolenm == 'admin':
272
 
                return set(['save'])
273
 
            else:
274
 
                return set()
275
 
        else:
276
 
            return set()    
277
 
 
278
218
    def __init__(self, req, **kwargs):
279
219
    
280
220
        self.worksheet = kwargs['worksheet']
286
226
            Worksheet.identifier == self.worksheet,
287
227
            Worksheet.offering_id == Offering.id,
288
228
            Offering.subject_id == Subject.id,
289
 
            Subject.code == self.subject,
 
229
            Subject.short_name == self.subject,
290
230
            Offering.semester_id == Semester.id,
291
231
            Semester.year == self.year,
292
232
            Semester.semester == self.semester).one()
294
234
        if self.context is None:
295
235
            raise NotFound()
296
236
    
297
 
    @named_operation('save')
 
237
    @named_operation('edit')
298
238
    def save(self, req, name, assessable, data, format):
299
239
        """Takes worksheet data and saves it."""
300
 
        self.generate_exerciselist(self.context, req, data)
301
 
        
302
240
        self.context.name = unicode(name)
303
241
        self.context.assessable = self.convert_bool(assessable)
304
242
        self.context.data = unicode(data)
 
243
        self.context.format = unicode(format)
 
244
        ivle.worksheet.utils.update_exerciselist(self.context)
305
245
        
306
246
        return {"result": "ok"}
307
247
 
308
248
class WorksheetsRESTView(JSONRESTView):
309
249
    """View used to update and create Worksheets."""
310
250
    
311
 
    def get_permissions(self, user):
312
 
        # XXX: Do it properly.
313
 
        # XXX: Lecturers should be allowed to add worksheets Only to subjects
314
 
        #      under their control
315
 
        if user is not None:
316
 
            if user.rolenm == 'admin':
317
 
                return set(['add_worksheet', 'set_sequence'])
318
 
            else:
319
 
                return set()
320
 
        else:
321
 
            return set()
322
 
 
323
251
    def __init__(self, req, **kwargs):
324
252
    
325
253
        self.subject = kwargs['subject']
328
256
    
329
257
        self.context = req.store.find(Offering,
330
258
            Offering.subject_id == Subject.id,
331
 
            Subject.code == self.subject,
 
259
            Subject.short_name == self.subject,
332
260
            Offering.semester_id == Semester.id,
333
261
            Semester.year == self.year,
334
262
            Semester.semester == self.semester).one()
336
264
        if self.context is None:
337
265
            raise NotFound()
338
266
 
339
 
    @named_operation('add_worksheet')
 
267
    @named_operation('edit')
340
268
    def add_worksheet(self, req, identifier, name, assessable, data, format):
341
269
        """Takes worksheet data and adds it."""
342
270
        
353
281
        
354
282
        # This call is added for clarity, as the worksheet is implicitly added.        
355
283
        req.store.add(new_worksheet)
356
 
        
357
 
        generate_exerciselist(new_worksheet, req, data)
358
 
        
 
284
 
 
285
        ivle.worksheet.utils.update_exerciselist(new_worksheet)
 
286
 
359
287
        return {"result": "ok"}
360
288
 
361
 
    @named_operation('set_sequence')
362
 
    def seq_sequence(self, req, worksheet_list):
363
 
        """Takes a list of worksheet-seq_no pairs and updates their 
364
 
        corresponding Worksheet objects to match."""
365
 
        
366
 
        for worksheetid, seq_no in worksheet_list:
367
 
            worksheet = req.store.find(Worksheet,
368
 
                Worksheet.offering_id == self.context.id,
369
 
                Worksheet.identifier == worksheetid).one()
370
 
            if worksheet is None:
371
 
                raise NotFound(worksheet)
372
 
            worksheet.seq_no = seq_no
 
289
    @named_operation('edit')
 
290
    def move_up(self, req, worksheetid):
 
291
        """Takes a list of worksheet-seq_no pairs and updates their 
 
292
        corresponding Worksheet objects to match."""
 
293
        
 
294
        worksheet_below = req.store.find(Worksheet,
 
295
            Worksheet.offering_id == self.context.id,
 
296
            Worksheet.identifier == unicode(worksheetid)).one()
 
297
        if worksheet_below is None:
 
298
            raise NotFound('worksheet_below')
 
299
        worksheet_above = req.store.find(Worksheet,
 
300
            Worksheet.offering_id == self.context.id,
 
301
            Worksheet.seq_no == (worksheet_below.seq_no - 1)).one()
 
302
        if worksheet_above is None:
 
303
            raise NotFound('worksheet_above')
 
304
 
 
305
        worksheet_below.seq_no = worksheet_below.seq_no - 1
 
306
        worksheet_above.seq_no = worksheet_above.seq_no + 1
 
307
        
 
308
        return {'result': 'ok'}
 
309
 
 
310
    @named_operation('edit')
 
311
    def move_down(self, req, worksheetid):
 
312
        """Takes a list of worksheet-seq_no pairs and updates their 
 
313
        corresponding Worksheet objects to match."""
 
314
        
 
315
        worksheet_above = req.store.find(Worksheet,
 
316
            Worksheet.offering_id == self.context.id,
 
317
            Worksheet.identifier == unicode(worksheetid)).one()
 
318
        if worksheet_above is None:
 
319
            raise NotFound('worksheet_below')
 
320
        worksheet_below = req.store.find(Worksheet,
 
321
            Worksheet.offering_id == self.context.id,
 
322
            Worksheet.seq_no == (worksheet_above.seq_no + 1)).one()
 
323
        if worksheet_below is None:
 
324
            raise NotFound('worksheet_above')
 
325
 
 
326
        worksheet_below.seq_no = worksheet_below.seq_no - 1
 
327
        worksheet_above.seq_no = worksheet_above.seq_no + 1
373
328
        
374
329
        return {'result': 'ok'}