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

« back to all changes in this revision

Viewing changes to ivle/database.py

  • Committer: William Grant
  • Date: 2010-02-26 01:09:49 UTC
  • Revision ID: grantw@unimelb.edu.au-20100226010949-xka8c9s6y7aq4id1
Scroll to the end of the console output after *every* output, not just some. This removes some artifiacts resulting from scrolling a fraction of a second too late.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import hashlib
27
27
import datetime
28
28
import os
29
 
import urlparse
30
 
import urllib
31
29
 
32
30
from storm.locals import create_database, Store, Int, Unicode, DateTime, \
33
31
                         Reference, ReferenceSet, Bool, Storm, Desc
150
148
            Offering.semester_id == Semester.id,
151
149
            Offering.subject_id == Subject.id).order_by(
152
150
                Desc(Semester.year),
153
 
                Desc(Semester.display_name),
 
151
                Desc(Semester.semester),
154
152
                Desc(Subject.code)
155
153
            )
156
154
 
231
229
        """Find a user in a store by login name."""
232
230
        return store.find(cls, cls.login == unicode(login)).one()
233
231
 
234
 
    def get_svn_url(self, config):
235
 
        """Get the subversion repository URL for this user or group."""
236
 
        url = config['urls']['svn_addr']
237
 
        path = 'users/%s' % self.login
238
 
        return urlparse.urljoin(url, path)
239
 
 
240
232
    def get_permissions(self, user, config):
241
233
        """Determine privileges held by a user over this object.
242
234
 
298
290
        """
299
291
        return self.offerings.find(Offering.semester_id == Semester.id,
300
292
                               Semester.year == unicode(year),
301
 
                               Semester.url_name == unicode(semester)).one()
 
293
                               Semester.semester == unicode(semester)).one()
302
294
 
303
295
class Semester(Storm):
304
296
    """A semester in which subjects can be run."""
307
299
 
308
300
    id = Int(primary=True, name="semesterid")
309
301
    year = Unicode()
310
 
    code = Unicode()
311
 
    url_name = Unicode()
312
 
    display_name = Unicode()
 
302
    semester = Unicode()
313
303
    state = Unicode()
314
304
 
315
305
    offerings = ReferenceSet(id, 'Offering.semester_id')
321
311
    __init__ = _kwarg_init
322
312
 
323
313
    def __repr__(self):
324
 
        return "<%s %s/%s>" % (type(self).__name__, self.year, self.code)
 
314
        return "<%s %s/%s>" % (type(self).__name__, self.year, self.semester)
325
315
 
326
316
class Offering(Storm):
327
317
    """An offering of a subject in a particular semester."""
612
602
        return "<%s '%s' in %r>" % (type(self).__name__, self.short_name,
613
603
                                  self.project_set.offering)
614
604
 
615
 
    def can_submit(self, principal, user, late=False):
616
 
        """
617
 
        @param late: If True, does not take the deadline into account.
618
 
        """
 
605
    def can_submit(self, principal, user):
619
606
        return (self in principal.get_projects() and
620
 
                (late or not self.has_deadline_passed(user)))
 
607
                not self.has_deadline_passed(user))
621
608
 
622
 
    def submit(self, principal, path, revision, who, late=False):
 
609
    def submit(self, principal, path, revision, who):
623
610
        """Submit a Subversion path and revision to a project.
624
611
 
625
612
        @param principal: The owner of the Subversion repository, and the
627
614
        @param path: A path within that repository to submit.
628
615
        @param revision: The revision of that path to submit.
629
616
        @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.)
632
617
        """
633
618
 
634
 
        if not self.can_submit(principal, who, late=late):
 
619
        if not self.can_submit(principal, who):
635
620
            raise DeadlinePassed()
636
621
 
637
622
        a = Assessed.get(Store.of(self), principal, self)
740
725
            Semester.id == Offering.semester_id,
741
726
            (not active_only) or (Semester.state == u'current'))
742
727
 
743
 
    def get_svn_url(self, config):
744
 
        """Get the subversion repository URL for this user or group."""
745
 
        url = config['urls']['svn_addr']
746
 
        path = 'groups/%s_%s_%s_%s' % (
747
 
                self.project_set.offering.subject.short_name,
748
 
                self.project_set.offering.semester.year,
749
 
                self.project_set.offering.semester.url_name,
750
 
                self.name
751
 
                )
752
 
        return urlparse.urljoin(url, path)
753
728
 
754
729
    def get_permissions(self, user, config):
755
730
        if user.admin or user in self.members:
867
842
    id = Int(name="extensionid", primary=True)
868
843
    assessed_id = Int(name="assessedid")
869
844
    assessed = Reference(assessed_id, Assessed.id)
870
 
    days = Int()
 
845
    deadline = DateTime()
871
846
    approver_id = Int(name="approver")
872
847
    approver = Reference(approver_id, User.id)
873
848
    notes = Unicode()
915
890
        return "/files/%s/%s/%s?r=%d" % (user.login,
916
891
            self.assessed.checkout_location, submitpath, self.revision)
917
892
 
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')))
925
 
        else:
926
 
            return os.path.join(base, urllib.quote(self.path.encode('utf-8')))
927
 
 
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
936
 
        # extension, if any
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)
940
 
 
941
893
    @staticmethod
942
894
    def test_and_normalise_path(path):
943
895
        """Test that path is valid, and normalise it. This prevents possible
957
909
            raise SubmissionError("Path must not contain '\\n', '[' or ']'")
958
910
        return os.path.normpath(path)
959
911
 
960
 
    @property
961
 
    def late(self):
962
 
        """True if the project was submitted late."""
963
 
        return self.days_late > 0
964
 
 
965
 
    @property
966
 
    def days_late(self):
967
 
        """The number of days the project was submitted late (rounded up), or
968
 
        0 if on-time."""
969
 
        # XXX: Need to respect extensions.
970
 
        return max(0,
971
 
            (self.date_submitted - self.assessed.project.deadline).days + 1)
972
 
 
973
912
# WORKSHEETS AND EXERCISES #
974
913
 
975
914
class Exercise(Storm):