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

« back to all changes in this revision

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

MergedĀ fromĀ trunk

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
 
from ivle.webapp.tutorial.rst import rst as rstfunc
35
34
from ivle.webapp.base.rest import (JSONRESTView, named_operation,
36
35
                                   require_permission)
37
36
from ivle.webapp.errors import NotFound
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
 
147
146
            Semester.year == year,
148
147
            Semester.semester == semester).one()
149
148
            
150
 
        attempt = ivle.worksheet.get_exercise_attempt(req.store, user,
 
149
        attempt = ivle.worksheet.utils.get_exercise_attempt(req.store, user,
151
150
                        worksheet_exercise, as_of=date,
152
151
                        allow_inactive=HISTORY_ALLOW_INACTIVE) 
153
152
 
209
208
        return {"result": "ok"}
210
209
 
211
210
 
212
 
def generate_exerciselist(worksheet, req, worksheetdata):
213
 
    """Runs through the worksheetstream, generating the appropriate
214
 
    WorksheetExercises, and de-activating the old ones."""
215
 
    exercises = []
216
 
    # Turns the worksheet into an xml stream, and then finds all the 
217
 
    # exercise nodes in the stream.
218
 
    worksheetdata = genshi.XML(worksheetdata)
219
 
    for kind, data, pos in worksheetdata:
220
 
        if kind is genshi.core.START:
221
 
            # Data is a tuple of tag name and a list of name->value tuples
222
 
            if data[0] == 'exercise':
223
 
                src = ""
224
 
                optional = False
225
 
                for attr in data[1]:
226
 
                    if attr[0] == 'src':
227
 
                        src = attr[1]
228
 
                    if attr[0] == 'optional':
229
 
                        optional = attr[1] == 'true'
230
 
                if src != "":
231
 
                    exercises.append((src, optional))
232
 
    ex_num = 0
233
 
    # Set all current worksheet_exercises to be inactive
234
 
    db_worksheet_exercises = req.store.find(WorksheetExercise,
235
 
        WorksheetExercise.worksheet_id == worksheet.id)
236
 
    for worksheet_exercise in db_worksheet_exercises:
237
 
        worksheet_exercise.active = False
238
 
    
239
 
    for exerciseid, optional in exercises:
240
 
        worksheet_exercise = req.store.find(WorksheetExercise,
241
 
            WorksheetExercise.worksheet_id == worksheet.id,
242
 
            Exercise.id == WorksheetExercise.exercise_id,
243
 
            Exercise.id == exerciseid).one()
244
 
        if worksheet_exercise is None:
245
 
            exercise = req.store.find(Exercise,
246
 
                Exercise.id == exerciseid
247
 
            ).one()
248
 
            if exercise is None:
249
 
                raise NotFound()
250
 
            worksheet_exercise = WorksheetExercise()
251
 
            worksheet_exercise.worksheet_id = worksheet.id
252
 
            worksheet_exercise.exercise_id = exercise.id
253
 
            req.store.add(worksheet_exercise)
254
 
        worksheet_exercise.active = True
255
 
        worksheet_exercise.seq_no = ex_num
256
 
        worksheet_exercise.optional = optional
257
 
 
258
211
 
259
212
# Note that this is the view of an existing worksheet. Creation is handled
260
213
# by OfferingRESTView (as offerings have worksheets)
266
219
        # XXX: Lecturers should be allowed to add worksheets Only to subjects
267
220
        #      under their control
268
221
        if user is not None:
269
 
            if user.rolenm == 'admin':
 
222
            if user.admin:
270
223
                return set(['save'])
271
224
            else:
272
225
                return set()
295
248
    @named_operation('save')
296
249
    def save(self, req, name, assessable, data, format):
297
250
        """Takes worksheet data and saves it."""
298
 
        generate_exerciselist(self.context, req, data)
299
 
        
300
251
        self.context.name = unicode(name)
301
252
        self.context.assessable = self.convert_bool(assessable)
302
253
        self.context.data = unicode(data)
303
254
        self.context.format = unicode(format)
 
255
        ivle.worksheet.utils.update_exerciselist(self.context)
304
256
        
305
257
        return {"result": "ok"}
306
258
 
312
264
        # XXX: Lecturers should be allowed to add worksheets Only to subjects
313
265
        #      under their control
314
266
        if user is not None:
315
 
            if user.rolenm == 'admin':
 
267
            if user.admin:
316
268
                return set(['edit'])
317
269
            else:
318
270
                return set()
352
304
        
353
305
        # This call is added for clarity, as the worksheet is implicitly added.        
354
306
        req.store.add(new_worksheet)
355
 
        
356
 
        if new_worksheet.format == u'rst':
357
 
            ws_data = '<worksheet>' + rstfunc(unicode(data)) + '</worksheet>'
358
 
            generate_exerciselist(new_worksheet, req, ws_data)
359
 
        else:
360
 
            generate_exerciselist(new_worksheet, req, data)
361
 
        
 
307
 
 
308
        ivle.worksheet.utils.update_exerciselist(new_worksheet)
 
309
 
362
310
        return {"result": "ok"}
363
311
 
364
312
    @named_operation('edit')