38
39
'ProjectSet', 'Project', 'ProjectGroup', 'ProjectGroupMembership',
39
40
'Exercise', 'Worksheet', 'WorksheetExercise',
40
41
'ExerciseSave', 'ExerciseAttempt',
41
'TestCase', 'TestSuite', 'TestSuiteVar'
42
'AlreadyEnrolledError'
44
45
def _kwarg_init(self, **kwargs):
53
54
Returns the Storm connection string, generated from the conf file.
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)
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,
98
88
studentid = Unicode()
99
89
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)
118
124
def password_expired(self):
119
125
fieldval = self.pass_exp
218
214
def __repr__(self):
219
215
return "<%s '%s'>" % (type(self).__name__, self.short_name)
221
def get_permissions(self, user):
229
217
class Semester(Storm):
230
218
__storm_table__ = "semester"
232
220
id = Int(primary=True, name="semesterid")
234
222
semester = Unicode()
237
225
offerings = ReferenceSet(id, 'Offering.semester_id')
259
247
project_sets = ReferenceSet(id, 'ProjectSet.offering_id')
261
worksheets = ReferenceSet(id,
262
'Worksheet.offering_id',
263
order_by="Worksheet.seq_no"
266
249
__init__ = _kwarg_init
268
251
def __repr__(self):
269
252
return "<%s %r in %r>" % (type(self).__name__, self.subject,
272
def enrol(self, user, role=u'student'):
255
def enrol(self, user):
273
256
'''Enrol a user in this offering.'''
274
enrolment = Store.of(self).find(Enrolment,
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,
275
260
Enrolment.user_id == user.id,
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):
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)
293
267
class Enrolment(Storm):
294
268
__storm_table__ = "enrolment"
392
368
# WORKSHEETS AND EXERCISES #
394
370
class Exercise(Storm):
395
__storm_table__ = "exercise"
396
id = Unicode(primary=True, name="identifier")
398
description = Unicode()
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")
404
379
worksheets = ReferenceSet(id,
405
380
'WorksheetExercise.exercise_id',
406
381
'WorksheetExercise.worksheet_id',
410
test_suites = ReferenceSet(id, 'TestSuite.exercise_id')
412
385
__init__ = _kwarg_init
414
387
def __repr__(self):
415
388
return "<%s %s>" % (type(self).__name__, self.name)
417
def get_permissions(self, user):
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))
425
405
class Worksheet(Storm):
426
406
__storm_table__ = "worksheet"
428
408
id = Int(primary=True, name="worksheetid")
429
offering_id = Int(name="offeringid")
430
identifier = Unicode()
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")
432
413
assessable = Bool()
437
attempts = ReferenceSet(id, "ExerciseAttempt.worksheetid")
438
offering = Reference(offering_id, 'Offering.id')
440
all_worksheet_exercises = ReferenceSet(id,
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
423
worksheet_exercises = ReferenceSet(id,
441
424
'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
447
def worksheet_exercises(self):
448
return self.all_worksheet_exercises.find(active=True)
450
426
__init__ = _kwarg_init
452
428
def __repr__(self):
473
449
store.find(WorksheetExercise,
474
450
WorksheetExercise.worksheet == self).remove()
476
def get_permissions(self, user):
477
return self.offering.get_permissions(user)
479
452
class WorksheetExercise(Storm):
480
__storm_table__ = "worksheet_exercise"
482
id = Int(primary=True, name="ws_ex_id")
453
__storm_table__ = "worksheet_problem"
454
__storm_primary__ = "worksheet_id", "exercise_id"
484
456
worksheet_id = Int(name="worksheetid")
485
457
worksheet = Reference(worksheet_id, Worksheet.id)
486
exercise_id = Unicode(name="exerciseid")
458
exercise_id = Int(name="problemid")
487
459
exercise = Reference(exercise_id, Exercise.id)
488
460
optional = Bool()
492
saves = ReferenceSet(id, "ExerciseSave.ws_ex_id")
493
attempts = ReferenceSet(id, "ExerciseAttempt.ws_ex_id")
495
462
__init__ = _kwarg_init
497
464
def __repr__(self):
498
465
return "<%s %s in %s>" % (type(self).__name__, self.exercise.name,
499
self.worksheet.identifier)
501
468
class ExerciseSave(Storm):
507
474
ExerciseSave may be extended with additional semantics (such as
508
475
ExerciseAttempt).
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")
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)
516
482
user_id = Int(name="loginid")
517
483
user = Reference(user_id, User.id)
518
484
date = DateTime()
537
503
they won't count (either as a penalty or success), but will still be
540
__storm_table__ = "exercise_attempt"
541
__storm_primary__ = "ws_ex_id", "user_id", "date"
506
__storm_table__ = "problem_attempt"
507
__storm_primary__ = "exercise_id", "user_id", "date"
543
509
# The "text" field is the same but has a different name in the DB table
544
510
# for some reason.
545
511
text = Unicode(name="attempt")
546
512
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