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

« back to all changes in this revision

Viewing changes to ivle/database.py

  • Committer: Matt Giuca
  • Date: 2009-04-22 05:14:58 UTC
  • Revision ID: matt.giuca@gmail.com-20090422051458-vh65hoaa3a54knxq
Stopped clobbering conf.py within the jail, using a proper ivle.conf instead.
Now works with Python 2.6 (and can have another version, possible 2.6, inside
the jail).

ivle/config/ivle-spec.conf: Added user_info section with in-jail variables.
ivle/makeuser: Writes a conf file (ivle.conf) instead of conf.py.
ivle/conf/conf.py: Added emulation layer bindings for the in-jail variables.
ivle-buildjail: No longer REMOVES the conf.py file (since it's just a normal
    source file now).

(--author Will)

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
It also provides miscellaneous utility functions for database interaction.
25
25
"""
26
26
 
27
 
import hashlib
 
27
import md5
28
28
import datetime
29
29
 
30
30
from storm.locals import create_database, Store, Int, Unicode, DateTime, \
31
31
                         Reference, ReferenceSet, Bool, Storm, Desc
32
32
from storm.exceptions import NotOneError, IntegrityError
33
33
 
 
34
import ivle.conf
34
35
from ivle.worksheet.rst import rst
35
36
 
36
37
__all__ = ['get_store',
50
51
                % (self.__class__.__name__, k))
51
52
        setattr(self, k, v)
52
53
 
53
 
def get_conn_string(config):
54
 
    """Create a Storm connection string to the IVLE database
55
 
 
56
 
    @param config: The IVLE configuration.
 
54
def get_conn_string():
 
55
    """
 
56
    Returns the Storm connection string, generated from the conf file.
57
57
    """
58
58
 
59
59
    clusterstr = ''
60
 
    if config['database']['username']:
61
 
        clusterstr += config['database']['username']
62
 
        if config['database']['password']:
63
 
            clusterstr += ':' + config['database']['password']
 
60
    if ivle.conf.db_user:
 
61
        clusterstr += ivle.conf.db_user
 
62
        if ivle.conf.db_password:
 
63
            clusterstr += ':' + ivle.conf.db_password
64
64
        clusterstr += '@'
65
65
 
66
 
    host = config['database']['host'] or 'localhost'
67
 
    port = config['database']['port'] or 5432
 
66
    host = ivle.conf.db_host or 'localhost'
 
67
    port = ivle.conf.db_port or 5432
68
68
 
69
69
    clusterstr += '%s:%d' % (host, port)
70
70
 
71
 
    return "postgres://%s/%s" % (clusterstr, config['database']['name'])
72
 
 
73
 
def get_store(config):
74
 
    """Create a Storm store connected to the IVLE database.
75
 
 
76
 
    @param config: The IVLE configuration.
77
 
    """
78
 
    return Store(create_database(get_conn_string(config)))
 
71
    return "postgres://%s/%s" % (clusterstr, ivle.conf.db_dbname)
 
72
 
 
73
def get_store():
 
74
    """
 
75
    Open a database connection and transaction. Return a storm.store.Store
 
76
    instance connected to the configured IVLE database.
 
77
    """
 
78
    return Store(create_database(get_conn_string()))
79
79
 
80
80
# USERS #
81
81
 
214
214
 
215
215
    @staticmethod
216
216
    def hash_password(password):
217
 
        return hashlib.md5(password).hexdigest()
 
217
        return md5.md5(password).hexdigest()
218
218
 
219
219
    @classmethod
220
220
    def get_by_login(cls, store, login):
256
256
                perms.add('edit')
257
257
        return perms
258
258
 
259
 
    def active_offerings(self):
260
 
        """Return a sequence of currently active offerings for this subject
261
 
        (offerings whose semester.state is "current"). There should be 0 or 1
262
 
        elements in this sequence, but it's possible there are more.
263
 
        """
264
 
        return self.offerings.find(Offering.semester_id == Semester.id,
265
 
                                   Semester.state == u'current')
266
 
 
267
 
    def offering_for_semester(self, year, semester):
268
 
        """Get the offering for the given year/semester, or None."""
269
 
        return self.offerings.find(Offering.semester_id == Semester.id,
270
 
                               Semester.year == unicode(year),
271
 
                               Semester.semester == unicode(semester)).one()
272
 
 
273
259
class Semester(Storm):
274
260
    __storm_table__ = "semester"
275
261
 
674
660
    def __repr__(self):
675
661
        return "<%s %s>" % (type(self).__name__, self.name)
676
662
 
 
663
    # XXX Refactor this - make it an instance method of Subject rather than a
 
664
    # class method of Worksheet. Can't do that now because Subject isn't
 
665
    # linked referentially to the Worksheet.
 
666
    @classmethod
 
667
    def get_by_name(cls, store, subjectname, worksheetname):
 
668
        """
 
669
        Get the Worksheet from the db associated with a given store, subject
 
670
        name and worksheet name.
 
671
        """
 
672
        return store.find(cls, cls.subject == unicode(subjectname),
 
673
            cls.name == unicode(worksheetname)).one()
 
674
 
677
675
    def remove_all_exercises(self):
678
676
        """
679
677
        Remove all exercises from this worksheet.