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

« back to all changes in this revision

Viewing changes to ivle/database.py

  • Committer: matt.giuca
  • Date: 2009-01-12 00:36:24 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1074
Removed "prototypes" directory (very old and clearly no longer used).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# IVLE - Informatics Virtual Learning Environment
2
 
# Copyright (C) 2007-2009 The University of Melbourne
3
 
#
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; either version 2 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
 
 
18
 
# Author: Matt Giuca, Will Grant
19
 
 
20
 
"""
21
 
Database Classes and Utilities for Storm ORM
22
 
 
23
 
This module provides all of the classes which map to database tables.
24
 
It also provides miscellaneous utility functions for database interaction.
25
 
"""
26
 
 
27
 
from storm.locals import create_database, Store, Int, Unicode, DateTime, \
28
 
                         Reference
29
 
 
30
 
import ivle.conf
31
 
import ivle.caps
32
 
 
33
 
def get_conn_string():
34
 
    """
35
 
    Returns the Storm connection string, generated from the conf file.
36
 
    """
37
 
    return "postgres://%s:%s@%s:%d/%s" % (ivle.conf.db_user,
38
 
        ivle.conf.db_password, ivle.conf.db_host, ivle.conf.db_port,
39
 
        ivle.conf.db_dbname)
40
 
 
41
 
def get_store():
42
 
    """
43
 
    Open a database connection and transaction. Return a storm.store.Store
44
 
    instance connected to the configured IVLE database.
45
 
    """
46
 
    return Store(create_database(get_conn_string()))
47
 
 
48
 
class User(object):
49
 
    """
50
 
    Represents an IVLE user.
51
 
    """
52
 
    __storm_table__ = "login"
53
 
 
54
 
    id = Int(primary=True, name="loginid")
55
 
    login = Unicode()
56
 
    passhash = Unicode()
57
 
    state = Unicode()
58
 
    rolenm = Unicode()
59
 
    unixid = Int()
60
 
    nick = Unicode()
61
 
    pass_exp = DateTime()
62
 
    acct_exp = DateTime()
63
 
    last_login = DateTime()
64
 
    svn_pass = Unicode()
65
 
    email = Unicode()
66
 
    fullname = Unicode()
67
 
    studentid = Unicode()
68
 
    settings = Unicode()
69
 
 
70
 
    def _get_role(self):
71
 
        if self.rolenm is None:
72
 
            return None
73
 
        return ivle.caps.Role(self.rolenm)
74
 
    def _set_role(self, value):
75
 
        if not isinstance(value, ivle.caps.Role):
76
 
            raise TypeError("role must be an ivle.caps.Role")
77
 
        self.rolenm = unicode(value)
78
 
    role = property(_get_role, _set_role)
79
 
 
80
 
    def __init__(self, **kwargs):
81
 
        """
82
 
        Create a new User object. Supply any columns as a keyword argument.
83
 
        """
84
 
        for k,v in kwargs.items():
85
 
            if k.startswith('_') or not hasattr(self, k):
86
 
                raise TypeError("User got an unexpected keyword argument '%s'"
87
 
                    % k)
88
 
            setattr(self, k, v)
89
 
 
90
 
    def __repr__(self):
91
 
        return "<%s '%s'>" % (type(self).__name__, self.login)
92
 
 
93
 
    def hasCap(self, capability):
94
 
        """Given a capability (which is a Role object), returns True if this
95
 
        User has that capability, False otherwise.
96
 
        """
97
 
        return self.role.hasCap(capability)
98
 
 
99
 
    # XXX Should be @property
100
 
    def pass_expired(self):
101
 
        """Determines whether the pass_exp field indicates that
102
 
           login should be denied.
103
 
        """
104
 
        fieldval = self.pass_exp
105
 
        return fieldval is not None and time.localtime() > fieldval
106
 
    # XXX Should be @property
107
 
    def acct_expired(self):
108
 
        """Determines whether the acct_exp field indicates that
109
 
           login should be denied.
110
 
        """
111
 
        fieldval = self.acct_exp
112
 
        return fieldval is not None and time.localtime() > fieldval
113
 
 
114
 
    @classmethod
115
 
    def get_by_login(cls, store, login):
116
 
        """
117
 
        Get the User from the db associated with a given store and
118
 
        login.
119
 
        """
120
 
        return store.find(cls, cls.login == unicode(login)).one()