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)
592
542
return "<%s %r in %r>" % (type(self).__name__,
593
543
self.user or self.project_group, self.project)
597
return self.project_group or self.user
600
546
def get(cls, store, principal, project):
601
"""Find or create an Assessed for the given user or group and project.
603
@param principal: The user or group.
604
@param project: The project.
606
547
t = type(principal)
607
548
if t not in (User, ProjectGroup):
608
549
raise AssertionError('principal must be User or ProjectGroup')
627
568
class ProjectExtension(Storm):
628
"""An extension granted to a user or group on a particular project.
630
The user or group and project are specified by the Assessed.
633
569
__storm_table__ = "project_extension"
635
571
id = Int(name="extensionid", primary=True)
641
577
notes = Unicode()
643
579
class ProjectSubmission(Storm):
644
"""A submission from a user or group repository to a particular project.
646
The content of a submission is a single path and revision inside a
647
repository. The repository is that owned by the submission's user and
648
group, while the path and revision are explicit.
650
The user or group and project are specified by the Assessed.
653
580
__storm_table__ = "project_submission"
655
582
id = Int(name="submissionid", primary=True)
665
592
# WORKSHEETS AND EXERCISES #
667
594
class Exercise(Storm):
668
"""An exercise for students to complete in a worksheet.
670
An exercise may be present in any number of worksheets.
673
595
__storm_table__ = "exercise"
674
596
id = Unicode(primary=True, name="identifier")
705
627
perms.add('edit')
706
628
perms.add('view')
707
elif u'lecturer' in set((e.role for e in user.active_enrolments)):
710
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)):
716
635
def get_description(self):
717
"""Return the description interpreted as reStructuredText."""
718
636
return rst(self.description)
720
638
def delete(self):
726
644
Store.of(self).remove(self)
728
646
class Worksheet(Storm):
729
"""A worksheet with exercises for students to complete.
731
Worksheets are owned by offerings.
734
647
__storm_table__ = "worksheet"
736
649
id = Int(primary=True, name="worksheetid")
762
675
return "<%s %s>" % (type(self).__name__, self.name)
764
677
def remove_all_exercises(self):
765
"""Remove all exercises from this worksheet.
679
Remove all exercises from this worksheet.
767
680
This does not delete the exercises themselves. It just removes them
768
681
from the worksheet.
773
686
raise IntegrityError()
774
687
store.find(WorksheetExercise,
775
688
WorksheetExercise.worksheet == self).remove()
777
690
def get_permissions(self, user):
778
691
return self.offering.get_permissions(user)
780
693
def get_xml(self):
781
694
"""Returns the xml of this worksheet, converts from rst if required."""
782
695
if self.format == u'rst':
788
701
def delete(self):
789
702
"""Deletes the worksheet, provided it has no attempts on any exercises.
791
704
Returns True if delete succeeded, or False if this worksheet has
792
705
attempts attached."""
793
706
for ws_ex in self.all_worksheet_exercises:
794
707
if ws_ex.saves.count() > 0 or ws_ex.attempts.count() > 0:
795
708
raise IntegrityError()
797
710
self.remove_all_exercises()
798
711
Store.of(self).remove(self)
800
713
class WorksheetExercise(Storm):
801
"""A link between a worksheet and one of its exercises.
803
These may be marked optional, in which case the exercise does not count
804
for marking purposes. The sequence number is used to order the worksheet
808
714
__storm_table__ = "worksheet_exercise"
810
716
id = Int(primary=True, name="ws_ex_id")
812
718
worksheet_id = Int(name="worksheetid")
829
735
def get_permissions(self, user):
830
736
return self.worksheet.get_permissions(user)
833
739
class ExerciseSave(Storm):
834
"""A potential exercise solution submitted by a user for storage.
836
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
840
748
__storm_table__ = "exercise_save"
841
749
__storm_primary__ = "ws_ex_id", "user_id"
855
763
self.exercise.name, self.user.login, self.date.strftime("%c"))
857
765
class ExerciseAttempt(ExerciseSave):
858
"""An attempt at solving an exercise.
860
This is a special case of ExerciseSave, used when the user submits a
861
candidate solution. Like an ExerciseSave, it constitutes exercise solution
864
In addition, it contains information about the result of the submission:
866
- complete - True if this submission was successful, rendering this
867
exercise complete for this user in this worksheet.
868
- active - True if this submission is "active" (usually true).
869
Submissions may be de-activated by privileged users for
870
special reasons, and then they won't count (either as a
871
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
874
778
__storm_table__ = "exercise_attempt"
875
779
__storm_primary__ = "ws_ex_id", "user_id", "date"
879
783
text = Unicode(name="attempt")
880
784
complete = Bool()
883
787
def get_permissions(self, user):
884
788
return set(['view']) if user is self.user else set()
886
790
class TestSuite(Storm):
887
"""A container to group an exercise's test cases.
889
The test suite contains some information on how to test. The function to
890
test, variables to set and stdin data are stored here.
791
"""A Testsuite acts as a container for the test cases of an exercise."""
893
792
__storm_table__ = "test_suite"
894
793
__storm_primary__ = "exercise_id", "suiteid"
897
796
exercise_id = Unicode(name="exerciseid")
898
797
description = Unicode()
902
801
exercise = Reference(exercise_id, Exercise.id)
903
802
test_cases = ReferenceSet(suiteid, 'TestCase.suiteid', order_by="seq_no")
904
803
variables = ReferenceSet(suiteid, 'TestSuiteVar.suiteid', order_by='arg_no')
906
805
def delete(self):
907
806
"""Delete this suite, without asking questions."""
908
807
for vaariable in self.variables:
912
811
Store.of(self).remove(self)
914
813
class TestCase(Storm):
915
"""A container for actual tests (see TestCasePart), inside a test suite.
917
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"""
919
817
__storm_table__ = "test_case"
920
818
__storm_primary__ = "testid", "suiteid"
924
822
suite = Reference(suiteid, "TestSuite.suiteid")
926
824
failmsg = Unicode()
927
825
test_default = Unicode()
930
828
parts = ReferenceSet(testid, "TestCasePart.testid")
932
830
__init__ = _kwarg_init
934
832
def delete(self):
935
833
for part in self.parts:
937
835
Store.of(self).remove(self)
939
837
class TestSuiteVar(Storm):
940
"""A variable used by an exercise test suite.
942
This may represent a function argument or a normal variable.
838
"""A container for the arguments of a Test Suite"""
945
839
__storm_table__ = "suite_variable"
946
840
__storm_primary__ = "varid"
950
844
var_name = Unicode()
951
845
var_value = Unicode()
952
846
var_type = Unicode()
955
849
suite = Reference(suiteid, "TestSuite.suiteid")
957
851
__init__ = _kwarg_init
959
853
def delete(self):
960
854
Store.of(self).remove(self)
962
856
class TestCasePart(Storm):
963
"""An actual piece of code to test an exercise solution."""
857
"""A container for the test elements of a Test Case"""
965
858
__storm_table__ = "test_case_part"
966
859
__storm_primary__ = "partid"
971
864
part_type = Unicode()
972
865
test_type = Unicode()
974
867
filename = Unicode()
976
869
test = Reference(testid, "TestCase.testid")
978
871
__init__ = _kwarg_init
980
873
def delete(self):
981
874
Store.of(self).remove(self)