18
18
# Author: Matt Giuca, Will Grant
20
"""Database utilities and content classes.
21
Database Classes and Utilities for Storm ORM
22
23
This module provides all of the classes which map to database tables.
23
24
It also provides miscellaneous utility functions for database interaction.
162
165
# TODO: Invitations should be listed too?
163
166
def get_groups(self, offering=None):
164
"""Get groups of which this user is a member.
166
@param offering: An optional offering to restrict the search to.
169
168
ProjectGroupMembership.user_id == self.id,
170
169
ProjectGroup.id == ProjectGroupMembership.project_group_id,
191
190
return self._get_enrolments(False)
193
192
def get_projects(self, offering=None, active_only=True):
194
"""Find projects that the user can submit.
193
'''Return Projects that the user can submit.
196
195
This will include projects for offerings in which the user is
197
196
enrolled, as long as the project is not in a project set which has
198
197
groups (ie. if maximum number of group members is 0).
200
@param active_only: Whether to only search active offerings.
201
@param offering: An optional offering to restrict the search to.
199
Unless active_only is False, only projects for active offerings will
202
If an offering is specified, returned projects will be limited to
203
those for that offering.
203
205
return Store.of(self).find(Project,
204
206
Project.project_set_id == ProjectSet.id,
205
207
ProjectSet.max_students_per_group == None,
214
216
def hash_password(password):
215
"""Hash a password with MD5."""
216
217
return hashlib.md5(password).hexdigest()
219
220
def get_by_login(cls, store, login):
220
"""Find a user in a store by login name."""
222
Get the User from the db associated with a given store and
221
225
return store.find(cls, cls.login == unicode(login)).one()
223
227
def get_permissions(self, user):
224
"""Determine privileges held by a user over this object.
226
If the user requesting privileges is this user or an admin,
227
they may do everything. Otherwise they may do nothing.
229
228
if user and user.admin or user is self:
230
229
return set(['view', 'edit', 'submit_project'])
234
233
# SUBJECTS AND ENROLMENTS #
236
235
class Subject(Storm):
237
"""A subject (or course) which is run in some semesters."""
239
236
__storm_table__ = "subject"
241
238
id = Int(primary=True, name="subjectid")
252
249
return "<%s '%s'>" % (type(self).__name__, self.short_name)
254
251
def get_permissions(self, user):
255
"""Determine privileges held by a user over this object.
257
If the user requesting privileges is an admin, they may edit.
258
Otherwise they may only read.
261
253
if user is not None:
262
254
perms.add('view')
267
259
def active_offerings(self):
268
"""Find active offerings for this subject.
270
Return a sequence of currently active offerings for this subject
260
"""Return a sequence of currently active offerings for this subject
271
261
(offerings whose semester.state is "current"). There should be 0 or 1
272
262
elements in this sequence, but it's possible there are more.
275
265
Semester.state == u'current')
277
267
def offering_for_semester(self, year, semester):
278
"""Get the offering for the given year/semester, or None.
280
@param year: A string representation of the year.
281
@param semester: A string representation of the semester.
268
"""Get the offering for the given year/semester, or None."""
283
269
return self.offerings.find(Offering.semester_id == Semester.id,
284
270
Semester.year == unicode(year),
285
271
Semester.semester == unicode(semester)).one()
287
273
class Semester(Storm):
288
"""A semester in which subjects can be run."""
290
274
__storm_table__ = "semester"
292
276
id = Int(primary=True, name="semesterid")
306
290
return "<%s %s/%s>" % (type(self).__name__, self.year, self.semester)
308
292
class Offering(Storm):
309
"""An offering of a subject in a particular semester."""
311
293
__storm_table__ = "offering"
313
295
id = Int(primary=True, name="offeringid")
338
320
def enrol(self, user, role=u'student'):
339
"""Enrol a user in this offering.
341
Enrolments handle both the staff and student cases. The role controls
342
the privileges granted by this enrolment.
321
'''Enrol a user in this offering.'''
344
322
enrolment = Store.of(self).find(Enrolment,
345
323
Enrolment.user_id == user.id,
346
324
Enrolment.offering_id == self.id).one()
382
359
class Enrolment(Storm):
383
"""An enrolment of a user in an offering.
385
This represents the roles of both staff and students.
388
360
__storm_table__ = "enrolment"
389
361
__storm_primary__ = "user_id", "offering_id"
415
387
class ProjectSet(Storm):
416
"""A set of projects that share common groups.
418
Each student project group is attached to a project set. The group is
419
valid for all projects in the group's set.
422
388
__storm_table__ = "project_set"
424
390
id = Int(name="projectsetid", primary=True)
435
401
return "<%s %d in %r>" % (type(self).__name__, self.id,
438
def get_permissions(self, user):
439
return self.offering.get_permissions(user)
441
404
class Project(Storm):
442
"""A student project for which submissions can be made."""
444
405
__storm_table__ = "project"
446
407
id = Int(name="projectid", primary=True)
471
432
def submit(self, principal, path, revision, who):
472
433
"""Submit a Subversion path and revision to a project.
474
@param principal: The owner of the Subversion repository, and the
475
entity on behalf of whom the submission is being made
476
@param path: A path within that repository to submit.
477
@param revision: The revision of that path to submit.
478
@param who: The user who is actually making the submission.
435
'principal' is the owner of the Subversion repository, and the
436
entity on behalf of whom the submission is being made. 'path' is
437
a path within that repository, and 'revision' specifies which
438
revision of that path. 'who' is the person making the submission.
481
441
if not self.can_submit(principal):
494
def get_permissions(self, user):
495
return self.project_set.offering.get_permissions(user)
498
455
class ProjectGroup(Storm):
499
"""A group of students working together on a project."""
501
456
__storm_table__ = "project_group"
503
458
id = Int(name="groupid", primary=True)
525
480
return '%s (%s)' % (self.nick, self.name)
527
482
def get_projects(self, offering=None, active_only=True):
528
'''Find projects that the group can submit.
483
'''Return Projects that the group can submit.
530
485
This will include projects in the project set which owns this group,
531
486
unless the project set disallows groups (in which case none will be
534
@param active_only: Whether to only search active offerings.
535
@param offering: An optional offering to restrict the search to.
489
Unless active_only is False, projects will only be returned if the
490
group's offering is active.
492
If an offering is specified, projects will only be returned if it
537
495
return Store.of(self).find(Project,
538
496
Project.project_set_id == ProjectSet.id,
553
511
class ProjectGroupMembership(Storm):
554
"""A student's membership in a project group."""
556
512
__storm_table__ = "group_member"
557
513
__storm_primary__ = "user_id", "project_group_id"
568
524
self.project_group)
570
526
class Assessed(Storm):
571
"""A composite of a user or group combined with a project.
573
Each project submission and extension refers to an Assessed. It is the
574
sole specifier of the repository and project.
577
527
__storm_table__ = "assessed"
579
529
id = Int(name="assessedid", primary=True)
596
546
def get(cls, store, principal, project):
597
"""Find or create an Assessed for the given user or group and project.
599
@param principal: The user or group.
600
@param project: The project.
602
547
t = type(principal)
603
548
if t not in (User, ProjectGroup):
604
549
raise AssertionError('principal must be User or ProjectGroup')
623
568
class ProjectExtension(Storm):
624
"""An extension granted to a user or group on a particular project.
626
The user or group and project are specified by the Assessed.
629
569
__storm_table__ = "project_extension"
631
571
id = Int(name="extensionid", primary=True)
637
577
notes = Unicode()
639
579
class ProjectSubmission(Storm):
640
"""A submission from a user or group repository to a particular project.
642
The content of a submission is a single path and revision inside a
643
repository. The repository is that owned by the submission's user and
644
group, while the path and revision are explicit.
646
The user or group and project are specified by the Assessed.
649
580
__storm_table__ = "project_submission"
651
582
id = Int(name="submissionid", primary=True)
661
592
# WORKSHEETS AND EXERCISES #
663
594
class Exercise(Storm):
664
"""An exercise for students to complete in a worksheet.
666
An exercise may be present in any number of worksheets.
669
595
__storm_table__ = "exercise"
670
596
id = Unicode(primary=True, name="identifier")
701
627
perms.add('edit')
702
628
perms.add('view')
703
elif u'lecturer' in set((e.role for e in user.active_enrolments)):
706
elif u'tutor' in set((e.role for e in user.active_enrolments)):
629
elif 'lecturer' in set((e.role for e in user.active_enrolments)):
712
635
def get_description(self):
713
"""Return the description interpreted as reStructuredText."""
714
636
return rst(self.description)
716
638
def delete(self):
722
644
Store.of(self).remove(self)
724
646
class Worksheet(Storm):
725
"""A worksheet with exercises for students to complete.
727
Worksheets are owned by offerings.
730
647
__storm_table__ = "worksheet"
732
649
id = Int(primary=True, name="worksheetid")
758
675
return "<%s %s>" % (type(self).__name__, self.name)
760
677
def remove_all_exercises(self):
761
"""Remove all exercises from this worksheet.
679
Remove all exercises from this worksheet.
763
680
This does not delete the exercises themselves. It just removes them
764
681
from the worksheet.
769
686
raise IntegrityError()
770
687
store.find(WorksheetExercise,
771
688
WorksheetExercise.worksheet == self).remove()
773
690
def get_permissions(self, user):
774
691
return self.offering.get_permissions(user)
776
693
def get_xml(self):
777
694
"""Returns the xml of this worksheet, converts from rst if required."""
778
695
if self.format == u'rst':
784
701
def delete(self):
785
702
"""Deletes the worksheet, provided it has no attempts on any exercises.
787
704
Returns True if delete succeeded, or False if this worksheet has
788
705
attempts attached."""
789
706
for ws_ex in self.all_worksheet_exercises:
790
707
if ws_ex.saves.count() > 0 or ws_ex.attempts.count() > 0:
791
708
raise IntegrityError()
793
710
self.remove_all_exercises()
794
711
Store.of(self).remove(self)
796
713
class WorksheetExercise(Storm):
797
"""A link between a worksheet and one of its exercises.
799
These may be marked optional, in which case the exercise does not count
800
for marking purposes. The sequence number is used to order the worksheet
804
714
__storm_table__ = "worksheet_exercise"
806
716
id = Int(primary=True, name="ws_ex_id")
808
718
worksheet_id = Int(name="worksheetid")
825
735
def get_permissions(self, user):
826
736
return self.worksheet.get_permissions(user)
829
739
class ExerciseSave(Storm):
830
"""A potential exercise solution submitted by a user for storage.
832
This is not an actual tested attempt at an exercise, it's just a save of
741
Represents a potential solution to an exercise that a user has submitted
742
to the server for storage.
743
A basic ExerciseSave is just the current saved text for this exercise for
744
this user (doesn't count towards their attempts).
745
ExerciseSave may be extended with additional semantics (such as
836
748
__storm_table__ = "exercise_save"
837
749
__storm_primary__ = "ws_ex_id", "user_id"
851
763
self.exercise.name, self.user.login, self.date.strftime("%c"))
853
765
class ExerciseAttempt(ExerciseSave):
854
"""An attempt at solving an exercise.
856
This is a special case of ExerciseSave, used when the user submits a
857
candidate solution. Like an ExerciseSave, it constitutes exercise solution
860
In addition, it contains information about the result of the submission:
862
- complete - True if this submission was successful, rendering this
863
exercise complete for this user in this worksheet.
864
- active - True if this submission is "active" (usually true).
865
Submissions may be de-activated by privileged users for
866
special reasons, and then they won't count (either as a
867
penalty or success), but will still be stored.
767
An ExerciseAttempt is a special case of an ExerciseSave. Like an
768
ExerciseSave, it constitutes exercise solution data that the user has
769
submitted to the server for storage.
770
In addition, it contains additional information about the submission.
771
complete - True if this submission was successful, rendering this exercise
772
complete for this user.
773
active - True if this submission is "active" (usually true). Submissions
774
may be de-activated by privileged users for special reasons, and then
775
they won't count (either as a penalty or success), but will still be
870
778
__storm_table__ = "exercise_attempt"
871
779
__storm_primary__ = "ws_ex_id", "user_id", "date"
875
783
text = Unicode(name="attempt")
876
784
complete = Bool()
879
787
def get_permissions(self, user):
880
788
return set(['view']) if user is self.user else set()
882
790
class TestSuite(Storm):
883
"""A container to group an exercise's test cases.
885
The test suite contains some information on how to test. The function to
886
test, variables to set and stdin data are stored here.
791
"""A Testsuite acts as a container for the test cases of an exercise."""
889
792
__storm_table__ = "test_suite"
890
793
__storm_primary__ = "exercise_id", "suiteid"
893
796
exercise_id = Unicode(name="exerciseid")
894
797
description = Unicode()
898
801
exercise = Reference(exercise_id, Exercise.id)
899
802
test_cases = ReferenceSet(suiteid, 'TestCase.suiteid', order_by="seq_no")
900
803
variables = ReferenceSet(suiteid, 'TestSuiteVar.suiteid', order_by='arg_no')
902
805
def delete(self):
903
806
"""Delete this suite, without asking questions."""
904
807
for vaariable in self.variables:
908
811
Store.of(self).remove(self)
910
813
class TestCase(Storm):
911
"""A container for actual tests (see TestCasePart), inside a test suite.
913
It is the lowest level shown to students on their pass/fail status."""
814
"""A TestCase is a member of a TestSuite.
816
It contains the data necessary to check if an exercise is correct"""
915
817
__storm_table__ = "test_case"
916
818
__storm_primary__ = "testid", "suiteid"
920
822
suite = Reference(suiteid, "TestSuite.suiteid")
922
824
failmsg = Unicode()
923
825
test_default = Unicode()
926
828
parts = ReferenceSet(testid, "TestCasePart.testid")
928
830
__init__ = _kwarg_init
930
832
def delete(self):
931
833
for part in self.parts:
933
835
Store.of(self).remove(self)
935
837
class TestSuiteVar(Storm):
936
"""A variable used by an exercise test suite.
938
This may represent a function argument or a normal variable.
838
"""A container for the arguments of a Test Suite"""
941
839
__storm_table__ = "suite_variable"
942
840
__storm_primary__ = "varid"
946
844
var_name = Unicode()
947
845
var_value = Unicode()
948
846
var_type = Unicode()
951
849
suite = Reference(suiteid, "TestSuite.suiteid")
953
851
__init__ = _kwarg_init
955
853
def delete(self):
956
854
Store.of(self).remove(self)
958
856
class TestCasePart(Storm):
959
"""An actual piece of code to test an exercise solution."""
857
"""A container for the test elements of a Test Case"""
961
858
__storm_table__ = "test_case_part"
962
859
__storm_primary__ = "partid"
967
864
part_type = Unicode()
968
865
test_type = Unicode()
970
867
filename = Unicode()
972
869
test = Reference(testid, "TestCase.testid")
974
871
__init__ = _kwarg_init
976
873
def delete(self):
977
874
Store.of(self).remove(self)