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

« back to all changes in this revision

Viewing changes to ivle/database.py

Remove the last two uses of req.write_html_head_foot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
                         Reference, ReferenceSet, Bool, Storm, Desc
32
32
 
33
33
import ivle.conf
 
34
import ivle.caps
34
35
 
35
36
__all__ = ['get_store',
36
37
            'User',
38
39
            'ProjectSet', 'Project', 'ProjectGroup', 'ProjectGroupMembership',
39
40
            'Exercise', 'Worksheet', 'WorksheetExercise',
40
41
            'ExerciseSave', 'ExerciseAttempt',
41
 
            'TestCase', 'TestSuite', 'TestSuiteVar'
 
42
            'AlreadyEnrolledError', 'TestCase', 'TestSuite', 'TestSuiteVar'
42
43
        ]
43
44
 
44
45
def _kwarg_init(self, **kwargs):
52
53
    """
53
54
    Returns the Storm connection string, generated from the conf file.
54
55
    """
55
 
 
56
 
    clusterstr = ''
57
 
    if ivle.conf.db_user:
58
 
        clusterstr += ivle.conf.db_user
59
 
        if ivle.conf.db_password:
60
 
            clusterstr += ':' + ivle.conf.db_password
61
 
        clusterstr += '@'
62
 
 
63
 
    host = ivle.conf.db_host or 'localhost'
64
 
    port = ivle.conf.db_port or 5432
65
 
 
66
 
    clusterstr += '%s:%d' % (host, port)
67
 
 
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,
 
58
        ivle.conf.db_dbname)
69
59
 
70
60
def get_store():
71
61
    """
86
76
    login = Unicode()
87
77
    passhash = Unicode()
88
78
    state = Unicode()
89
 
    admin = Bool()
 
79
    rolenm = Unicode()
90
80
    unixid = Int()
91
81
    nick = Unicode()
92
82
    pass_exp = DateTime()
98
88
    studentid = Unicode()
99
89
    settings = Unicode()
100
90
 
 
91
    def _get_role(self):
 
92
        if self.rolenm is None:
 
93
            return 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)
 
100
 
101
101
    __init__ = _kwarg_init
102
102
 
103
103
    def __repr__(self):
114
114
            return None
115
115
        return self.hash_password(password) == self.passhash
116
116
 
 
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.
 
120
        """
 
121
        return self.role.hasCap(capability)
 
122
 
117
123
    @property
118
124
    def password_expired(self):
119
125
        fieldval = self.pass_exp
195
201
        return store.find(cls, cls.login == unicode(login)).one()
196
202
 
197
203
    def get_permissions(self, user):
198
 
        if user and user.admin or user is self:
 
204
        if user and user.rolenm == 'admin' or user is self:
199
205
            return set(['view', 'edit'])
200
206
        else:
201
207
            return set()
222
228
        perms = set()
223
229
        if user is not None:
224
230
            perms.add('view')
225
 
            if user.admin:
 
231
            if user.rolenm == 'admin':
226
232
                perms.add('edit')
227
233
        return perms
228
234
 
232
238
    id = Int(primary=True, name="semesterid")
233
239
    year = Unicode()
234
240
    semester = Unicode()
235
 
    state = Unicode()
 
241
    active = Bool()
236
242
 
237
243
    offerings = ReferenceSet(id, 'Offering.semester_id')
238
244
 
258
264
                           'User.id')
259
265
    project_sets = ReferenceSet(id, 'ProjectSet.offering_id')
260
266
 
261
 
    worksheets = ReferenceSet(id, 
262
 
        'Worksheet.offering_id', 
263
 
        order_by="Worksheet.seq_no"
264
 
    )
 
267
    worksheets = ReferenceSet(id, 'Worksheet.offering_id')
265
268
 
266
269
    __init__ = _kwarg_init
267
270
 
269
272
        return "<%s %r in %r>" % (type(self).__name__, self.subject,
270
273
                                  self.semester)
271
274
 
272
 
    def enrol(self, user, role=u'student'):
 
275
    def enrol(self, user):
273
276
        '''Enrol a user in this offering.'''
274
 
        enrolment = Store.of(self).find(Enrolment,
 
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,
275
280
                               Enrolment.user_id == user.id,
276
 
                               Enrolment.offering_id == self.id).one()
277
 
 
278
 
        if enrolment is None:
279
 
            enrolment = Enrolment(user=user, offering=self)
280
 
            self.enrolments.add(enrolment)
281
 
 
282
 
        enrolment.active = True
283
 
        enrolment.role = role
 
281
                               Enrolment.offering_id == self.id).count() == 1:
 
282
            raise AlreadyEnrolledError()
 
283
 
 
284
        e = Enrolment(user=user, offering=self, active=True)
 
285
        self.enrolments.add(e)
284
286
 
285
287
    def get_permissions(self, user):
286
288
        perms = set()
287
289
        if user is not None:
288
290
            perms.add('view')
289
 
            if user.admin:
 
291
            if user.rolenm == 'admin':
290
292
                perms.add('edit')
291
293
        return perms
292
294
 
298
300
    user = Reference(user_id, User.id)
299
301
    offering_id = Int(name="offeringid")
300
302
    offering = Reference(offering_id, Offering.id)
301
 
    role = Unicode()
302
303
    notes = Unicode()
303
304
    active = Bool()
304
305
 
316
317
        return "<%s %r in %r>" % (type(self).__name__, self.user,
317
318
                                  self.offering)
318
319
 
 
320
class AlreadyEnrolledError(Exception):
 
321
    pass
 
322
 
319
323
# PROJECTS #
320
324
 
321
325
class ProjectSet(Storm):
392
396
# WORKSHEETS AND EXERCISES #
393
397
 
394
398
class Exercise(Storm):
395
 
    __storm_table__ = "exercise"
 
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
396
403
    id = Unicode(primary=True, name="identifier")
397
404
    name = Unicode()
398
405
    description = Unicode()
414
421
    def __repr__(self):
415
422
        return "<%s %s>" % (type(self).__name__, self.name)
416
423
 
417
 
    def get_permissions(self, user):
418
 
        perms = set()
419
 
        if user is not None:
420
 
            if user.admin:
421
 
                perms.add('edit')
422
 
                perms.add('view')
423
 
        return perms
424
424
 
425
425
class Worksheet(Storm):
426
426
    __storm_table__ = "worksheet"
427
427
 
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.
429
431
    offering_id = Int(name="offeringid")
430
 
    identifier = Unicode()
431
 
    name = Unicode()
 
432
    name = Unicode(name="identifier")
432
433
    assessable = Bool()
433
 
    data = Unicode()
434
 
    seq_no = Int()
435
 
    format = Unicode()
 
434
    mtime = DateTime()
436
435
 
437
436
    attempts = ReferenceSet(id, "ExerciseAttempt.worksheetid")
438
437
    offering = Reference(offering_id, 'Offering.id')
439
438
 
440
 
    all_worksheet_exercises = ReferenceSet(id,
 
439
    exercises = ReferenceSet(id,
 
440
        'WorksheetExercise.worksheet_id',
 
441
        'WorksheetExercise.exercise_id',
 
442
        Exercise.id)
 
443
    # Use worksheet_exercises to get access to the WorksheetExercise objects
 
444
    # binding worksheets to exercises. This is required to access the
 
445
    # "optional" field.
 
446
    worksheet_exercises = ReferenceSet(id,
441
447
        'WorksheetExercise.worksheet_id')
442
 
 
443
 
    # Use worksheet_exercises to get access to the *active* WorksheetExercise
444
 
    # objects binding worksheets to exercises. This is required to access the
445
 
    # "optional" field.
446
 
    @property
447
 
    def worksheet_exercises(self):
448
 
        return self.all_worksheet_exercises.find(active=True)
 
448
        
449
449
 
450
450
    __init__ = _kwarg_init
451
451
 
477
477
        return self.offering.get_permissions(user)
478
478
 
479
479
class WorksheetExercise(Storm):
480
 
    __storm_table__ = "worksheet_exercise"
481
 
    
482
 
    id = Int(primary=True, name="ws_ex_id")
 
480
    __storm_table__ = "worksheet_problem"
 
481
    __storm_primary__ = "worksheet_id", "exercise_id"
483
482
 
484
483
    worksheet_id = Int(name="worksheetid")
485
484
    worksheet = Reference(worksheet_id, Worksheet.id)
486
 
    exercise_id = Unicode(name="exerciseid")
 
485
    exercise_id = Unicode(name="problemid")
487
486
    exercise = Reference(exercise_id, Exercise.id)
488
487
    optional = Bool()
489
 
    active = Bool()
490
 
    seq_no = Int()
491
 
    
492
 
    saves = ReferenceSet(id, "ExerciseSave.ws_ex_id")
493
 
    attempts = ReferenceSet(id, "ExerciseAttempt.ws_ex_id")
494
488
 
495
489
    __init__ = _kwarg_init
496
490
 
497
491
    def __repr__(self):
498
492
        return "<%s %s in %s>" % (type(self).__name__, self.exercise.name,
499
 
                                  self.worksheet.identifier)
 
493
                                  self.worksheet.name)
500
494
 
501
495
class ExerciseSave(Storm):
502
496
    """
507
501
    ExerciseSave may be extended with additional semantics (such as
508
502
    ExerciseAttempt).
509
503
    """
510
 
    __storm_table__ = "exercise_save"
511
 
    __storm_primary__ = "ws_ex_id", "user_id"
512
 
 
513
 
    ws_ex_id = Int(name="ws_ex_id")
514
 
    worksheet_exercise = Reference(ws_ex_id, "WorksheetExercise.id")
515
 
 
 
504
    __storm_table__ = "problem_save"
 
505
    __storm_primary__ = "exercise_id", "user_id", "date"
 
506
 
 
507
    exercise_id = Unicode(name="problemid")
 
508
    exercise = Reference(exercise_id, Exercise.id)
516
509
    user_id = Int(name="loginid")
517
510
    user = Reference(user_id, User.id)
518
511
    date = DateTime()
519
512
    text = Unicode()
 
513
    worksheetid = Int()
 
514
    worksheet = Reference(worksheetid, Worksheet.id)
520
515
 
521
516
    __init__ = _kwarg_init
522
517
 
537
532
        they won't count (either as a penalty or success), but will still be
538
533
        stored.
539
534
    """
540
 
    __storm_table__ = "exercise_attempt"
541
 
    __storm_primary__ = "ws_ex_id", "user_id", "date"
 
535
    __storm_table__ = "problem_attempt"
 
536
    __storm_primary__ = "exercise_id", "user_id", "date"
542
537
 
543
538
    # The "text" field is the same but has a different name in the DB table
544
539
    # for some reason.
555
550
    __storm_primary__ = "exercise_id", "suiteid"
556
551
    
557
552
    suiteid = Int()
558
 
    exercise_id = Unicode(name="exerciseid")
 
553
    exercise_id = Unicode(name="problemid")
559
554
    description = Unicode()
560
555
    seq_no = Int()
561
556
    function = Unicode()
585
580
 
586
581
class TestSuiteVar(Storm):
587
582
    """A container for the arguments of a Test Suite"""
588
 
    __storm_table__ = "suite_variable"
 
583
    __storm_table__ = "suite_variables"
589
584
    __storm_primary__ = "varid"
590
585
    
591
586
    varid = Int()
601
596
    
602
597
class TestCasePart(Storm):
603
598
    """A container for the test elements of a Test Case"""
604
 
    __storm_table__ = "test_case_part"
 
599
    __storm_table__ = "test_case_parts"
605
600
    __storm_primary__ = "partid"
606
601
    
607
602
    partid = Int()