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

« back to all changes in this revision

Viewing changes to ivle/db.py

  • Committer: William Grant
  • Date: 2009-01-20 01:12:32 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: grantw@unimelb.edu.au-20090120011232-mvxq6n1p8e0pq5z2
ivle.db: insert_problem_attempt and write_problem_save now take a user object,
    not a login, so they don't need to call get_user_loginid. Thus we also
    remove get_user_login_id.
www/apps/tutorialservice: Call the two functions with req.user, not
    req.user.login.

Show diffs side-by-side

added added

removed removed

Lines of Context:
316
316
        if dry: return query
317
317
        self.db.query(query)
318
318
 
319
 
    # USER MANAGEMENT FUNCTIONS #
320
 
 
321
 
    login_primary = frozenset(["login"])
322
 
 
323
 
    def get_user_loginid(self, login, dry=False):
324
 
        """Given a login, returns the integer loginid for this user.
325
 
 
326
 
        Raises a DBException if the login is not found in the DB.
327
 
        """
328
 
        userdict = self.get_single({"login": login}, "login",
329
 
            ['loginid'], self.login_primary,
330
 
            error_notfound="get_user_loginid: No user with that login name",
331
 
            dry=dry)
332
 
        if dry:
333
 
            return userdict     # Query string
334
 
        return userdict['loginid']
335
 
 
336
319
    # PROBLEM AND PROBLEM ATTEMPT FUNCTIONS #
337
320
 
338
321
    def get_problem_problemid(self, exercisename, dry=False):
368
351
 
369
352
        return d['problemid']
370
353
 
371
 
    def insert_problem_attempt(self, login, exercisename, date, complete,
 
354
    def insert_problem_attempt(self, user, exercisename, date, complete,
372
355
        attempt, dry=False):
373
356
        """Inserts the details of a problem attempt into the database.
374
357
        exercisename: Name of the exercise. (identifier field of problem
375
358
            table). If this exercise does not exist, also creates a new row in
376
359
            the problem table for this exercise name.
377
 
        login: Name of the user submitting the attempt. (login field of the
378
 
            login table).
 
360
        user: The user submitting the attempt.
379
361
        date: struct_time, the date this attempt was made.
380
362
        complete: bool. Whether the test passed or not.
381
363
        attempt: Text of the attempt.
382
364
 
383
365
        Note: Even if dry, will still physically call get_problem_problemid,
384
 
        which may mutate the DB, and get_user_loginid, which may fail.
 
366
        which may mutate the DB.
385
367
        """
386
368
        problemid = self.get_problem_problemid(exercisename)
387
 
        loginid = self.get_user_loginid(login)  # May raise a DBException
388
369
 
389
370
        return self.insert({
390
371
                'problemid': problemid,
391
 
                'loginid': loginid,
 
372
                'loginid': user.id,
392
373
                'date': date,
393
374
                'complete': complete,
394
375
                'attempt': attempt,
396
377
            frozenset(['problemid','loginid','date','complete','attempt']),
397
378
            dry=dry)
398
379
 
399
 
    def write_problem_save(self, login, exercisename, date, text, dry=False):
 
380
    def write_problem_save(self, user, exercisename, date, text, dry=False):
400
381
        """Writes text to the problem_save table (for when the user saves an
401
382
        exercise). Creates a new row, or overwrites an existing one if the
402
383
        user has already saved that problem.
403
384
        (Unlike problem_attempt, does not keep historical records).
404
385
        """
405
386
        problemid = self.get_problem_problemid(exercisename)
406
 
        loginid = self.get_user_loginid(login)  # May raise a DBException
407
387
 
408
388
        try:
409
389
            return self.insert({
410
390
                    'problemid': problemid,
411
 
                    'loginid': loginid,
 
391
                    'loginid': user.id,
412
392
                    'date': date,
413
393
                    'text': text,
414
394
                }, 'problem_save',
423
403
                raise
424
404
            self.update({
425
405
                    'problemid': problemid,
426
 
                    'loginid': loginid,
 
406
                    'loginid': user.id,
427
407
                },
428
408
                {
429
409
                    'date': date,