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

« back to all changes in this revision

Viewing changes to lib/common/db.py

  • Committer: mattgiuca
  • Date: 2008-08-18 10:05:46 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1025
tutorialservice: Added two new GET actions: getattempts and getattempt.
    getattempts retrieves a list of all attempts the user has made for a
    particular question.
    getattempt returns the code for a specific attempt.
    Note that BY DEFAULT, these do not allow you to browse disabled attempts
    (but this can be easily activated via a flag at the top of
    tutorialservice/__init__.py).
db.py: Added get_problem_attempts and get_problem_attempt methods to do the DB
    queries for the above actions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
616
616
        else:
617
617
            return None
618
618
 
 
619
    def get_problem_attempts(self, login, exercisename, allow_inactive=True,
 
620
                             dry=False):
 
621
        """Given a login name and exercise name, returns a list of dicts, one
 
622
        for each attempt made for that exercise.
 
623
        Dicts are {'date': 'formatted_time', 'complete': bool}.
 
624
        Ordered with the newest first.
 
625
        
 
626
        Note: By default, returns de-activated problem attempts (unlike
 
627
        get_problem_stored_text).
 
628
        If allow_inactive is False, will not return disabled attempts.
 
629
 
 
630
        Note: Even if dry, will still physically call get_problem_problemid,
 
631
        which may mutate the DB, and get_user_loginid, which may fail.
 
632
        """
 
633
        problemid = self.get_problem_problemid(exercisename)
 
634
        loginid = self.get_user_loginid(login)  # May raise a DBException
 
635
        andactive = '' if allow_inactive else ' AND active'
 
636
        query = """SELECT date, complete FROM problem_attempt
 
637
    WHERE loginid = %d AND problemid = %d%s
 
638
    ORDER BY date DESC;""" % (loginid, problemid, andactive)
 
639
        if dry: return query
 
640
        result = self.db.query(query).getresult()
 
641
        # Make into dicts (could use dictresult, but want to convert values)
 
642
        return [{'date': date, 'complete': _parse_boolean(complete)}
 
643
                for date, complete in result]
 
644
 
 
645
    def get_problem_attempt(self, login, exercisename, as_of,
 
646
        allow_inactive=True, dry=False):
 
647
        """Given a login name, exercise name, and struct_time, returns the
 
648
        text of the submitted attempt for this question as of that date.
 
649
        Returns None if the user had not made an attempt on this problem at
 
650
        that date.
 
651
        
 
652
        Note: By default, returns de-activated problem attempts (unlike
 
653
        get_problem_stored_text).
 
654
        If allow_inactive is False, will not return disabled attempts.
 
655
 
 
656
        Note: Even if dry, will still physically call get_problem_problemid,
 
657
        which may mutate the DB, and get_user_loginid, which may fail.
 
658
        """
 
659
        problemid = self.get_problem_problemid(exercisename)
 
660
        loginid = self.get_user_loginid(login)  # May raise a DBException
 
661
        # Very similar to query in get_problem_stored_text, but without
 
662
        # looking in problem_save, and restricting to a certain date.
 
663
        andactive = '' if allow_inactive else ' AND active'
 
664
        query = """SELECT attempt FROM problem_attempt
 
665
    WHERE loginid = %d AND problemid = %d%s AND date <= %s
 
666
    ORDER BY date DESC
 
667
    LIMIT 1;""" % (loginid, problemid, andactive, _escape(as_of))
 
668
        if dry: return query
 
669
        result = self.db.query(query)
 
670
        if result.ntuples() == 1:
 
671
            # The user has made at least 1 attempt. Return the newest.
 
672
            return result.getresult()[0][0]
 
673
        else:
 
674
            return None
 
675
 
619
676
    def get_problem_status(self, login, exercisename, dry=False):
620
677
        """Given a login name and exercise name, returns information about the
621
678
        user's performance on that problem.