~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-25 03:10:35 UTC
  • Revision ID: grantw@unimelb.edu.au-20100225031035-9al4btb2kxpzav9b
Override MediaFileView.authorize(), so no permission (and therefore user) retrieval requests are made for media files.

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
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
 
437
429
        # XXX: Respect extensions.
438
430
        return self.projects.find(Project.deadline > datetime.datetime.now())
439
431
 
440
 
    def has_worksheet_cutoff_passed(self, user):
441
 
        """Check whether the worksheet cutoff has passed.
442
 
        A user is required, in case we support extensions.
443
 
        """
444
 
        if self.worksheet_cutoff is None:
445
 
            return False
446
 
        else:
447
 
            return self.worksheet_cutoff < datetime.datetime.now()
448
 
 
449
432
    def clone_worksheets(self, source):
450
433
        """Clone all worksheets from the specified source to this offering."""
451
434
        import ivle.worksheet.utils
666
649
            return
667
650
        return assessed.submissions
668
651
 
669
 
    @property
670
 
    def can_delete(self):
671
 
        """Can only delete if there are no submissions."""
672
 
        return self.submissions.count() == 0
673
652
 
674
 
    def delete(self):
675
 
        """Delete the project. Fails if can_delete is False."""
676
 
        if not self.can_delete:
677
 
            raise IntegrityError()
678
 
        for assessed in self.assesseds:
679
 
            assessed.delete()
680
 
        Store.of(self).remove(self)
681
653
 
682
654
class ProjectGroup(Storm):
683
655
    """A group of students working together on a project."""
733
705
            Semester.id == Offering.semester_id,
734
706
            (not active_only) or (Semester.state == u'current'))
735
707
 
736
 
    def get_svn_url(self, config):
737
 
        """Get the subversion repository URL for this user or group."""
738
 
        url = config['urls']['svn_addr']
739
 
        path = 'groups/%s_%s_%s_%s' % (
740
 
                self.project_set.offering.subject.short_name,
741
 
                self.project_set.offering.semester.year,
742
 
                self.project_set.offering.semester.semester,
743
 
                self.name
744
 
                )
745
 
        return urlparse.urljoin(url, path)
746
708
 
747
709
    def get_permissions(self, user, config):
748
710
        if user.admin or user in self.members:
840
802
 
841
803
        return a
842
804
 
843
 
    def delete(self):
844
 
        """Delete the assessed. Fails if there are any submissions. Deletes
845
 
        extensions."""
846
 
        if self.submissions.count() > 0:
847
 
            raise IntegrityError()
848
 
        for extension in self.extensions:
849
 
            extension.delete()
850
 
        Store.of(self).remove(self)
851
805
 
852
806
class ProjectExtension(Storm):
853
807
    """An extension granted to a user or group on a particular project.
865
819
    approver = Reference(approver_id, User.id)
866
820
    notes = Unicode()
867
821
 
868
 
    def delete(self):
869
 
        """Delete the extension."""
870
 
        Store.of(self).remove(self)
871
 
 
872
822
class SubmissionError(Exception):
873
823
    """Denotes a validation error during submission."""
874
824
    pass
908
858
        return "/files/%s/%s/%s?r=%d" % (user.login,
909
859
            self.assessed.checkout_location, submitpath, self.revision)
910
860
 
911
 
    def get_svn_url(self, config):
912
 
        """Get subversion URL for this submission"""
913
 
        princ = self.assessed.principal
914
 
        base = princ.get_svn_url(config)
915
 
        if self.path.startswith(os.sep):
916
 
            return os.path.join(base,
917
 
                    urllib.quote(self.path[1:].encode('utf-8')))
918
 
        else:
919
 
            return os.path.join(base, urllib.quote(self.path.encode('utf-8')))
920
 
 
921
 
    def get_svn_export_command(self, req):
922
 
        """Returns a Unix shell command to export a submission"""
923
 
        svn_url = self.get_svn_url(req.config)
924
 
        username = (req.user.login if req.user.login.isalnum() else
925
 
                "'%s'"%req.user.login)
926
 
        export_dir = self.assessed.principal.short_name
927
 
        return "svn export --username %s -r%d '%s' %s"%(req.user.login,
928
 
                self.revision, svn_url, export_dir)
929
 
 
930
861
    @staticmethod
931
862
    def test_and_normalise_path(path):
932
863
        """Test that path is valid, and normalise it. This prevents possible
958
889
    id = Unicode(primary=True, name="identifier")
959
890
    name = Unicode()
960
891
    description = Unicode()
961
 
    _description_xhtml_cache = Unicode(name='description_xhtml_cache')
962
892
    partial = Unicode()
963
893
    solution = Unicode()
964
894
    include = Unicode()
1007
937
 
1008
938
        return perms
1009
939
 
1010
 
    def _cache_description_xhtml(self, invalidate=False):
1011
 
        # Don't regenerate an existing cache unless forced.
1012
 
        if self._description_xhtml_cache is not None and not invalidate:
1013
 
            return
1014
 
 
1015
 
        if self.description:
1016
 
            self._description_xhtml_cache = rst(self.description)
1017
 
        else:
1018
 
            self._description_xhtml_cache = None
1019
 
 
1020
 
    @property
1021
 
    def description_xhtml(self):
1022
 
        """The XHTML exercise description, converted from reStructuredText."""
1023
 
        self._cache_description_xhtml()
1024
 
        return self._description_xhtml_cache
1025
 
 
1026
 
    def set_description(self, description):
1027
 
        self.description = description
1028
 
        self._cache_description_xhtml(invalidate=True)
 
940
    def get_description(self):
 
941
        """Return the description interpreted as reStructuredText."""
 
942
        return rst(self.description)
1029
943
 
1030
944
    def delete(self):
1031
945
        """Deletes the exercise, providing it has no associated worksheets."""
1050
964
    assessable = Bool()
1051
965
    published = Bool()
1052
966
    data = Unicode()
1053
 
    _data_xhtml_cache = Unicode(name='data_xhtml_cache')
1054
967
    seq_no = Int()
1055
968
    format = Unicode()
1056
969
 
1103
1016
 
1104
1017
        return perms
1105
1018
 
1106
 
    def _cache_data_xhtml(self, invalidate=False):
1107
 
        # Don't regenerate an existing cache unless forced.
1108
 
        if self._data_xhtml_cache is not None and not invalidate:
1109
 
            return
1110
 
 
1111
 
        if self.format == u'rst':
1112
 
            self._data_xhtml_cache = rst(self.data)
1113
 
        else:
1114
 
            self._data_xhtml_cache = None
1115
 
 
1116
 
    @property
1117
 
    def data_xhtml(self):
1118
 
        """The XHTML of this worksheet, converted from rST if required."""
1119
 
        # Update the rST -> XHTML cache, if required.
1120
 
        self._cache_data_xhtml()
1121
 
 
1122
 
        if self.format == u'rst':
1123
 
            return self._data_xhtml_cache
 
1019
    def get_xml(self):
 
1020
        """Returns the xml of this worksheet, converts from rst if required."""
 
1021
        if self.format == u'rst':
 
1022
            ws_xml = rst(self.data)
 
1023
            return ws_xml
1124
1024
        else:
1125
1025
            return self.data
1126
1026
 
1127
 
    def set_data(self, data):
1128
 
        self.data = data
1129
 
        self._cache_data_xhtml(invalidate=True)
1130
 
 
1131
1027
    def delete(self):
1132
1028
        """Deletes the worksheet, provided it has no attempts on any exercises.
1133
1029