~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-19 00:54:28 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:500
db: get_user and get_users now return User objects instead of dictionaries.
    This is the major part of replacing dicts with User objects, as it
    propagates upwards.

Propagated User objects up through the following modules:
    listusers.py, dispatch.login, authenticate, userservice, forumutil
All of these now treat users as an object rather than a dict.

To save on the size of the changes so far, login still individually copies
fields over to the session (so the session does not yet store a user object;
that is the second part of this refactor).

WOO!! Revision 500 :)

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
 
39
from common import (caps, user)
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
97
98
        self.db = pg.connect(dbname=conf.db_dbname, host=conf.db_host,
98
99
                port=conf.db_port, user=conf.db_user, passwd=conf.db_password)
99
100
        self.open = True
259
260
        if dry: return query
260
261
        return self.db.query(query).dictresult()
261
262
 
 
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
 
262
287
    # USER MANAGEMENT FUNCTIONS #
263
288
 
264
289
    login_primary = frozenset(["login"])
318
343
            dry=dry)
319
344
 
320
345
    def get_user(self, login, dry=False):
321
 
        """Given a login, returns a dictionary of the user's DB fields,
322
 
        excluding the passhash field.
 
346
        """Given a login, returns a User object containing details looked up
 
347
        in the DB.
323
348
 
324
349
        Raises a DBException if the login is not found in the DB.
325
350
        """
326
 
        return self.get_single({"login": login}, "login",
 
351
        userdict = self.get_single({"login": login}, "login",
327
352
            self.login_getfields, self.login_primary,
328
353
            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)
329
358
 
330
359
    def get_users(self, dry=False):
331
 
        """Returns a list of all users. The list elements are a dictionary of
332
 
        the user's DB fields, excluding the passhash field.
 
360
        """Returns a list of all users in the DB, as User objects.
333
361
        """
334
 
        return self.get_all("login", self.login_getfields, dry=dry)
 
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]
335
367
 
336
368
    def user_authenticate(self, login, password, dry=False):
337
369
        """Performs a password authentication on a user. Returns True if