39
38
'ProjectSet', 'Project', 'ProjectGroup', 'ProjectGroupMembership',
40
39
'Exercise', 'Worksheet', 'WorksheetExercise',
41
40
'ExerciseSave', 'ExerciseAttempt',
42
'AlreadyEnrolledError'
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
214
218
def __repr__(self):
215
219
return "<%s '%s'>" % (type(self).__name__, self.short_name)
221
def get_permissions(self, user):
217
229
class Semester(Storm):
218
230
__storm_table__ = "semester"
220
232
id = Int(primary=True, name="semesterid")
222
234
semester = Unicode()
225
237
offerings = ReferenceSet(id, 'Offering.semester_id')
247
259
project_sets = ReferenceSet(id, 'ProjectSet.offering_id')
261
worksheets = ReferenceSet(id,
262
'Worksheet.offering_id',
263
order_by="Worksheet.seq_no"
249
266
__init__ = _kwarg_init
251
268
def __repr__(self):
252
269
return "<%s %r in %r>" % (type(self).__name__, self.subject,
255
def enrol(self, user):
272
def enrol(self, user, role=u'student'):
256
273
'''Enrol a user in this offering.'''
257
# We'll get a horrible database constraint violation error if we try
258
# to add a second enrolment.
259
if Store.of(self).find(Enrolment,
274
enrolment = Store.of(self).find(Enrolment,
260
275
Enrolment.user_id == user.id,
261
Enrolment.offering_id == self.id).count() == 1:
262
raise AlreadyEnrolledError()
264
e = Enrolment(user=user, offering=self, active=True)
265
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
285
def get_permissions(self, user):
267
293
class Enrolment(Storm):
268
294
__storm_table__ = "enrolment"
368
392
# WORKSHEETS AND EXERCISES #
370
394
class Exercise(Storm):
371
# Note: Table "problem" is called "Exercise" in the Object layer, since
372
# it's called that everywhere else.
373
__storm_table__ = "problem"
375
id = Int(primary=True, name="problemid")
376
name = Unicode(name="identifier")
395
__storm_table__ = "exercise"
396
id = Unicode(primary=True, name="identifier")
398
description = Unicode()
379
404
worksheets = ReferenceSet(id,
380
405
'WorksheetExercise.exercise_id',
381
406
'WorksheetExercise.worksheet_id',
410
test_suites = ReferenceSet(id, 'TestSuite.exercise_id')
385
412
__init__ = _kwarg_init
387
414
def __repr__(self):
388
415
return "<%s %s>" % (type(self).__name__, self.name)
391
def get_by_name(cls, store, name):
393
Get the Exercise from the db associated with a given store and name.
394
If the exercise is not in the database, creates it and inserts it
397
ex = store.find(cls, cls.name == unicode(name)).one()
400
ex = Exercise(name=unicode(name))
417
def get_permissions(self, user):
405
425
class Worksheet(Storm):
406
426
__storm_table__ = "worksheet"
408
428
id = Int(primary=True, name="worksheetid")
409
# XXX subject is not linked to a Subject object. This is a property of
410
# the database, and will be refactored.
412
name = Unicode(name="identifier")
429
offering_id = Int(name="offeringid")
430
identifier = Unicode()
413
432
assessable = Bool()
416
exercises = ReferenceSet(id,
417
'WorksheetExercise.worksheet_id',
418
'WorksheetExercise.exercise_id',
420
# Use worksheet_exercises to get access to the WorksheetExercise objects
421
# binding worksheets to exercises. This is required to access the
437
attempts = ReferenceSet(id, "ExerciseAttempt.worksheetid")
438
offering = Reference(offering_id, 'Offering.id')
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
422
445
# "optional" field.
423
worksheet_exercises = ReferenceSet(id,
424
'WorksheetExercise.worksheet_id')
447
def worksheet_exercises(self):
448
return self.all_worksheet_exercises.find(active=True)
426
450
__init__ = _kwarg_init
449
473
store.find(WorksheetExercise,
450
474
WorksheetExercise.worksheet == self).remove()
476
def get_permissions(self, user):
477
return self.offering.get_permissions(user)
452
479
class WorksheetExercise(Storm):
453
__storm_table__ = "worksheet_problem"
454
__storm_primary__ = "worksheet_id", "exercise_id"
480
__storm_table__ = "worksheet_exercise"
482
id = Int(primary=True, name="ws_ex_id")
456
484
worksheet_id = Int(name="worksheetid")
457
485
worksheet = Reference(worksheet_id, Worksheet.id)
458
exercise_id = Int(name="problemid")
486
exercise_id = Unicode(name="exerciseid")
459
487
exercise = Reference(exercise_id, Exercise.id)
460
488
optional = Bool()
492
saves = ReferenceSet(id, "ExerciseSave.ws_ex_id")
493
attempts = ReferenceSet(id, "ExerciseAttempt.ws_ex_id")
462
495
__init__ = _kwarg_init
464
497
def __repr__(self):
465
498
return "<%s %s in %s>" % (type(self).__name__, self.exercise.name,
499
self.worksheet.identifier)
468
501
class ExerciseSave(Storm):
474
507
ExerciseSave may be extended with additional semantics (such as
475
508
ExerciseAttempt).
477
__storm_table__ = "problem_save"
478
__storm_primary__ = "exercise_id", "user_id", "date"
480
exercise_id = Int(name="problemid")
481
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")
482
516
user_id = Int(name="loginid")
483
517
user = Reference(user_id, User.id)
484
518
date = DateTime()
503
537
they won't count (either as a penalty or success), but will still be
506
__storm_table__ = "problem_attempt"
507
__storm_primary__ = "exercise_id", "user_id", "date"
540
__storm_table__ = "exercise_attempt"
541
__storm_primary__ = "ws_ex_id", "user_id", "date"
509
543
# The "text" field is the same but has a different name in the DB table
510
544
# for some reason.
511
545
text = Unicode(name="attempt")
512
546
complete = Bool()
549
def get_permissions(self, user):
550
return set(['view']) if user is self.user else set()
552
class TestSuite(Storm):
553
"""A Testsuite acts as a container for the test cases of an exercise."""
554
__storm_table__ = "test_suite"
555
__storm_primary__ = "exercise_id", "suiteid"
558
exercise_id = Unicode(name="exerciseid")
559
description = Unicode()
563
exercise = Reference(exercise_id, Exercise.id)
564
test_cases = ReferenceSet(suiteid, 'TestCase.suiteid')
565
variables = ReferenceSet(suiteid, 'TestSuiteVar.suiteid')
567
class TestCase(Storm):
568
"""A TestCase is a member of a TestSuite.
570
It contains the data necessary to check if an exercise is correct"""
571
__storm_table__ = "test_case"
572
__storm_primary__ = "testid", "suiteid"
576
suite = Reference(suiteid, "TestSuite.suiteid")
579
test_default = Unicode()
582
parts = ReferenceSet(testid, "TestCasePart.testid")
584
__init__ = _kwarg_init
586
class TestSuiteVar(Storm):
587
"""A container for the arguments of a Test Suite"""
588
__storm_table__ = "suite_variable"
589
__storm_primary__ = "varid"
594
var_value = Unicode()
598
suite = Reference(suiteid, "TestSuite.suiteid")
600
__init__ = _kwarg_init
602
class TestCasePart(Storm):
603
"""A container for the test elements of a Test Case"""
604
__storm_table__ = "test_case_part"
605
__storm_primary__ = "partid"
610
part_type = Unicode()
611
test_type = Unicode()
615
test = Reference(testid, "TestCase.testid")
617
__init__ = _kwarg_init