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

« back to all changes in this revision

Viewing changes to ivle/webapp/tutorial/service.py

  • Committer: William Grant
  • Date: 2009-07-05 03:49:31 UTC
  • mto: (1294.4.2 ui-the-third)
  • mto: This revision was merged to the branch mainline in revision 1353.
  • Revision ID: grantw@unimelb.edu.au-20090705034931-e41r97axkoxeitw6
Move i.w.tutorial traversal stuff into i.w.tutorial.traversal.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
                                   require_permission)
36
36
from ivle.webapp.errors import NotFound
37
37
 
38
 
# If True, getattempts or getattempt will allow browsing of inactive/disabled
39
 
# attempts. If False, will not allow this.
40
 
HISTORY_ALLOW_INACTIVE = False
41
38
 
42
39
TIMESTAMP_FORMAT = '%Y-%m-%d %H:%M:%S'
43
40
 
44
 
 
45
 
class ExerciseAttempts(object):
46
 
    """The set of exercise attempts for a user and exercise.
47
 
 
48
 
    A combination of a User and WorksheetExercise, this provides access to
49
 
    the User's ExerciseAttempts.
50
 
    """
51
 
 
52
 
    def __init__(self, worksheet_exercise, user):
53
 
        self.worksheet_exercise = worksheet_exercise
54
 
        self.user = user
55
 
 
56
 
    def get_permissions(self, user):
57
 
        return self.user.get_permissions(user)
58
 
 
59
 
 
60
 
def exerciseattempts_to_attempt(exercise_attempts, date):
61
 
    try:
62
 
        date = datetime.datetime.strptime(date, TIMESTAMP_FORMAT)
63
 
    except ValueError:
64
 
        return None
65
 
 
66
 
    # XXX Hack around Google Code issue #87
67
 
    # Query from the given date +1 secnod.
68
 
    # Date is in seconds (eg. 3:47:12), while the data is in finer time
69
 
    # (eg. 3:47:12.3625). The query "date <= 3:47:12" will fail because
70
 
    # 3:47:12.3625 is greater. Hence we do the query from +1 second,
71
 
    # "date <= 3:47:13", and it finds the correct submission, UNLESS there
72
 
    # are multiple submissions inside the same second.
73
 
    date += datetime.timedelta(seconds=1)
74
 
 
75
 
    return ivle.worksheet.utils.get_exercise_attempt(
76
 
                Store.of(exercise_attempts.user),
77
 
                exercise_attempts.user, exercise_attempts.worksheet_exercise,
78
 
                as_of=date, allow_inactive=HISTORY_ALLOW_INACTIVE)
79
 
 
80
 
 
81
 
def worksheet_exercise_to_user_attempts(worksheet_exercise, login):
82
 
    user = User.get_by_login(Store.of(worksheet_exercise), login)
83
 
    if user is None:
84
 
        return None
85
 
    return ExerciseAttempts(worksheet_exercise, user)
86
 
 
87
 
 
88
 
def worksheet_to_worksheet_exercise(worksheet, exercise_name):
89
 
    return Store.of(worksheet).find(
90
 
        WorksheetExercise,
91
 
        WorksheetExercise.exercise_id == exercise_name,
92
 
        WorksheetExercise.worksheet == worksheet
93
 
        ).one()
94
 
 
95
 
 
96
41
class AttemptsRESTView(JSONRESTView):
97
42
    '''REST view of a user's attempts at an exercise.'''
98
43