~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 17:06:24 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: matt.giuca@gmail.com-20090119170624-3bowlh5rs7lf7eqz
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
tutorialservice: Updated to use these new functions rather than ivle.db. Also
    uses datetime and does its own dictionarification rather than relying on
    ivle.db to do it.
    NOTE: This is just for the above two functions, not the whole module
    (still reliant on ivle.db).
ivle.db: Removed get_problem_attempts and get_problem_attempt.

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
    return first_success is not None, num_attempts
64
64
 
65
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).
 
66
    """Given a storm.store, User and Exercise, returns an
 
67
    ivle.database.ExerciseSave object for the last saved/submitted attempt for
 
68
    this question (note that ExerciseAttempt is a subclass of ExerciseSave).
70
69
    Returns None if the user has not saved or made an attempt on this
71
70
    problem.
72
71
    If the user has both saved and submitted, it returns whichever was
98
97
            return attempt
99
98
        else:
100
99
            return None
 
100
 
 
101
def _get_exercise_attempts(store, user, exercise, as_of=None,
 
102
        allow_inactive=False):
 
103
    """Same as get_exercise_attempts, but doesn't convert Storm's iterator
 
104
    into a list."""
 
105
    ExerciseAttempt = ivle.database.ExerciseAttempt
 
106
 
 
107
    # Get the most recent attempt before as_of, or None
 
108
    return store.find(ExerciseAttempt,
 
109
            ExerciseAttempt.user_id == user.id,
 
110
            ExerciseAttempt.exercise_id == exercise.id,
 
111
            True if allow_inactive else ExerciseAttempt.active == True,
 
112
            True if as_of is None else ExerciseAttempt.date <= as_of,
 
113
        ).order_by(Desc(ExerciseAttempt.date))
 
114
 
 
115
def get_exercise_attempts(store, user, exercise, as_of=None,
 
116
        allow_inactive=False):
 
117
    """Given a storm.store, User and Exercise, returns a list of
 
118
    ivle.database.ExerciseAttempt objects, one for each attempt made for the
 
119
    exercise, sorted from latest to earliest.
 
120
 
 
121
    as_of: Optional datetime.datetime object. If supplied, only returns
 
122
        attempts made before or at this time.
 
123
    allow_inactive: If True, will return disabled attempts.
 
124
    """
 
125
    return list(_get_exercise_attempts(store, user, exercise, as_of,
 
126
        allow_inactive))
 
127
 
 
128
def get_exercise_attempt(store, user, exercise, as_of=None,
 
129
        allow_inactive=False):
 
130
    """Given a storm.store, User and Exercise, returns an
 
131
    ivle.database.ExerciseAttempt object for the last submitted attempt for
 
132
    this question.
 
133
    Returns None if the user has not made an attempt on this
 
134
    problem.
 
135
 
 
136
    as_of: Optional datetime.datetime object. If supplied, only returns
 
137
        attempts made before or at this time.
 
138
    allow_inactive: If True, will return disabled attempts.
 
139
    """
 
140
    return _get_exercise_attempts(store, user, exercise, as_of,
 
141
        allow_inactive).first()