39
38
'ProjectSet', 'Project', 'ProjectGroup', 'ProjectGroupMembership',
40
39
'Exercise', 'Worksheet', 'WorksheetExercise',
41
40
'ExerciseSave', 'ExerciseAttempt',
42
'AlreadyEnrolledError', 'TestCase', 'TestSuite', 'TestSuiteVar'
41
'TestCase', 'TestSuite', 'TestSuiteVar'
45
44
def _kwarg_init(self, **kwargs):
54
53
Returns the Storm connection string, generated from the conf file.
56
return "postgres://%s:%s@%s:%d/%s" % (ivle.conf.db_user,
57
ivle.conf.db_password, ivle.conf.db_host, ivle.conf.db_port,
58
clusterstr += ivle.conf.db_user
59
if ivle.conf.db_password:
60
clusterstr += ':' + ivle.conf.db_password
63
host = ivle.conf.db_host or 'localhost'
64
port = ivle.conf.db_port or 5432
66
clusterstr += '%s:%d' % (host, port)
68
return "postgres://%s/%s" % (clusterstr, ivle.conf.db_dbname)
88
98
studentid = Unicode()
89
99
settings = Unicode()
92
if self.rolenm is None:
94
return ivle.caps.Role(self.rolenm)
95
def _set_role(self, value):
96
if not isinstance(value, ivle.caps.Role):
97
raise TypeError("role must be an ivle.caps.Role")
98
self.rolenm = unicode(value)
99
role = property(_get_role, _set_role)
101
101
__init__ = _kwarg_init
103
103
def __repr__(self):
115
115
return self.hash_password(password) == self.passhash
117
def hasCap(self, capability):
118
"""Given a capability (which is a Role object), returns True if this
119
User has that capability, False otherwise.
121
return self.role.hasCap(capability)
124
118
def password_expired(self):
125
119
fieldval = self.pass_exp
201
195
return store.find(cls, cls.login == unicode(login)).one()
203
197
def get_permissions(self, user):
204
if user and user.rolenm == 'admin' or user is self:
198
if user and user.admin or user is self:
205
199
return set(['view', 'edit'])
265
259
project_sets = ReferenceSet(id, 'ProjectSet.offering_id')
267
worksheets = ReferenceSet(id, 'Worksheet.offering_id')
261
worksheets = ReferenceSet(id,
262
'Worksheet.offering_id',
263
order_by="Worksheet.seq_no"
269
266
__init__ = _kwarg_init
272
269
return "<%s %r in %r>" % (type(self).__name__, self.subject,
275
def enrol(self, user):
272
def enrol(self, user, role=u'student'):
276
273
'''Enrol a user in this offering.'''
277
# We'll get a horrible database constraint violation error if we try
278
# to add a second enrolment.
279
if Store.of(self).find(Enrolment,
274
enrolment = Store.of(self).find(Enrolment,
280
275
Enrolment.user_id == user.id,
281
Enrolment.offering_id == self.id).count() == 1:
282
raise AlreadyEnrolledError()
284
e = Enrolment(user=user, offering=self, active=True)
285
self.enrolments.add(e)
276
Enrolment.offering_id == self.id).one()
278
if enrolment is None:
279
enrolment = Enrolment(user=user, offering=self)
280
self.enrolments.add(enrolment)
282
enrolment.active = True
283
enrolment.role = role
287
285
def get_permissions(self, user):
289
287
if user is not None:
290
288
perms.add('view')
291
if user.rolenm == 'admin':
292
290
perms.add('edit')
396
392
# WORKSHEETS AND EXERCISES #
398
394
class Exercise(Storm):
399
# Note: Table "problem" is called "Exercise" in the Object layer, since
400
# it's called that everywhere else.
401
__storm_table__ = "problem"
402
#TODO: Add in a field for the user-friendly identifier
395
__storm_table__ = "exercise"
403
396
id = Unicode(primary=True, name="identifier")
405
398
description = Unicode()
421
414
def __repr__(self):
422
415
return "<%s %s>" % (type(self).__name__, self.name)
417
def get_permissions(self, user):
425
425
class Worksheet(Storm):
426
426
__storm_table__ = "worksheet"
428
428
id = Int(primary=True, name="worksheetid")
429
# XXX subject is not linked to a Subject object. This is a property of
430
# the database, and will be refactored.
431
429
offering_id = Int(name="offeringid")
432
name = Unicode(name="identifier")
430
identifier = Unicode()
433
432
assessable = Bool()
436
437
attempts = ReferenceSet(id, "ExerciseAttempt.worksheetid")
437
438
offering = Reference(offering_id, 'Offering.id')
439
exercises = ReferenceSet(id,
440
'WorksheetExercise.worksheet_id',
441
'WorksheetExercise.exercise_id',
443
# Use worksheet_exercises to get access to the WorksheetExercise objects
444
# binding worksheets to exercises. This is required to access the
440
all_worksheet_exercises = ReferenceSet(id,
441
'WorksheetExercise.worksheet_id')
443
# Use worksheet_exercises to get access to the *active* WorksheetExercise
444
# objects binding worksheets to exercises. This is required to access the
445
445
# "optional" field.
446
worksheet_exercises = ReferenceSet(id,
447
'WorksheetExercise.worksheet_id')
447
def worksheet_exercises(self):
448
return self.all_worksheet_exercises.find(active=True)
450
450
__init__ = _kwarg_init
477
477
return self.offering.get_permissions(user)
479
479
class WorksheetExercise(Storm):
480
__storm_table__ = "worksheet_problem"
481
__storm_primary__ = "worksheet_id", "exercise_id"
480
__storm_table__ = "worksheet_exercise"
482
id = Int(primary=True, name="ws_ex_id")
483
484
worksheet_id = Int(name="worksheetid")
484
485
worksheet = Reference(worksheet_id, Worksheet.id)
485
exercise_id = Unicode(name="problemid")
486
exercise_id = Unicode(name="exerciseid")
486
487
exercise = Reference(exercise_id, Exercise.id)
487
488
optional = Bool()
492
saves = ReferenceSet(id, "ExerciseSave.ws_ex_id")
493
attempts = ReferenceSet(id, "ExerciseAttempt.ws_ex_id")
489
495
__init__ = _kwarg_init
491
497
def __repr__(self):
492
498
return "<%s %s in %s>" % (type(self).__name__, self.exercise.name,
499
self.worksheet.identifier)
495
501
class ExerciseSave(Storm):
501
507
ExerciseSave may be extended with additional semantics (such as
502
508
ExerciseAttempt).
504
__storm_table__ = "problem_save"
505
__storm_primary__ = "exercise_id", "user_id", "date"
507
exercise_id = Unicode(name="problemid")
508
exercise = Reference(exercise_id, Exercise.id)
510
__storm_table__ = "exercise_save"
511
__storm_primary__ = "ws_ex_id", "user_id"
513
ws_ex_id = Int(name="ws_ex_id")
514
worksheet_exercise = Reference(ws_ex_id, "WorksheetExercise.id")
509
516
user_id = Int(name="loginid")
510
517
user = Reference(user_id, User.id)
511
518
date = DateTime()
514
worksheet = Reference(worksheetid, Worksheet.id)
516
521
__init__ = _kwarg_init
532
537
they won't count (either as a penalty or success), but will still be
535
__storm_table__ = "problem_attempt"
536
__storm_primary__ = "exercise_id", "user_id", "date"
540
__storm_table__ = "exercise_attempt"
541
__storm_primary__ = "ws_ex_id", "user_id", "date"
538
543
# The "text" field is the same but has a different name in the DB table
539
544
# for some reason.
581
586
class TestSuiteVar(Storm):
582
587
"""A container for the arguments of a Test Suite"""
583
__storm_table__ = "suite_variables"
588
__storm_table__ = "suite_variable"
584
589
__storm_primary__ = "varid"
597
602
class TestCasePart(Storm):
598
603
"""A container for the test elements of a Test Case"""
599
__storm_table__ = "test_case_parts"
604
__storm_table__ = "test_case_part"
600
605
__storm_primary__ = "partid"