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

« back to all changes in this revision

Viewing changes to ivle/database.py

  • Committer: David Coles
  • Date: 2010-07-27 04:52:14 UTC
  • Revision ID: coles.david@gmail.com-20100727045214-p32h1kc0gcv48dpr
Worksheets: Strip off whitespace from the end of exercise attempts.

This solves an issue where accidental whitespace in an attempt will cause 
"IndentationError" syntax error (which don't occur when run in console).

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import datetime
28
28
import os
29
29
import urlparse
 
30
import urllib
30
31
 
31
32
from storm.locals import create_database, Store, Int, Unicode, DateTime, \
32
33
                         Reference, ReferenceSet, Bool, Storm, Desc
230
231
        """Find a user in a store by login name."""
231
232
        return store.find(cls, cls.login == unicode(login)).one()
232
233
 
233
 
    def get_svn_url(self, config, req):
 
234
    def get_svn_url(self, config):
234
235
        """Get the subversion repository URL for this user or group."""
235
 
        login = req.user.login
236
 
        url = urlparse.urlsplit(config['urls']['svn_addr'])
237
 
        url = urlparse.urlunsplit(url[:1] + (login+'@'+url[1],) + url[2:])
 
236
        url = config['urls']['svn_addr']
238
237
        path = 'users/%s' % self.login
239
238
        return urlparse.urljoin(url, path)
240
239
 
611
610
        return "<%s '%s' in %r>" % (type(self).__name__, self.short_name,
612
611
                                  self.project_set.offering)
613
612
 
614
 
    def can_submit(self, principal, user):
 
613
    def can_submit(self, principal, user, late=False):
 
614
        """
 
615
        @param late: If True, does not take the deadline into account.
 
616
        """
615
617
        return (self in principal.get_projects() and
616
 
                not self.has_deadline_passed(user))
 
618
                (late or not self.has_deadline_passed(user)))
617
619
 
618
 
    def submit(self, principal, path, revision, who):
 
620
    def submit(self, principal, path, revision, who, late=False):
619
621
        """Submit a Subversion path and revision to a project.
620
622
 
621
623
        @param principal: The owner of the Subversion repository, and the
623
625
        @param path: A path within that repository to submit.
624
626
        @param revision: The revision of that path to submit.
625
627
        @param who: The user who is actually making the submission.
 
628
        @param late: If True, will not raise a DeadlinePassed exception even
 
629
            after the deadline. (Default False.)
626
630
        """
627
631
 
628
 
        if not self.can_submit(principal, who):
 
632
        if not self.can_submit(principal, who, late=late):
629
633
            raise DeadlinePassed()
630
634
 
631
635
        a = Assessed.get(Store.of(self), principal, self)
734
738
            Semester.id == Offering.semester_id,
735
739
            (not active_only) or (Semester.state == u'current'))
736
740
 
737
 
    def get_svn_url(self, config, req):
 
741
    def get_svn_url(self, config):
738
742
        """Get the subversion repository URL for this user or group."""
739
 
        login = req.user.login
740
 
        url = urlparse.urlsplit(config['urls']['svn_addr'])
741
 
        url = urlparse.urlunsplit(url[:1] + (login+'@'+url[1],) + url[2:])
 
743
        url = config['urls']['svn_addr']
742
744
        path = 'groups/%s_%s_%s_%s' % (
743
745
                self.project_set.offering.subject.short_name,
744
746
                self.project_set.offering.semester.year,
863
865
    id = Int(name="extensionid", primary=True)
864
866
    assessed_id = Int(name="assessedid")
865
867
    assessed = Reference(assessed_id, Assessed.id)
866
 
    deadline = DateTime()
 
868
    days = Int()
867
869
    approver_id = Int(name="approver")
868
870
    approver = Reference(approver_id, User.id)
869
871
    notes = Unicode()
911
913
        return "/files/%s/%s/%s?r=%d" % (user.login,
912
914
            self.assessed.checkout_location, submitpath, self.revision)
913
915
 
 
916
    def get_svn_url(self, config):
 
917
        """Get subversion URL for this submission"""
 
918
        princ = self.assessed.principal
 
919
        base = princ.get_svn_url(config)
 
920
        if self.path.startswith(os.sep):
 
921
            return os.path.join(base,
 
922
                    urllib.quote(self.path[1:].encode('utf-8')))
 
923
        else:
 
924
            return os.path.join(base, urllib.quote(self.path.encode('utf-8')))
 
925
 
 
926
    def get_svn_export_command(self, req):
 
927
        """Returns a Unix shell command to export a submission"""
 
928
        svn_url = self.get_svn_url(req.config)
 
929
        username = (req.user.login if req.user.login.isalnum() else
 
930
                "'%s'"%req.user.login)
 
931
        export_dir = self.assessed.principal.short_name
 
932
        return "svn export --username %s -r%d '%s' %s"%(req.user.login,
 
933
                self.revision, svn_url, export_dir)
 
934
 
914
935
    @staticmethod
915
936
    def test_and_normalise_path(path):
916
937
        """Test that path is valid, and normalise it. This prevents possible
930
951
            raise SubmissionError("Path must not contain '\\n', '[' or ']'")
931
952
        return os.path.normpath(path)
932
953
 
 
954
    @property
 
955
    def late(self):
 
956
        """True if the project was submitted late."""
 
957
        return self.days_late > 0
 
958
 
 
959
    @property
 
960
    def days_late(self):
 
961
        """The number of days the project was submitted late (rounded up), or
 
962
        0 if on-time."""
 
963
        # XXX: Need to respect extensions.
 
964
        return max(0,
 
965
            (self.date_submitted - self.assessed.project.deadline).days + 1)
 
966
 
933
967
# WORKSHEETS AND EXERCISES #
934
968
 
935
969
class Exercise(Storm):