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

« back to all changes in this revision

Viewing changes to lib/common/db.py

  • Committer: mattgiuca
  • Date: 2008-03-15 04:20:30 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:693
db: get_problem_problemid now automatically inserts a new entry if the problem
    doesn't exist (now it is no longer referentially transparent, but it is
    idempotent).
    Removed insert_problem - you can just call get_problem_problemid to do
    this.
tutorialservice: No longer tries to insert a new entry if problem doesnt
    exist, just expects db to do this.
    No longer calls insert_problem (removed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
424
424
 
425
425
    def get_problem_problemid(self, exercisename, dry=False):
426
426
        """Given an exercise name, returns the associated problemID.
427
 
 
428
 
        Raises a DBException if the login is not found in the DB.
 
427
        If the exercise name is NOT in the database, it inserts it and returns
 
428
        the new problemID. Hence this may mutate the DB, but is idempotent.
429
429
        """
430
 
        d = self.get_single({"identifier": exercisename}, "problem",
431
 
            ['problemid'], frozenset(["identifier"]),
432
 
            error_notfound="get_problem_problemid: No exercise with that name",
433
 
            dry=dry)
434
 
        if dry:
435
 
            return d        # Query string
 
430
        try:
 
431
            d = self.get_single({"identifier": exercisename}, "problem",
 
432
                ['problemid'], frozenset(["identifier"]),
 
433
                dry=dry)
 
434
            if dry:
 
435
                return d        # Query string
 
436
        except DBException:
 
437
            if dry:
 
438
                # Shouldn't try again, must have failed for some other reason
 
439
                raise
 
440
            # if we failed to get a problemid, it was probably because
 
441
            # the exercise wasn't in the db. So lets insert it!
 
442
            #
 
443
            # The insert can fail if someone else simultaneously does
 
444
            # the insert, so if the insert fails, we ignore the problem. 
 
445
            try:
 
446
                self.insert({'identifier': exercisename}, "problem",
 
447
                        frozenset(['identifier']))
 
448
            except Exception, e:
 
449
                pass
 
450
 
 
451
            # Assuming the insert succeeded, we should be able to get the
 
452
            # problemid now.
 
453
            d = self.get_single({"identifier": exercisename}, "problem",
 
454
                ['problemid'], frozenset(["identifier"]))
 
455
 
436
456
        return d['problemid']
437
457
 
438
 
    def insert_problem(self, exercisename, dry=False):
439
 
        """Inserts a new problem in the problem table, with the given
440
 
        exercisename.
441
 
        """
442
 
        return self.insert({'identifier': exercisename}, "problem",
443
 
                frozenset(['identifier']), dry=dry)
444
 
 
445
458
    def insert_problem_attempt(self, problemid, loginid, date, complete,
446
459
        attempt, dry=False):
447
460
        """Inserts the details of a problem attempt into the database.