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

« back to all changes in this revision

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

  • Committer: Nick Chadwick
  • Date: 2009-02-19 05:47:56 UTC
  • mto: (1099.1.180 new-dispatch)
  • mto: This revision was merged to the branch mainline in revision 1100.
  • Revision ID: chadnickbok@gmail.com-20090219054756-v984vmc7kheiq6xy
Updated the tutorial service, to now allow users to edit worksheets
online.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
import os
23
23
import datetime
 
24
import genshi
24
25
 
25
26
import ivle.util
26
27
import ivle.console
27
28
import ivle.database
28
 
from ivle.database import Exercise, ExerciseAttempt, ExerciseSave, Worksheet, Offering, Subject, Semester
 
29
from ivle.database import Exercise, ExerciseAttempt, ExerciseSave, Worksheet, \
 
30
                          Offering, Subject, Semester, WorksheetExercise
29
31
import ivle.worksheet
30
32
import ivle.conf
31
33
import ivle.webapp.tutorial.test
40
42
 
41
43
TIMESTAMP_FORMAT = '%Y-%m-%d %H:%M:%S'
42
44
 
43
 
 
44
45
class AttemptsRESTView(JSONRESTView):
45
46
    '''REST view of a user's attempts at an exercise.'''
46
47
    def __init__(self, req, subject, year, semester, worksheet, 
188
189
class WorksheetRESTView(JSONRESTView):
189
190
    """View used to update a worksheet."""
190
191
 
 
192
    def generate_exerciselist(self, req, worksheet):
 
193
        """Runs through the worksheetstream, generating the appropriate
 
194
        WorksheetExercises, and de-activating the old ones."""
 
195
        exercises = []
 
196
        # Turns the worksheet into an xml stream, and then finds all the 
 
197
        # exercise nodes in the stream.
 
198
        worksheet = genshi.XML(worksheet)
 
199
        for kind, data, pos in worksheet:
 
200
            if kind is genshi.core.START:
 
201
                # Data is a tuple of tag name and a list of name->value tuples
 
202
                if data[0] == 'exercise':
 
203
                    src = ""
 
204
                    optional = False
 
205
                    for attr in data[1]:
 
206
                        if attr[0] == 'src':
 
207
                            src = attr[1]
 
208
                        if attr[0] == 'optional':
 
209
                            optional = attr[1] == 'true'
 
210
                    if src != "":
 
211
                        exercises.append((src, optional))
 
212
        ex_num = 0
 
213
        # Set all current worksheet_exercises to be inactive
 
214
        db_worksheet_exercises = req.store.find(WorksheetExercise,
 
215
            WorksheetExercise.worksheet_id == self.context.id)
 
216
        for worksheet_exercise in db_worksheet_exercises:
 
217
            worksheet_exercise.active = False
 
218
        
 
219
        for exerciseid, optional in exercises:
 
220
            worksheet_exercise = req.store.find(WorksheetExercise,
 
221
                WorksheetExercise.worksheet_id == self.context.id,
 
222
                Exercise.id == WorksheetExercise.exercise_id,
 
223
                Exercise.id == exerciseid).one()
 
224
            if worksheet_exercise is None:
 
225
                exercise = req.store.find(Exercise,
 
226
                    Exercise.id == exerciseid
 
227
                ).one()
 
228
                if exercise is None:
 
229
                    raise NotFound()
 
230
                worksheet_exercise = WorksheetExercise()
 
231
                worksheet_exercise.worksheet_id = self.context.id
 
232
                worksheet_exercise.exercise_id = exercise.id
 
233
                req.store.add(worksheet_exercise)
 
234
            worksheet_exercise.active = True
 
235
            worksheet_exercise.seq_no = ex_num
 
236
            worksheet_exercise.optional = optional
 
237
 
191
238
    def get_permissions(self, user):
192
239
        # XXX: Do it properly.
193
240
        if user is not None:
206
253
        self.semester = kwargs['semester']
207
254
    
208
255
        self.context = req.store.find(Worksheet,
209
 
            Worksheet.name == self.worksheet,
 
256
            Worksheet.identifier == self.worksheet,
210
257
            Worksheet.offering_id == Offering.id,
211
258
            Offering.subject_id == Subject.id,
212
259
            Subject.code == self.subject,
213
260
            Offering.semester_id == Semester.id,
214
261
            Semester.year == self.year,
215
262
            Semester.semester == self.semester).one()
 
263
        
 
264
        if self.context is None:
 
265
            raise NotFound()
216
266
    
217
267
    @named_operation('save')
218
 
    def save(self, req, data):
219
 
        self.worksheet.data = data
 
268
    def save(self, req, name, assessable, data):
 
269
        """Takes worksheet data and saves it."""
 
270
        self.generate_exerciselist(req, data)
 
271
        
 
272
        self.context.name = unicode(name)
 
273
        self.context.data = unicode(data)
 
274
        self.context.assessable = self.convert_bool(assessable)
220
275
        
221
276
        return {"result": "ok"}