24
24
It also provides miscellaneous utility functions for database interaction.
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
34
35
from ivle.worksheet.rst import rst
36
37
__all__ = ['get_store',
50
51
% (self.__class__.__name__, k))
51
52
setattr(self, k, v)
53
def get_conn_string(config):
54
"""Create a Storm connection string to the IVLE database
56
@param config: The IVLE configuration.
54
def get_conn_string():
56
Returns the Storm connection string, generated from the conf file.
60
if config['database']['username']:
61
clusterstr += config['database']['username']
62
if config['database']['password']:
63
clusterstr += ':' + config['database']['password']
61
clusterstr += ivle.conf.db_user
62
if ivle.conf.db_password:
63
clusterstr += ':' + ivle.conf.db_password
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
69
69
clusterstr += '%s:%d' % (host, port)
71
return "postgres://%s/%s" % (clusterstr, config['database']['name'])
73
def get_store(config):
74
"""Create a Storm store connected to the IVLE database.
76
@param config: The IVLE configuration.
78
return Store(create_database(get_conn_string(config)))
71
return "postgres://%s/%s" % (clusterstr, ivle.conf.db_dbname)
75
Open a database connection and transaction. Return a storm.store.Store
76
instance connected to the configured IVLE database.
78
return Store(create_database(get_conn_string()))
216
216
def hash_password(password):
217
return hashlib.md5(password).hexdigest()
217
return md5.md5(password).hexdigest()
220
220
def get_by_login(cls, store, login):
256
256
perms.add('edit')
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.
264
return self.offerings.find(Offering.semester_id == Semester.id,
265
Semester.state == u'current')
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()
273
259
class Semester(Storm):
274
260
__storm_table__ = "semester"
674
660
def __repr__(self):
675
661
return "<%s %s>" % (type(self).__name__, self.name)
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.
667
def get_by_name(cls, store, subjectname, worksheetname):
669
Get the Worksheet from the db associated with a given store, subject
670
name and worksheet name.
672
return store.find(cls, cls.subject == unicode(subjectname),
673
cls.name == unicode(worksheetname)).one()
677
675
def remove_all_exercises(self):
679
677
Remove all exercises from this worksheet.