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

1080.1.2 by matt.giuca
New module: ivle.database. Classes and utilities for Storm ORM.
1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2009 The University of Melbourne
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18
# Author: Matt Giuca, Will Grant
19
20
"""
21
Database Classes and Utilities for Storm ORM
22
23
This module provides all of the classes which map to database tables.
24
It also provides miscellaneous utility functions for database interaction.
25
"""
26
1080.1.13 by me at id
ivle.database.User: Add an authenticate() method, and a hash_password()
27
import md5
1080.1.15 by me at id
Give ivle.database.User {password,account}_expired attributes, and get
28
import datetime
1080.1.13 by me at id
ivle.database.User: Add an authenticate() method, and a hash_password()
29
1080.1.4 by matt.giuca
ivle.database: Added User class.
30
from storm.locals import create_database, Store, Int, Unicode, DateTime, \
1080.1.27 by me at id
ivle.database.User: Add an 'active_enrolments' property, which returns a list
31
                         Reference, ReferenceSet, Bool, Storm, Desc
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
32
from storm.exceptions import NotOneError, IntegrityError
1080.1.2 by matt.giuca
New module: ivle.database. Classes and utilities for Storm ORM.
33
34
import ivle.conf
1099.1.220 by Nick Chadwick
Merged from trunk
35
from ivle.worksheet.rst import rst
1080.1.2 by matt.giuca
New module: ivle.database. Classes and utilities for Storm ORM.
36
1080.1.39 by Matt Giuca
ivle.database: Added __all__ to the top of the file.
37
__all__ = ['get_store',
38
            'User',
39
            'Subject', 'Semester', 'Offering', 'Enrolment',
40
            'ProjectSet', 'Project', 'ProjectGroup', 'ProjectGroupMembership',
1165.1.4 by William Grant
Add database classes for assessed, project_extension and project_submission.
41
            'Assessed', 'ProjectSubmission', 'ProjectExtension',
1080.1.59 by Matt Giuca
ivle.worksheet, ivle.database: Added/updated __all__.
42
            'Exercise', 'Worksheet', 'WorksheetExercise',
1080.1.61 by William Grant
ivle.database: Add an Offering.enrol(user) method, which enrols the user in
43
            'ExerciseSave', 'ExerciseAttempt',
1110 by William Grant
ivle-enrol now allows updating of existing enrolments. It also sets the role.
44
            'TestCase', 'TestSuite', 'TestSuiteVar'
1080.1.39 by Matt Giuca
ivle.database: Added __all__ to the top of the file.
45
        ]
46
1080.1.25 by me at id
ivle.database: Add Subject, Semester and Offering.
47
def _kwarg_init(self, **kwargs):
48
    for k,v in kwargs.items():
1080.1.46 by William Grant
ivle.database._kwarg_init: Check with hasattr() on the class, not the object,
49
        if k.startswith('_') or not hasattr(self.__class__, k):
1080.1.25 by me at id
ivle.database: Add Subject, Semester and Offering.
50
            raise TypeError("%s got an unexpected keyword argument '%s'"
1080.1.45 by William Grant
ivle.database._kwarg_init: Fix exception throwing.
51
                % (self.__class__.__name__, k))
1080.1.25 by me at id
ivle.database: Add Subject, Semester and Offering.
52
        setattr(self, k, v)
53
1080.1.2 by matt.giuca
New module: ivle.database. Classes and utilities for Storm ORM.
54
def get_conn_string():
55
    """
56
    Returns the Storm connection string, generated from the conf file.
57
    """
1099.1.174 by William Grant
ivle.database.get_conn_string() now defaults to localhost:5432, rather than
58
59
    clusterstr = ''
60
    if ivle.conf.db_user:
61
        clusterstr += ivle.conf.db_user
62
        if ivle.conf.db_password:
63
            clusterstr += ':' + ivle.conf.db_password
64
        clusterstr += '@'
65
66
    host = ivle.conf.db_host or 'localhost'
67
    port = ivle.conf.db_port or 5432
68
69
    clusterstr += '%s:%d' % (host, port)
70
71
    return "postgres://%s/%s" % (clusterstr, ivle.conf.db_dbname)
1080.1.2 by matt.giuca
New module: ivle.database. Classes and utilities for Storm ORM.
72
73
def get_store():
74
    """
75
    Open a database connection and transaction. Return a storm.store.Store
76
    instance connected to the configured IVLE database.
77
    """
78
    return Store(create_database(get_conn_string()))
1080.1.4 by matt.giuca
ivle.database: Added User class.
79
1080.1.39 by Matt Giuca
ivle.database: Added __all__ to the top of the file.
80
# USERS #
81
1080.1.26 by me at id
ivle.database: Add an Enrolment class, and reference(set)s between all of the
82
class User(Storm):
1080.1.4 by matt.giuca
ivle.database: Added User class.
83
    """
84
    Represents an IVLE user.
85
    """
86
    __storm_table__ = "login"
87
88
    id = Int(primary=True, name="loginid")
89
    login = Unicode()
90
    passhash = Unicode()
91
    state = Unicode()
1101 by William Grant
Privileges (apart from admin) are now offering-local, not global.
92
    admin = Bool()
1080.1.4 by matt.giuca
ivle.database: Added User class.
93
    unixid = Int()
94
    nick = Unicode()
95
    pass_exp = DateTime()
96
    acct_exp = DateTime()
97
    last_login = DateTime()
98
    svn_pass = Unicode()
99
    email = Unicode()
100
    fullname = Unicode()
101
    studentid = Unicode()
102
    settings = Unicode()
103
1080.1.25 by me at id
ivle.database: Add Subject, Semester and Offering.
104
    __init__ = _kwarg_init
1080.1.4 by matt.giuca
ivle.database: Added User class.
105
106
    def __repr__(self):
107
        return "<%s '%s'>" % (type(self).__name__, self.login)
1080.1.5 by matt.giuca
ivle.database.User: Add the missing methods from ivle.user.User.
108
1080.1.13 by me at id
ivle.database.User: Add an authenticate() method, and a hash_password()
109
    def authenticate(self, password):
110
        """Validate a given password against this user.
111
112
        Returns True if the given password matches the password hash for this
113
        User, False if it doesn't match, and None if there is no hash for the
114
        user.
115
        """
116
        if self.passhash is None:
117
            return None
118
        return self.hash_password(password) == self.passhash
119
1080.1.15 by me at id
Give ivle.database.User {password,account}_expired attributes, and get
120
    @property
1165.1.26 by William Grant
Add display_name properties to users and groups.
121
    def display_name(self):
122
        return self.fullname
123
124
    @property
1080.1.15 by me at id
Give ivle.database.User {password,account}_expired attributes, and get
125
    def password_expired(self):
1080.1.5 by matt.giuca
ivle.database.User: Add the missing methods from ivle.user.User.
126
        fieldval = self.pass_exp
1080.1.15 by me at id
Give ivle.database.User {password,account}_expired attributes, and get
127
        return fieldval is not None and datetime.datetime.now() > fieldval
128
129
    @property
130
    def account_expired(self):
1080.1.5 by matt.giuca
ivle.database.User: Add the missing methods from ivle.user.User.
131
        fieldval = self.acct_exp
1080.1.15 by me at id
Give ivle.database.User {password,account}_expired attributes, and get
132
        return fieldval is not None and datetime.datetime.now() > fieldval
1080.1.6 by matt.giuca
ivle.database.User: Added get_by_login method.
133
1099.1.121 by William Grant
Don't set req.user unless the login in the session specifies a valid user.
134
    @property
135
    def valid(self):
136
        return self.state == 'enabled' and not self.account_expired
137
1080.1.29 by me at id
ivle.database.User: Order 'enrolments' the same way as 'active_enrolments'.
138
    def _get_enrolments(self, justactive):
1080.1.27 by me at id
ivle.database.User: Add an 'active_enrolments' property, which returns a list
139
        return Store.of(self).find(Enrolment,
140
            Enrolment.user_id == self.id,
1080.1.29 by me at id
ivle.database.User: Order 'enrolments' the same way as 'active_enrolments'.
141
            (Enrolment.active == True) if justactive else True,
1080.1.27 by me at id
ivle.database.User: Add an 'active_enrolments' property, which returns a list
142
            Enrolment.offering_id == Offering.id,
143
            Offering.semester_id == Semester.id,
144
            Offering.subject_id == Subject.id).order_by(
145
                Desc(Semester.year),
146
                Desc(Semester.semester),
147
                Desc(Subject.code)
148
            )
149
1080.1.68 by William Grant
ivle.database.User: Add a write-only 'password' attribute. When set, it will
150
    def _set_password(self, password):
151
        if password is None:
152
            self.passhash = None
153
        else:
154
            self.passhash = unicode(User.hash_password(password))
155
    password = property(fset=_set_password)
156
1080.1.29 by me at id
ivle.database.User: Order 'enrolments' the same way as 'active_enrolments'.
157
    @property
1080.1.31 by me at id
ivle.database.User: Add 'subjects', an attribute containing currently
158
    def subjects(self):
159
        return Store.of(self).find(Subject,
160
            Enrolment.user_id == self.id,
161
            Enrolment.active == True,
162
            Offering.id == Enrolment.offering_id,
163
            Subject.id == Offering.subject_id).config(distinct=True)
164
1080.1.36 by William Grant
ivle.database: Add ProjectSet, Project, ProjectGroup, ProjectGroupMembership
165
    # TODO: Invitations should be listed too?
166
    def get_groups(self, offering=None):
167
        preds = [
168
            ProjectGroupMembership.user_id == self.id,
169
            ProjectGroup.id == ProjectGroupMembership.project_group_id,
170
        ]
171
        if offering:
172
            preds.extend([
173
                ProjectSet.offering_id == offering.id,
174
                ProjectGroup.project_set_id == ProjectSet.id,
175
            ])
176
        return Store.of(self).find(ProjectGroup, *preds)
177
178
    @property
179
    def groups(self):
180
        return self.get_groups()
181
1080.1.31 by me at id
ivle.database.User: Add 'subjects', an attribute containing currently
182
    @property
1080.1.29 by me at id
ivle.database.User: Order 'enrolments' the same way as 'active_enrolments'.
183
    def active_enrolments(self):
184
        '''A sanely ordered list of the user's active enrolments.'''
185
        return self._get_enrolments(True)
186
187
    @property
188
    def enrolments(self):
189
        '''A sanely ordered list of all of the user's enrolments.'''
190
        return self._get_enrolments(False) 
1080.1.27 by me at id
ivle.database.User: Add an 'active_enrolments' property, which returns a list
191
1165.1.11 by William Grant
Let callsites ask User.get_projects() to show inactive offerings too.
192
    def get_projects(self, offering=None, active_only=True):
1165.1.10 by William Grant
Add User.get_projects(), returning a list of submission targets.
193
        '''Return Projects that the user can submit.
194
1165.1.11 by William Grant
Let callsites ask User.get_projects() to show inactive offerings too.
195
        This will include projects for offerings in which the user is
1165.1.10 by William Grant
Add User.get_projects(), returning a list of submission targets.
196
        enrolled, as long as the project is not in a project set which has
197
        groups (ie. if maximum number of group members is 0).
198
1165.1.11 by William Grant
Let callsites ask User.get_projects() to show inactive offerings too.
199
        Unless active_only is False, only projects for active offerings will
200
        be returned.
201
1165.1.10 by William Grant
Add User.get_projects(), returning a list of submission targets.
202
        If an offering is specified, returned projects will be limited to
203
        those for that offering.
204
        '''
205
        return Store.of(self).find(Project,
206
            Project.project_set_id == ProjectSet.id,
207
            ProjectSet.max_students_per_group == 0,
208
            ProjectSet.offering_id == Offering.id,
1165.1.11 by William Grant
Let callsites ask User.get_projects() to show inactive offerings too.
209
            (offering is None) or (Offering.id == offering.id),
1165.1.10 by William Grant
Add User.get_projects(), returning a list of submission targets.
210
            Semester.id == Offering.semester_id,
1165.1.11 by William Grant
Let callsites ask User.get_projects() to show inactive offerings too.
211
            (not active_only) or (Semester.state == u'current'),
1165.1.10 by William Grant
Add User.get_projects(), returning a list of submission targets.
212
            Enrolment.offering_id == Offering.id,
213
            Enrolment.user_id == self.id)
214
1080.1.13 by me at id
ivle.database.User: Add an authenticate() method, and a hash_password()
215
    @staticmethod
216
    def hash_password(password):
217
        return md5.md5(password).hexdigest()
218
1080.1.6 by matt.giuca
ivle.database.User: Added get_by_login method.
219
    @classmethod
220
    def get_by_login(cls, store, login):
221
        """
222
        Get the User from the db associated with a given store and
223
        login.
224
        """
1080.1.7 by matt.giuca
The new ivle.database.User class is now used in Request and usrmgt, which
225
        return store.find(cls, cls.login == unicode(login)).one()
1080.1.25 by me at id
ivle.database: Add Subject, Semester and Offering.
226
1099.1.110 by William Grant
Implement an authorization system in the new framework. This breaks the REST
227
    def get_permissions(self, user):
1101 by William Grant
Privileges (apart from admin) are now offering-local, not global.
228
        if user and user.admin or user is self:
1165.1.7 by William Grant
Grant submit_project on users to themselves, and on groups to their members.
229
            return set(['view', 'edit', 'submit_project'])
1099.1.110 by William Grant
Implement an authorization system in the new framework. This breaks the REST
230
        else:
231
            return set()
232
1080.1.39 by Matt Giuca
ivle.database: Added __all__ to the top of the file.
233
# SUBJECTS AND ENROLMENTS #
234
1080.1.26 by me at id
ivle.database: Add an Enrolment class, and reference(set)s between all of the
235
class Subject(Storm):
1080.1.25 by me at id
ivle.database: Add Subject, Semester and Offering.
236
    __storm_table__ = "subject"
237
238
    id = Int(primary=True, name="subjectid")
239
    code = Unicode(name="subj_code")
240
    name = Unicode(name="subj_name")
241
    short_name = Unicode(name="subj_short_name")
242
    url = Unicode()
243
1080.1.26 by me at id
ivle.database: Add an Enrolment class, and reference(set)s between all of the
244
    offerings = ReferenceSet(id, 'Offering.subject_id')
245
1080.1.25 by me at id
ivle.database: Add Subject, Semester and Offering.
246
    __init__ = _kwarg_init
247
248
    def __repr__(self):
249
        return "<%s '%s'>" % (type(self).__name__, self.short_name)
250
1099.1.110 by William Grant
Implement an authorization system in the new framework. This breaks the REST
251
    def get_permissions(self, user):
252
        perms = set()
253
        if user is not None:
254
            perms.add('view')
1101 by William Grant
Privileges (apart from admin) are now offering-local, not global.
255
            if user.admin:
1099.1.141 by Nick Chadwick
Updated the exercises to be loaded from the database, not a local file.
256
                perms.add('edit')
1099.1.110 by William Grant
Implement an authorization system in the new framework. This breaks the REST
257
        return perms
258
1080.1.26 by me at id
ivle.database: Add an Enrolment class, and reference(set)s between all of the
259
class Semester(Storm):
1080.1.25 by me at id
ivle.database: Add Subject, Semester and Offering.
260
    __storm_table__ = "semester"
261
262
    id = Int(primary=True, name="semesterid")
263
    year = Unicode()
264
    semester = Unicode()
1104 by William Grant
Replace Semester.active with Semester.state, allowing more useful state
265
    state = Unicode()
1080.1.25 by me at id
ivle.database: Add Subject, Semester and Offering.
266
1080.1.26 by me at id
ivle.database: Add an Enrolment class, and reference(set)s between all of the
267
    offerings = ReferenceSet(id, 'Offering.semester_id')
1124 by William Grant
Add Semester.enrolments.
268
    enrolments = ReferenceSet(id,
269
                              'Offering.semester_id',
270
                              'Offering.id',
271
                              'Enrolment.offering_id')
1080.1.26 by me at id
ivle.database: Add an Enrolment class, and reference(set)s between all of the
272
1080.1.25 by me at id
ivle.database: Add Subject, Semester and Offering.
273
    __init__ = _kwarg_init
274
275
    def __repr__(self):
276
        return "<%s %s/%s>" % (type(self).__name__, self.year, self.semester)
277
1080.1.26 by me at id
ivle.database: Add an Enrolment class, and reference(set)s between all of the
278
class Offering(Storm):
1080.1.25 by me at id
ivle.database: Add Subject, Semester and Offering.
279
    __storm_table__ = "offering"
280
281
    id = Int(primary=True, name="offeringid")
282
    subject_id = Int(name="subject")
283
    subject = Reference(subject_id, Subject.id)
284
    semester_id = Int(name="semesterid")
285
    semester = Reference(semester_id, Semester.id)
286
    groups_student_permissions = Unicode()
287
1080.1.26 by me at id
ivle.database: Add an Enrolment class, and reference(set)s between all of the
288
    enrolments = ReferenceSet(id, 'Enrolment.offering_id')
1080.1.79 by William Grant
ivle.database.Offering: Add a members ReferenceSet.
289
    members = ReferenceSet(id,
290
                           'Enrolment.offering_id',
291
                           'Enrolment.user_id',
292
                           'User.id')
1080.1.76 by William Grant
ivle.database.Offering: Add project_sets referenceset.
293
    project_sets = ReferenceSet(id, 'ProjectSet.offering_id')
1080.1.26 by me at id
ivle.database: Add an Enrolment class, and reference(set)s between all of the
294
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
295
    worksheets = ReferenceSet(id, 
296
        'Worksheet.offering_id', 
1099.1.212 by Nick Chadwick
Added a new page to display exercises. This will then be modified to
297
        order_by="seq_no"
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
298
    )
1099.1.114 by Nick Chadwick
Modified the database so that exercises are now stored in the database, rather
299
1080.1.25 by me at id
ivle.database: Add Subject, Semester and Offering.
300
    __init__ = _kwarg_init
301
302
    def __repr__(self):
303
        return "<%s %r in %r>" % (type(self).__name__, self.subject,
304
                                  self.semester)
1080.1.26 by me at id
ivle.database: Add an Enrolment class, and reference(set)s between all of the
305
1110 by William Grant
ivle-enrol now allows updating of existing enrolments. It also sets the role.
306
    def enrol(self, user, role=u'student'):
1080.1.61 by William Grant
ivle.database: Add an Offering.enrol(user) method, which enrols the user in
307
        '''Enrol a user in this offering.'''
1110 by William Grant
ivle-enrol now allows updating of existing enrolments. It also sets the role.
308
        enrolment = Store.of(self).find(Enrolment,
1080.1.61 by William Grant
ivle.database: Add an Offering.enrol(user) method, which enrols the user in
309
                               Enrolment.user_id == user.id,
1110 by William Grant
ivle-enrol now allows updating of existing enrolments. It also sets the role.
310
                               Enrolment.offering_id == self.id).one()
311
312
        if enrolment is None:
313
            enrolment = Enrolment(user=user, offering=self)
314
            self.enrolments.add(enrolment)
315
316
        enrolment.active = True
317
        enrolment.role = role
1080.1.61 by William Grant
ivle.database: Add an Offering.enrol(user) method, which enrols the user in
318
1132 by William Grant
Add Offering.unenrol(), to unenrol a user from an offering.
319
    def unenrol(self, user):
320
        '''Unenrol a user from this offering.'''
321
        enrolment = Store.of(self).find(Enrolment,
322
                               Enrolment.user_id == user.id,
323
                               Enrolment.offering_id == self.id).one()
324
        Store.of(enrolment).remove(enrolment)
325
1099.1.141 by Nick Chadwick
Updated the exercises to be loaded from the database, not a local file.
326
    def get_permissions(self, user):
327
        perms = set()
328
        if user is not None:
1131 by William Grant
Offerings now give 'view' only to user enrolled in them. 'edit' is granted
329
            enrolment = self.get_enrolment(user)
330
            if enrolment or user.admin:
331
                perms.add('view')
332
            if (enrolment and enrolment.role in (u'tutor', u'lecturer')) \
333
               or user.admin:
1099.1.141 by Nick Chadwick
Updated the exercises to be loaded from the database, not a local file.
334
                perms.add('edit')
335
        return perms
336
1129 by William Grant
Move the group admin view to per-offering.
337
    def get_enrolment(self, user):
338
        try:
339
            enrolment = self.enrolments.find(user=user).one()
340
        except NotOneError:
341
            enrolment = None
342
343
        return enrolment
344
1080.1.26 by me at id
ivle.database: Add an Enrolment class, and reference(set)s between all of the
345
class Enrolment(Storm):
346
    __storm_table__ = "enrolment"
347
    __storm_primary__ = "user_id", "offering_id"
348
349
    user_id = Int(name="loginid")
350
    user = Reference(user_id, User.id)
351
    offering_id = Int(name="offeringid")
352
    offering = Reference(offering_id, Offering.id)
1101 by William Grant
Privileges (apart from admin) are now offering-local, not global.
353
    role = Unicode()
1080.1.26 by me at id
ivle.database: Add an Enrolment class, and reference(set)s between all of the
354
    notes = Unicode()
355
    active = Bool()
356
1080.1.81 by William Grant
ivle.database.Enrolment: Add a groups attribute, containing groups of which
357
    @property
358
    def groups(self):
359
        return Store.of(self).find(ProjectGroup,
360
                ProjectSet.offering_id == self.offering.id,
361
                ProjectGroup.project_set_id == ProjectSet.id,
362
                ProjectGroupMembership.project_group_id == ProjectGroup.id,
363
                ProjectGroupMembership.user_id == self.user.id)
364
1080.1.26 by me at id
ivle.database: Add an Enrolment class, and reference(set)s between all of the
365
    __init__ = _kwarg_init
366
367
    def __repr__(self):
368
        return "<%s %r in %r>" % (type(self).__name__, self.user,
369
                                  self.offering)
1080.1.36 by William Grant
ivle.database: Add ProjectSet, Project, ProjectGroup, ProjectGroupMembership
370
1080.1.39 by Matt Giuca
ivle.database: Added __all__ to the top of the file.
371
# PROJECTS #
372
1080.1.36 by William Grant
ivle.database: Add ProjectSet, Project, ProjectGroup, ProjectGroupMembership
373
class ProjectSet(Storm):
374
    __storm_table__ = "project_set"
375
376
    id = Int(name="projectsetid", primary=True)
377
    offering_id = Int(name="offeringid")
378
    offering = Reference(offering_id, Offering.id)
379
    max_students_per_group = Int()
380
1080.1.77 by William Grant
ivle.database.ProjectSet: Add projects and project_groups referencesets.
381
    projects = ReferenceSet(id, 'Project.project_set_id')
382
    project_groups = ReferenceSet(id, 'ProjectGroup.project_set_id')
383
1080.1.36 by William Grant
ivle.database: Add ProjectSet, Project, ProjectGroup, ProjectGroupMembership
384
    __init__ = _kwarg_init
385
386
    def __repr__(self):
387
        return "<%s %d in %r>" % (type(self).__name__, self.id,
388
                                  self.offering)
389
390
class Project(Storm):
391
    __storm_table__ = "project"
392
393
    id = Int(name="projectid", primary=True)
1165.1.4 by William Grant
Add database classes for assessed, project_extension and project_submission.
394
    name = Unicode()
395
    short_name = Unicode()
1080.1.36 by William Grant
ivle.database: Add ProjectSet, Project, ProjectGroup, ProjectGroupMembership
396
    synopsis = Unicode()
397
    url = Unicode()
398
    project_set_id = Int(name="projectsetid")
399
    project_set = Reference(project_set_id, ProjectSet.id)
400
    deadline = DateTime()
401
1165.1.5 by William Grant
Add relevant ReferenceSets to Project and Assessed.
402
    assesseds = ReferenceSet(id, 'Assessed.project_id')
403
    submissions = ReferenceSet(id,
404
                               'Assessed.project_id',
405
                               'Assessed.id',
406
                               'ProjectSubmission.assessed_id')
407
1080.1.36 by William Grant
ivle.database: Add ProjectSet, Project, ProjectGroup, ProjectGroupMembership
408
    __init__ = _kwarg_init
409
410
    def __repr__(self):
1165.1.4 by William Grant
Add database classes for assessed, project_extension and project_submission.
411
        return "<%s '%s' in %r>" % (type(self).__name__, self.short_name,
1080.1.36 by William Grant
ivle.database: Add ProjectSet, Project, ProjectGroup, ProjectGroupMembership
412
                                  self.project_set.offering)
413
1165.1.19 by William Grant
Add Project.submit(), to create a submission for the principal, path and rev.
414
    def can_submit(self, principal):
415
        return (self in principal.get_projects() and
416
                self.deadline > datetime.datetime.now())
417
418
    def submit(self, principal, path, revision):
419
        if not self.can_submit(principal):
420
            raise Exception('cannot submit')
421
422
        a = Assessed.get(Store.of(self), principal, self)
423
        ps = ProjectSubmission()
424
        ps.path = path
425
        ps.revision = revision
426
        ps.date_submitted = datetime.datetime.now()
427
        ps.assessed = a
428
429
        return ps
430
431
1080.1.36 by William Grant
ivle.database: Add ProjectSet, Project, ProjectGroup, ProjectGroupMembership
432
class ProjectGroup(Storm):
433
    __storm_table__ = "project_group"
434
435
    id = Int(name="groupid", primary=True)
436
    name = Unicode(name="groupnm")
437
    project_set_id = Int(name="projectsetid")
438
    project_set = Reference(project_set_id, ProjectSet.id)
439
    nick = Unicode()
440
    created_by_id = Int(name="createdby")
441
    created_by = Reference(created_by_id, User.id)
442
    epoch = DateTime()
443
1080.1.78 by William Grant
ivle.database.ProjectGroup.members: Use a ReferenceSet.
444
    members = ReferenceSet(id,
445
                           "ProjectGroupMembership.project_group_id",
446
                           "ProjectGroupMembership.user_id",
447
                           "User.id")
448
1080.1.36 by William Grant
ivle.database: Add ProjectSet, Project, ProjectGroup, ProjectGroupMembership
449
    __init__ = _kwarg_init
450
451
    def __repr__(self):
452
        return "<%s %s in %r>" % (type(self).__name__, self.name,
453
                                  self.project_set.offering)
454
1165.1.26 by William Grant
Add display_name properties to users and groups.
455
    @property
456
    def display_name(self):
457
        return '%s (%s)' % (self.nick, self.name)
458
1165.1.12 by William Grant
Implement ProjectGroup.get_projects(), with identical interface.
459
    def get_projects(self, offering=None, active_only=True):
460
        '''Return Projects that the group can submit.
461
462
        This will include projects in the project set which owns this group,
463
        unless the project set disallows groups (in which case none will be
464
        returned).
465
466
        Unless active_only is False, projects will only be returned if the
467
        group's offering is active.
468
469
        If an offering is specified, projects will only be returned if it
470
        matches the group's.
471
        '''
472
        return Store.of(self).find(Project,
473
            Project.project_set_id == ProjectSet.id,
474
            ProjectSet.id == self.project_set.id,
475
            ProjectSet.max_students_per_group > 0,
476
            ProjectSet.offering_id == Offering.id,
477
            (offering is None) or (Offering.id == offering.id),
478
            Semester.id == Offering.semester_id,
479
            (not active_only) or (Semester.state == u'current'))
480
481
1165.1.7 by William Grant
Grant submit_project on users to themselves, and on groups to their members.
482
    def get_permissions(self, user):
483
        if user.admin or user in self.members:
484
            return set(['submit_project'])
485
        else:
486
            return set()
487
1080.1.36 by William Grant
ivle.database: Add ProjectSet, Project, ProjectGroup, ProjectGroupMembership
488
class ProjectGroupMembership(Storm):
489
    __storm_table__ = "group_member"
490
    __storm_primary__ = "user_id", "project_group_id"
491
492
    user_id = Int(name="loginid")
493
    user = Reference(user_id, User.id)
494
    project_group_id = Int(name="groupid")
495
    project_group = Reference(project_group_id, ProjectGroup.id)
496
497
    __init__ = _kwarg_init
498
499
    def __repr__(self):
500
        return "<%s %r in %r>" % (type(self).__name__, self.user,
501
                                  self.project_group)
502
1165.1.4 by William Grant
Add database classes for assessed, project_extension and project_submission.
503
class Assessed(Storm):
504
    __storm_table__ = "assessed"
505
506
    id = Int(name="assessedid", primary=True)
507
    user_id = Int(name="loginid")
508
    user = Reference(user_id, User.id)
509
    project_group_id = Int(name="groupid")
510
    project_group = Reference(project_group_id, ProjectGroup.id)
511
512
    project_id = Int(name="projectid")
513
    project = Reference(project_id, Project.id)
514
1165.1.5 by William Grant
Add relevant ReferenceSets to Project and Assessed.
515
    extensions = ReferenceSet(id, 'ProjectExtension.assessed_id')
516
    submissions = ReferenceSet(id, 'ProjectSubmission.assessed_id')
517
1165.1.4 by William Grant
Add database classes for assessed, project_extension and project_submission.
518
    def __repr__(self):
519
        return "<%s %r in %r>" % (type(self).__name__,
520
            self.user or self.project_group, self.project)
521
1165.1.18 by William Grant
Add a method to retrieve or create an Assessed given a principal and project.
522
    @classmethod
523
    def get(cls, store, principal, project):
524
        t = type(principal)
525
        if t not in (User, ProjectGroup):
526
            raise AssertionError('principal must be User or ProjectGroup')
527
528
        a = store.find(cls,
529
            (t is User) or (cls.project_group_id == principal.id),
530
            (t is ProjectGroup) or (cls.user_id == principal.id),
531
            Project.id == project.id).one()
532
533
        if a is None:
534
            a = cls()
535
            if t is User:
536
                a.user = principal
537
            else:
538
                a.project_group = principal
539
            a.project = project
540
            store.add(a)
541
542
        return a
543
544
1165.1.4 by William Grant
Add database classes for assessed, project_extension and project_submission.
545
class ProjectExtension(Storm):
546
    __storm_table__ = "project_extension"
547
548
    id = Int(name="extensionid", primary=True)
549
    assessed_id = Int(name="assessedid")
550
    assessed = Reference(assessed_id, Assessed.id)
551
    deadline = DateTime()
552
    approver_id = Int(name="approver")
553
    approver = Reference(approver_id, User.id)
554
    notes = Unicode()
555
556
class ProjectSubmission(Storm):
557
    __storm_table__ = "project_submission"
558
559
    id = Int(name="submissionid", primary=True)
560
    assessed_id = Int(name="assessedid")
561
    assessed = Reference(assessed_id, Assessed.id)
562
    path = Unicode()
563
    revision = Int()
564
    date_submitted = DateTime()
565
566
1080.1.40 by Matt Giuca
ivle.database: Added Worksheet and Exercise classes (more to come in this
567
# WORKSHEETS AND EXERCISES #
568
569
class Exercise(Storm):
1099.1.195 by William Grant
Rename problem to exercise in the DB.
570
    __storm_table__ = "exercise"
1099.1.114 by Nick Chadwick
Modified the database so that exercises are now stored in the database, rather
571
    id = Unicode(primary=True, name="identifier")
572
    name = Unicode()
573
    description = Unicode()
574
    partial = Unicode()
575
    solution = Unicode()
576
    include = Unicode()
577
    num_rows = Int()
1080.1.40 by Matt Giuca
ivle.database: Added Worksheet and Exercise classes (more to come in this
578
1099.6.2 by Nick Chadwick
Added a listing of all exercises
579
    worksheet_exercises =  ReferenceSet(id,
580
        'WorksheetExercise.exercise_id')
581
1080.1.50 by Matt Giuca
ivle.database: Added WorksheetExercise (relates worksheets to exercises), and
582
    worksheets = ReferenceSet(id,
583
        'WorksheetExercise.exercise_id',
584
        'WorksheetExercise.worksheet_id',
585
        'Worksheet.id'
586
    )
1099.1.114 by Nick Chadwick
Modified the database so that exercises are now stored in the database, rather
587
    
1099.1.212 by Nick Chadwick
Added a new page to display exercises. This will then be modified to
588
    test_suites = ReferenceSet(id, 
589
        'TestSuite.exercise_id',
590
        order_by='seq_no')
1080.1.50 by Matt Giuca
ivle.database: Added WorksheetExercise (relates worksheets to exercises), and
591
1080.1.40 by Matt Giuca
ivle.database: Added Worksheet and Exercise classes (more to come in this
592
    __init__ = _kwarg_init
593
594
    def __repr__(self):
595
        return "<%s %s>" % (type(self).__name__, self.name)
596
1099.1.210 by Nick Chadwick
Modified the database layer so that exercises have a get_permissions
597
    def get_permissions(self, user):
598
        perms = set()
1099.1.234 by Nick Chadwick
Permissions for editing and deleting exercises now come from the
599
        roles = set()
1099.1.210 by Nick Chadwick
Modified the database layer so that exercises have a get_permissions
600
        if user is not None:
1101 by William Grant
Privileges (apart from admin) are now offering-local, not global.
601
            if user.admin:
1099.1.210 by Nick Chadwick
Modified the database layer so that exercises have a get_permissions
602
                perms.add('edit')
603
                perms.add('view')
1099.1.236 by Nick Chadwick
Fixed a syntax error.
604
            elif 'lecturer' in set((e.role for e in user.active_enrolments)):
1099.1.234 by Nick Chadwick
Permissions for editing and deleting exercises now come from the
605
                perms.add('edit')
606
                perms.add('view')
1099.1.235 by Nick Chadwick
Made checking if a user is a lecturer in exercise get_permissions
607
            
1099.1.210 by Nick Chadwick
Modified the database layer so that exercises have a get_permissions
608
        return perms
1099.6.3 by Nick Chadwick
Edited the exercise service to delete individual parts of an exercise.
609
    
610
    def get_description(self):
1099.1.232 by Nick Chadwick
Removed XML from database. RST now generates a full xml document, not
611
        return rst(self.description)
1080.1.51 by Matt Giuca
tutorial: Replaced call to ivle.db.create_worksheet with local code (roughly
612
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
613
    def delete(self):
614
        """Deletes the exercise, providing it has no associated worksheets."""
615
        if (self.worksheet_exercises.count() > 0):
616
            raise IntegrityError()
1099.1.233 by Nick Chadwick
Exercise objects in the database module, along with their test cases,
617
        for suite in self.test_suites:
618
            suite.delete()
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
619
        Store.of(self).remove(self)
1099.1.233 by Nick Chadwick
Exercise objects in the database module, along with their test cases,
620
1080.1.40 by Matt Giuca
ivle.database: Added Worksheet and Exercise classes (more to come in this
621
class Worksheet(Storm):
622
    __storm_table__ = "worksheet"
623
624
    id = Int(primary=True, name="worksheetid")
1099.1.114 by Nick Chadwick
Modified the database so that exercises are now stored in the database, rather
625
    offering_id = Int(name="offeringid")
1099.4.1 by Nick Chadwick
Working on putting worksheets into the database.
626
    identifier = Unicode()
627
    name = Unicode()
1080.1.40 by Matt Giuca
ivle.database: Added Worksheet and Exercise classes (more to come in this
628
    assessable = Bool()
1099.4.1 by Nick Chadwick
Working on putting worksheets into the database.
629
    data = Unicode()
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
630
    seq_no = Int()
631
    format = Unicode()
1080.1.40 by Matt Giuca
ivle.database: Added Worksheet and Exercise classes (more to come in this
632
1099.1.141 by Nick Chadwick
Updated the exercises to be loaded from the database, not a local file.
633
    attempts = ReferenceSet(id, "ExerciseAttempt.worksheetid")
1099.1.118 by William Grant
Fix a bad reference introduced with the worksheet changes.
634
    offering = Reference(offering_id, 'Offering.id')
1099.1.114 by Nick Chadwick
Modified the database so that exercises are now stored in the database, rather
635
1103 by William Grant
Worksheet.worksheet_exercises now only contains active ones.
636
    all_worksheet_exercises = ReferenceSet(id,
637
        'WorksheetExercise.worksheet_id')
638
639
    # Use worksheet_exercises to get access to the *active* WorksheetExercise
640
    # objects binding worksheets to exercises. This is required to access the
1080.1.50 by Matt Giuca
ivle.database: Added WorksheetExercise (relates worksheets to exercises), and
641
    # "optional" field.
1099.1.220 by Nick Chadwick
Merged from trunk
642
1103 by William Grant
Worksheet.worksheet_exercises now only contains active ones.
643
    @property
644
    def worksheet_exercises(self):
645
        return self.all_worksheet_exercises.find(active=True)
1080.1.50 by Matt Giuca
ivle.database: Added WorksheetExercise (relates worksheets to exercises), and
646
1080.1.40 by Matt Giuca
ivle.database: Added Worksheet and Exercise classes (more to come in this
647
    __init__ = _kwarg_init
648
649
    def __repr__(self):
650
        return "<%s %s>" % (type(self).__name__, self.name)
1080.1.47 by Matt Giuca
ivle.database: Added Worksheet.get_by_name method.
651
652
    # XXX Refactor this - make it an instance method of Subject rather than a
653
    # class method of Worksheet. Can't do that now because Subject isn't
654
    # linked referentially to the Worksheet.
655
    @classmethod
656
    def get_by_name(cls, store, subjectname, worksheetname):
657
        """
658
        Get the Worksheet from the db associated with a given store, subject
659
        name and worksheet name.
660
        """
661
        return store.find(cls, cls.subject == unicode(subjectname),
662
            cls.name == unicode(worksheetname)).one()
1080.1.50 by Matt Giuca
ivle.database: Added WorksheetExercise (relates worksheets to exercises), and
663
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
664
    def remove_all_exercises(self):
1080.1.51 by Matt Giuca
tutorial: Replaced call to ivle.db.create_worksheet with local code (roughly
665
        """
666
        Remove all exercises from this worksheet.
667
        This does not delete the exercises themselves. It just removes them
668
        from the worksheet.
669
        """
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
670
        store = Store.of(self)
671
        for ws_ex in self.all_worksheet_exercises:
672
            if ws_ex.saves.count() > 0 or ws_ex.attempts.count() > 0:
673
                raise IntegrityError()
1080.1.51 by Matt Giuca
tutorial: Replaced call to ivle.db.create_worksheet with local code (roughly
674
        store.find(WorksheetExercise,
675
            WorksheetExercise.worksheet == self).remove()
1099.1.141 by Nick Chadwick
Updated the exercises to be loaded from the database, not a local file.
676
            
677
    def get_permissions(self, user):
678
        return self.offering.get_permissions(user)
1099.1.220 by Nick Chadwick
Merged from trunk
679
    
680
    def get_xml(self):
681
        """Returns the xml of this worksheet, converts from rst if required."""
682
        if self.format == u'rst':
1099.1.232 by Nick Chadwick
Removed XML from database. RST now generates a full xml document, not
683
            ws_xml = rst(self.data)
1099.1.220 by Nick Chadwick
Merged from trunk
684
            return ws_xml
685
        else:
686
            return self.data
1099.1.233 by Nick Chadwick
Exercise objects in the database module, along with their test cases,
687
    
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
688
    def delete(self):
1099.1.233 by Nick Chadwick
Exercise objects in the database module, along with their test cases,
689
        """Deletes the worksheet, provided it has no attempts on any exercises.
690
        
691
        Returns True if delete succeeded, or False if this worksheet has
692
        attempts attached."""
693
        for ws_ex in self.all_worksheet_exercises:
694
            if ws_ex.saves.count() > 0 or ws_ex.attempts.count() > 0:
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
695
                raise IntegrityError()
1099.1.233 by Nick Chadwick
Exercise objects in the database module, along with their test cases,
696
        
697
        self.remove_all_exercises()
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
698
        Store.of(self).remove(self)
1099.1.233 by Nick Chadwick
Exercise objects in the database module, along with their test cases,
699
        
1080.1.50 by Matt Giuca
ivle.database: Added WorksheetExercise (relates worksheets to exercises), and
700
class WorksheetExercise(Storm):
1099.1.195 by William Grant
Rename problem to exercise in the DB.
701
    __storm_table__ = "worksheet_exercise"
1099.4.4 by Nick Chadwick
Made what should (hopefully) be the last changes to the database schema.
702
    
1099.1.195 by William Grant
Rename problem to exercise in the DB.
703
    id = Int(primary=True, name="ws_ex_id")
1080.1.50 by Matt Giuca
ivle.database: Added WorksheetExercise (relates worksheets to exercises), and
704
705
    worksheet_id = Int(name="worksheetid")
706
    worksheet = Reference(worksheet_id, Worksheet.id)
1099.1.195 by William Grant
Rename problem to exercise in the DB.
707
    exercise_id = Unicode(name="exerciseid")
1080.1.50 by Matt Giuca
ivle.database: Added WorksheetExercise (relates worksheets to exercises), and
708
    exercise = Reference(exercise_id, Exercise.id)
709
    optional = Bool()
1099.4.3 by Nick Chadwick
Updated the tutorial service, to now allow users to edit worksheets
710
    active = Bool()
711
    seq_no = Int()
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
712
    
713
    saves = ReferenceSet(id, "ExerciseSave.ws_ex_id")
1099.1.183 by William Grant
Fix a reference typo in ivle.database.
714
    attempts = ReferenceSet(id, "ExerciseAttempt.ws_ex_id")
1080.1.50 by Matt Giuca
ivle.database: Added WorksheetExercise (relates worksheets to exercises), and
715
716
    __init__ = _kwarg_init
717
718
    def __repr__(self):
719
        return "<%s %s in %s>" % (type(self).__name__, self.exercise.name,
1099.4.1 by Nick Chadwick
Working on putting worksheets into the database.
720
                                  self.worksheet.identifier)
1080.1.55 by Matt Giuca
ivle.database: Added ExerciseAttempt and ExerciseSave classes.
721
1131 by William Grant
Offerings now give 'view' only to user enrolled in them. 'edit' is granted
722
    def get_permissions(self, user):
723
        return self.worksheet.get_permissions(user)
1099.1.233 by Nick Chadwick
Exercise objects in the database module, along with their test cases,
724
    
1131 by William Grant
Offerings now give 'view' only to user enrolled in them. 'edit' is granted
725
1080.1.55 by Matt Giuca
ivle.database: Added ExerciseAttempt and ExerciseSave classes.
726
class ExerciseSave(Storm):
727
    """
728
    Represents a potential solution to an exercise that a user has submitted
729
    to the server for storage.
730
    A basic ExerciseSave is just the current saved text for this exercise for
731
    this user (doesn't count towards their attempts).
732
    ExerciseSave may be extended with additional semantics (such as
733
    ExerciseAttempt).
734
    """
1099.1.195 by William Grant
Rename problem to exercise in the DB.
735
    __storm_table__ = "exercise_save"
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
736
    __storm_primary__ = "ws_ex_id", "user_id"
737
1099.1.195 by William Grant
Rename problem to exercise in the DB.
738
    ws_ex_id = Int(name="ws_ex_id")
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
739
    worksheet_exercise = Reference(ws_ex_id, "WorksheetExercise.id")
740
1080.1.55 by Matt Giuca
ivle.database: Added ExerciseAttempt and ExerciseSave classes.
741
    user_id = Int(name="loginid")
742
    user = Reference(user_id, User.id)
743
    date = DateTime()
744
    text = Unicode()
745
746
    __init__ = _kwarg_init
747
748
    def __repr__(self):
749
        return "<%s %s by %s at %s>" % (type(self).__name__,
750
            self.exercise.name, self.user.login, self.date.strftime("%c"))
751
752
class ExerciseAttempt(ExerciseSave):
753
    """
754
    An ExerciseAttempt is a special case of an ExerciseSave. Like an
755
    ExerciseSave, it constitutes exercise solution data that the user has
756
    submitted to the server for storage.
757
    In addition, it contains additional information about the submission.
758
    complete - True if this submission was successful, rendering this exercise
759
        complete for this user.
760
    active - True if this submission is "active" (usually true). Submissions
761
        may be de-activated by privileged users for special reasons, and then
762
        they won't count (either as a penalty or success), but will still be
763
        stored.
764
    """
1099.1.195 by William Grant
Rename problem to exercise in the DB.
765
    __storm_table__ = "exercise_attempt"
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
766
    __storm_primary__ = "ws_ex_id", "user_id", "date"
1080.1.55 by Matt Giuca
ivle.database: Added ExerciseAttempt and ExerciseSave classes.
767
768
    # The "text" field is the same but has a different name in the DB table
769
    # for some reason.
770
    text = Unicode(name="attempt")
771
    complete = Bool()
772
    active = Bool()
1099.1.114 by Nick Chadwick
Modified the database so that exercises are now stored in the database, rather
773
    
1099.1.113 by William Grant
Give console and tutorial services security declarations.
774
    def get_permissions(self, user):
775
        return set(['view']) if user is self.user else set()
1099.1.114 by Nick Chadwick
Modified the database so that exercises are now stored in the database, rather
776
  
777
class TestSuite(Storm):
778
    """A Testsuite acts as a container for the test cases of an exercise."""
779
    __storm_table__ = "test_suite"
780
    __storm_primary__ = "exercise_id", "suiteid"
781
    
782
    suiteid = Int()
1099.1.195 by William Grant
Rename problem to exercise in the DB.
783
    exercise_id = Unicode(name="exerciseid")
1099.1.141 by Nick Chadwick
Updated the exercises to be loaded from the database, not a local file.
784
    description = Unicode()
785
    seq_no = Int()
786
    function = Unicode()
787
    stdin = Unicode()
1099.1.114 by Nick Chadwick
Modified the database so that exercises are now stored in the database, rather
788
    exercise = Reference(exercise_id, Exercise.id)
1099.1.212 by Nick Chadwick
Added a new page to display exercises. This will then be modified to
789
    test_cases = ReferenceSet(suiteid, 'TestCase.suiteid', order_by="seq_no")
790
    variables = ReferenceSet(suiteid, 'TestSuiteVar.suiteid', order_by='arg_no')
1099.1.233 by Nick Chadwick
Exercise objects in the database module, along with their test cases,
791
    
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
792
    def delete(self):
1099.1.233 by Nick Chadwick
Exercise objects in the database module, along with their test cases,
793
        """Delete this suite, without asking questions."""
794
        for vaariable in self.variables:
795
            variable.delete()
796
        for test_case in self.test_cases:
797
            test_case.delete()
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
798
        Store.of(self).remove(self)
1099.1.114 by Nick Chadwick
Modified the database so that exercises are now stored in the database, rather
799
800
class TestCase(Storm):
801
    """A TestCase is a member of a TestSuite.
802
    
803
    It contains the data necessary to check if an exercise is correct"""
804
    __storm_table__ = "test_case"
805
    __storm_primary__ = "testid", "suiteid"
806
    
807
    testid = Int()
808
    suiteid = Int()
1099.1.141 by Nick Chadwick
Updated the exercises to be loaded from the database, not a local file.
809
    suite = Reference(suiteid, "TestSuite.suiteid")
1099.1.114 by Nick Chadwick
Modified the database so that exercises are now stored in the database, rather
810
    passmsg = Unicode()
811
    failmsg = Unicode()
1099.1.141 by Nick Chadwick
Updated the exercises to be loaded from the database, not a local file.
812
    test_default = Unicode()
1099.1.114 by Nick Chadwick
Modified the database so that exercises are now stored in the database, rather
813
    seq_no = Int()
814
    
1099.1.141 by Nick Chadwick
Updated the exercises to be loaded from the database, not a local file.
815
    parts = ReferenceSet(testid, "TestCasePart.testid")
816
    
817
    __init__ = _kwarg_init
1099.1.233 by Nick Chadwick
Exercise objects in the database module, along with their test cases,
818
    
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
819
    def delete(self):
1099.1.233 by Nick Chadwick
Exercise objects in the database module, along with their test cases,
820
        for part in self.parts:
821
            part.delete()
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
822
        Store.of(self).remove(self)
1099.1.141 by Nick Chadwick
Updated the exercises to be loaded from the database, not a local file.
823
824
class TestSuiteVar(Storm):
825
    """A container for the arguments of a Test Suite"""
1099.1.195 by William Grant
Rename problem to exercise in the DB.
826
    __storm_table__ = "suite_variable"
1099.1.141 by Nick Chadwick
Updated the exercises to be loaded from the database, not a local file.
827
    __storm_primary__ = "varid"
828
    
829
    varid = Int()
830
    suiteid = Int()
831
    var_name = Unicode()
832
    var_value = Unicode()
833
    var_type = Unicode()
834
    arg_no = Int()
835
    
836
    suite = Reference(suiteid, "TestSuite.suiteid")
837
    
838
    __init__ = _kwarg_init
839
    
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
840
    def delete(self):
841
        Store.of(self).remove(self)
1099.1.233 by Nick Chadwick
Exercise objects in the database module, along with their test cases,
842
    
1099.1.141 by Nick Chadwick
Updated the exercises to be loaded from the database, not a local file.
843
class TestCasePart(Storm):
844
    """A container for the test elements of a Test Case"""
1099.1.195 by William Grant
Rename problem to exercise in the DB.
845
    __storm_table__ = "test_case_part"
1099.1.141 by Nick Chadwick
Updated the exercises to be loaded from the database, not a local file.
846
    __storm_primary__ = "partid"
847
    
848
    partid = Int()
849
    testid = Int()
850
    
851
    part_type = Unicode()
852
    test_type = Unicode()
853
    data = Unicode()
854
    filename = Unicode()
855
    
856
    test = Reference(testid, "TestCase.testid")
857
    
1099.1.114 by Nick Chadwick
Modified the database so that exercises are now stored in the database, rather
858
    __init__ = _kwarg_init
1099.1.233 by Nick Chadwick
Exercise objects in the database module, along with their test cases,
859
    
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
860
    def delete(self):
861
        Store.of(self).remove(self)