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
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)
259
260
if dry: return query
260
261
return self.db.query(query).dictresult()
263
def start_transaction(self, dry=False):
264
"""Starts a DB transaction.
265
Will not commit any changes until self.commit() is called.
267
query = "START TRANSACTION;"
271
def commit(self, dry=False):
272
"""Commits (ends) a DB transaction.
273
Commits all changes since the call to start_transaction.
279
def rollback(self, dry=False):
280
"""Rolls back (ends) a DB transaction, undoing all changes since the
281
call to start_transaction.
262
287
# USER MANAGEMENT FUNCTIONS #
264
289
login_primary = frozenset(["login"])
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
324
349
Raises a DBException if the login is not found in the DB.
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)
355
return userdict # Query string
356
# Package into a User object
357
return user.User(**userdict)
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.
334
return self.get_all("login", self.login_getfields, dry=dry)
362
userdicts = self.get_all("login", self.login_getfields, dry=dry)
364
return userdicts # Query string
365
# Package into User objects
366
return [user.User(**userdict) for userdict in userdicts]
336
368
def user_authenticate(self, login, password, dry=False):
337
369
"""Performs a password authentication on a user. Returns True if