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

« back to all changes in this revision

Viewing changes to ivle/db.py

  • Committer: Matt Giuca
  • Date: 2009-01-19 16:20:55 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: matt.giuca@gmail.com-20090119162055-z8m5kziajg5hchi6
ivle.worksheet: Added get_exercise_stored_text, ported from
    ivle.db.get_problem_stored_text, to Storm.

    Golly jeepers, the code is so nice with an ORM :)

ivle.db: Removed get_problem_stored_text.

tutorial: Updated to use get_exercise_stored_text.

Show diffs side-by-side

added added

removed removed

Lines of Context:
513
513
                frozenset(['date', 'text']),
514
514
                frozenset(['problemid', 'loginid']))
515
515
 
516
 
    def get_problem_stored_text(self, login, exercisename, dry=False):
517
 
        """Given a login name and exercise name, returns the text of the
518
 
        last saved/submitted attempt for this question.
519
 
        Returns None if the user has not saved or made an attempt on this
520
 
        problem.
521
 
        (If the user has both saved and submitted, it returns whichever was
522
 
        made last).
523
 
 
524
 
        Note: Even if dry, will still physically call get_problem_problemid,
525
 
        which may mutate the DB, and get_user_loginid, which may fail.
526
 
        """
527
 
        problemid = self.get_problem_problemid(exercisename)
528
 
        loginid = self.get_user_loginid(login)  # May raise a DBException
529
 
        # This very complex query finds all submissions made by this user for
530
 
        # this problem, as well as the save made by this user for this
531
 
        # problem, and returns the text of the newest one.
532
 
        # (Whichever is newer out of the save or the submit).
533
 
        query = """SELECT text FROM
534
 
    (
535
 
        (SELECT * FROM problem_save WHERE loginid = %d AND problemid = %d)
536
 
    UNION
537
 
        (SELECT problemid, loginid, date, text FROM problem_attempt
538
 
         AS problem_attempt (problemid, loginid, date, text)
539
 
         WHERE loginid = %d AND problemid = %d AND active)
540
 
    )
541
 
    AS _
542
 
    ORDER BY date DESC
543
 
    LIMIT 1;""" % (loginid, problemid, loginid, problemid)
544
 
        if dry: return query
545
 
        result = self.db.query(query)
546
 
        if result.ntuples() == 1:
547
 
            # The user has made at least 1 attempt. Return the newest.
548
 
            return result.getresult()[0][0]
549
 
        else:
550
 
            return None
551
 
 
552
516
    def get_problem_attempts(self, login, exercisename, allow_inactive=True,
553
517
                             dry=False):
554
518
        """Given a login name and exercise name, returns a list of dicts, one
556
520
        Dicts are {'date': 'formatted_time', 'complete': bool}.
557
521
        Ordered with the newest first.
558
522
        
559
 
        Note: By default, returns de-activated problem attempts (unlike
560
 
        get_problem_stored_text).
 
523
        Note: By default, returns de-activated problem attempts.
561
524
        If allow_inactive is False, will not return disabled attempts.
562
525
 
563
526
        Note: Even if dry, will still physically call get_problem_problemid,
582
545
        Returns None if the user had not made an attempt on this problem at
583
546
        that date.
584
547
        
585
 
        Note: By default, returns de-activated problem attempts (unlike
586
 
        get_problem_stored_text).
 
548
        Note: By default, returns de-activated problem attempts.
587
549
        If allow_inactive is False, will not return disabled attempts.
588
550
 
589
551
        Note: Even if dry, will still physically call get_problem_problemid,