32
31
from storm.locals import create_database, Store, Int, Unicode, DateTime, \
33
32
Reference, ReferenceSet, Bool, Storm, Desc
231
230
"""Find a user in a store by login name."""
232
231
return store.find(cls, cls.login == unicode(login)).one()
234
def get_svn_url(self, config):
233
def get_svn_url(self, config, req):
235
234
"""Get the subversion repository URL for this user or group."""
236
url = config['urls']['svn_addr']
235
login = req.user.login
236
url = urlparse.urlsplit(config['urls']['svn_addr'])
237
url = urlparse.urlunsplit(url[:1] + (login+'@'+url[1],) + url[2:])
237
238
path = 'users/%s' % self.login
238
239
return urlparse.urljoin(url, path)
299
300
return self.offerings.find(Offering.semester_id == Semester.id,
300
301
Semester.year == unicode(year),
301
Semester.url_name == unicode(semester)).one()
302
Semester.semester == unicode(semester)).one()
303
304
class Semester(Storm):
304
305
"""A semester in which subjects can be run."""
321
320
__init__ = _kwarg_init
323
322
def __repr__(self):
324
return "<%s %s/%s>" % (type(self).__name__, self.year, self.code)
323
return "<%s %s/%s>" % (type(self).__name__, self.year, self.semester)
326
325
class Offering(Storm):
327
326
"""An offering of a subject in a particular semester."""
612
611
return "<%s '%s' in %r>" % (type(self).__name__, self.short_name,
613
612
self.project_set.offering)
615
def can_submit(self, principal, user, late=False):
617
@param late: If True, does not take the deadline into account.
614
def can_submit(self, principal, user):
619
615
return (self in principal.get_projects() and
620
(late or not self.has_deadline_passed(user)))
616
not self.has_deadline_passed(user))
622
def submit(self, principal, path, revision, who, late=False):
618
def submit(self, principal, path, revision, who):
623
619
"""Submit a Subversion path and revision to a project.
625
621
@param principal: The owner of the Subversion repository, and the
627
623
@param path: A path within that repository to submit.
628
624
@param revision: The revision of that path to submit.
629
625
@param who: The user who is actually making the submission.
630
@param late: If True, will not raise a DeadlinePassed exception even
631
after the deadline. (Default False.)
634
if not self.can_submit(principal, who, late=late):
628
if not self.can_submit(principal, who):
635
629
raise DeadlinePassed()
637
631
a = Assessed.get(Store.of(self), principal, self)
740
734
Semester.id == Offering.semester_id,
741
735
(not active_only) or (Semester.state == u'current'))
743
def get_svn_url(self, config):
737
def get_svn_url(self, config, req):
744
738
"""Get the subversion repository URL for this user or group."""
745
url = config['urls']['svn_addr']
739
login = req.user.login
740
url = urlparse.urlsplit(config['urls']['svn_addr'])
741
url = urlparse.urlunsplit(url[:1] + (login+'@'+url[1],) + url[2:])
746
742
path = 'groups/%s_%s_%s_%s' % (
747
743
self.project_set.offering.subject.short_name,
748
744
self.project_set.offering.semester.year,
749
self.project_set.offering.semester.url_name,
745
self.project_set.offering.semester.semester,
752
748
return urlparse.urljoin(url, path)
867
863
id = Int(name="extensionid", primary=True)
868
864
assessed_id = Int(name="assessedid")
869
865
assessed = Reference(assessed_id, Assessed.id)
866
deadline = DateTime()
871
867
approver_id = Int(name="approver")
872
868
approver = Reference(approver_id, User.id)
873
869
notes = Unicode()
915
911
return "/files/%s/%s/%s?r=%d" % (user.login,
916
912
self.assessed.checkout_location, submitpath, self.revision)
918
def get_svn_url(self, config):
919
"""Get subversion URL for this submission"""
920
princ = self.assessed.principal
921
base = princ.get_svn_url(config)
922
if self.path.startswith(os.sep):
923
return os.path.join(base,
924
urllib.quote(self.path[1:].encode('utf-8')))
926
return os.path.join(base, urllib.quote(self.path.encode('utf-8')))
928
def get_svn_export_command(self, req):
929
"""Returns a Unix shell command to export a submission"""
930
svn_url = self.get_svn_url(req.config)
931
_, ext = os.path.splitext(svn_url)
932
username = (req.user.login if req.user.login.isalnum() else
933
"'%s'"%req.user.login)
934
# Export to a file or directory relative to the current directory,
935
# with the student's login name, appended with the submitted file's
937
export_path = self.assessed.principal.short_name + ext
938
return "svn export --username %s -r%d '%s' %s"%(req.user.login,
939
self.revision, svn_url, export_path)
942
915
def test_and_normalise_path(path):
943
916
"""Test that path is valid, and normalise it. This prevents possible
957
930
raise SubmissionError("Path must not contain '\\n', '[' or ']'")
958
931
return os.path.normpath(path)
962
"""True if the project was submitted late."""
963
return self.days_late > 0
967
"""The number of days the project was submitted late (rounded up), or
969
# XXX: Need to respect extensions.
971
(self.date_submitted - self.assessed.project.deadline).days + 1)
973
933
# WORKSHEETS AND EXERCISES #
975
935
class Exercise(Storm):