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

« back to all changes in this revision

Viewing changes to lib/common/db.py

  • Committer: mattgiuca
  • Date: 2008-02-15 06:34:19 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:476
Added new module: common/caps.py. This is the Capabilities centre of IVLE.
    It provides a Role class which is a rich enumeration type for Roles.
    The Role class will replace what has previously been simple strings used
    for Roles within the program. It has comparison ability to see if a Role
    is greater than or equal to another.
    This module also provides a set of capability objects which roles can be
    checked against.
dispatch/login: Rather than setting 'rolenm' string in session, now sets
    'role', a Role object.
common/db: _escape allows Role objects, which get converted into strings.
    So the DB now accepts Role objects as values (though we don't make use of
    this currently).
www/apps/tos: svn:ignore

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
import md5
37
37
import copy
38
38
 
39
 
from common import (caps, user)
 
39
from common import caps
40
40
 
41
41
def _escape(val):
42
42
    """Wrapper around pg.escape_string. Prepares the Python value for use in
94
94
    def __init__(self):
95
95
        """Connects to the database and creates a DB object.
96
96
        Takes no parameters - gets all the DB info from the configuration."""
97
 
        self.open = False
98
97
        self.db = pg.connect(dbname=conf.db_dbname, host=conf.db_host,
99
98
                port=conf.db_port, user=conf.db_user, passwd=conf.db_password)
100
99
        self.open = True
260
259
        if dry: return query
261
260
        return self.db.query(query).dictresult()
262
261
 
263
 
    def start_transaction(self, dry=False):
264
 
        """Starts a DB transaction.
265
 
        Will not commit any changes until self.commit() is called.
266
 
        """
267
 
        query = "START TRANSACTION;"
268
 
        if dry: return query
269
 
        self.db.query(query)
270
 
 
271
 
    def commit(self, dry=False):
272
 
        """Commits (ends) a DB transaction.
273
 
        Commits all changes since the call to start_transaction.
274
 
        """
275
 
        query = "COMMIT;"
276
 
        if dry: return query
277
 
        self.db.query(query)
278
 
 
279
 
    def rollback(self, dry=False):
280
 
        """Rolls back (ends) a DB transaction, undoing all changes since the
281
 
        call to start_transaction.
282
 
        """
283
 
        query = "ROLLBACK;"
284
 
        if dry: return query
285
 
        self.db.query(query)
286
 
 
287
262
    # USER MANAGEMENT FUNCTIONS #
288
263
 
289
264
    login_primary = frozenset(["login"])
343
318
            dry=dry)
344
319
 
345
320
    def get_user(self, login, dry=False):
346
 
        """Given a login, returns a User object containing details looked up
347
 
        in the DB.
 
321
        """Given a login, returns a dictionary of the user's DB fields,
 
322
        excluding the passhash field.
348
323
 
349
324
        Raises a DBException if the login is not found in the DB.
350
325
        """
351
 
        userdict = self.get_single({"login": login}, "login",
 
326
        return self.get_single({"login": login}, "login",
352
327
            self.login_getfields, self.login_primary,
353
328
            error_notfound="get_user: No user with that login name", dry=dry)
354
 
        if dry:
355
 
            return userdict     # Query string
356
 
        # Package into a User object
357
 
        return user.User(**userdict)
358
329
 
359
330
    def get_users(self, dry=False):
360
 
        """Returns a list of all users in the DB, as User objects.
 
331
        """Returns a list of all users. The list elements are a dictionary of
 
332
        the user's DB fields, excluding the passhash field.
361
333
        """
362
 
        userdicts = self.get_all("login", self.login_getfields, dry=dry)
363
 
        if dry:
364
 
            return userdicts    # Query string
365
 
        # Package into User objects
366
 
        return [user.User(**userdict) for userdict in userdicts]
 
334
        return self.get_all("login", self.login_getfields, dry=dry)
367
335
 
368
336
    def user_authenticate(self, login, password, dry=False):
369
337
        """Performs a password authentication on a user. Returns True if