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

« back to all changes in this revision

Viewing changes to ivle/worksheet.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:
61
61
        num_attempts = store.find(ExerciseAttempt, is_relevant).count()
62
62
 
63
63
    return first_success is not None, num_attempts
 
64
 
 
65
def get_exercise_stored_text(store, user, exercise):
 
66
    """Given a storm.store, User and Exercise, returns the text of the last
 
67
    saved/submitted attempt for this question, as an
 
68
    ivle.database.ExerciseSave object (note that ExerciseAttempt is a subclass
 
69
    of ExerciseSave).
 
70
    Returns None if the user has not saved or made an attempt on this
 
71
    problem.
 
72
    If the user has both saved and submitted, it returns whichever was
 
73
    made last.
 
74
    """
 
75
    ExerciseSave = ivle.database.ExerciseSave
 
76
    ExerciseAttempt = ivle.database.ExerciseAttempt
 
77
 
 
78
    # Get the saved text, or None
 
79
    saved = store.find(ExerciseSave,
 
80
                ExerciseSave.user_id == user.id,
 
81
                ExerciseSave.exercise_id == exercise.id).one()
 
82
 
 
83
    # Get the most recent attempt, or None
 
84
    attempt = store.find(ExerciseAttempt,
 
85
            ExerciseAttempt.user_id == user.id,
 
86
            ExerciseAttempt.exercise_id == exercise.id,
 
87
            ExerciseAttempt.active == True,
 
88
        ).order_by(Asc(ExerciseAttempt.date)).last()
 
89
 
 
90
    # Pick the most recent of these two
 
91
    if saved is not None:
 
92
        if attempt is not None:
 
93
            return saved if saved.date > attempt.date else attempt
 
94
        else:
 
95
            return saved
 
96
    else:
 
97
        if attempt is not None:
 
98
            return attempt
 
99
        else:
 
100
            return None