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

« back to all changes in this revision

Viewing changes to www/common/db.py

  • Committer: mattgiuca
  • Date: 2008-02-01 05:51:53 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:372
Added new module: common/db.py.
This is a Data Access Layer which will handle ALL direct DB interaction.
No other module should import pg, or feature any SQL code strings.

Currently implements the DB class, and create_user and drop_user
functionality.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# IVLE - Informatics Virtual Learning Environment
 
2
# Copyright (C) 2007-2008 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
# Module: Database
 
19
# Author: Matt Giuca
 
20
# Date:   1/2/2008
 
21
 
 
22
# Code to talk to the PostgreSQL database.
 
23
# (This is the Data Access Layer).
 
24
# All DB code should be in this module to ensure portability if we want to
 
25
# change the DB implementation.
 
26
# This means no SQL strings should be outside of this module. Add functions
 
27
# here to perform the activities needed, and place the SQL code for those
 
28
# activities within.
 
29
 
 
30
# CAUTION to editors of this module.
 
31
# All string inputs must be sanitized by calling _escape before being
 
32
# formatted into an SQL query string.
 
33
 
 
34
import pg
 
35
import conf
 
36
import md5
 
37
 
 
38
def _escape(str):
 
39
    """Wrapper around pg.escape_string. Escapes the string for use in SQL, and
 
40
    also quotes it to make sure that every string used in a query is quoted.
 
41
    """
 
42
    return "'" + pg.escape_string(str) + "'"
 
43
 
 
44
class DB:
 
45
    """An IVLE database object. This object provides an interface to
 
46
    interacting with the IVLE database without using any external SQL.
 
47
 
 
48
    Most methods of this class have an optional dry argument. If true, they
 
49
    will return the SQL query string and NOT actually execute it. (For
 
50
    debugging purposes).
 
51
    """
 
52
    def __init__(self):
 
53
        """Connects to the database and creates a DB object.
 
54
        Takes no parameters - gets all the DB info from the configuration."""
 
55
        self.db = pg.connect(dbname=conf.db_dbname, host=conf.db_host,
 
56
                port=conf.db_port, user=conf.db_user, passwd=conf.db_password)
 
57
 
 
58
    @staticmethod
 
59
    def _passhash(password):
 
60
        return md5.md5(password).hexdigest()
 
61
 
 
62
    def create_user(self, login, password, nick, fullname, rolenm, studentid,
 
63
        dry=False):
 
64
        """Creates a user login entry in the database.
 
65
        Arguments are the same as those in the "login" table of the schema.
 
66
        The exception is "password", which is a cleartext password. makeuser
 
67
        will hash the password.
 
68
        """
 
69
        passhash = DB._passhash(password)
 
70
        query = ("INSERT INTO login (login, passhash, nick, fullname, "
 
71
            "rolenm, studentid) VALUES (%s, %s, %s, %s, %s, %s);" %
 
72
            (_escape(login), _escape(passhash), _escape(nick),
 
73
            _escape(fullname), _escape(rolenm), _escape(studentid)))
 
74
        if dry: return query
 
75
        self.db.query(query)
 
76
 
 
77
    def drop_user(self, login, dry=False):
 
78
        """Deletes a user login entry from the database."""
 
79
        query = "DELETE FROM login WHERE login = %s;" % _escape(login)
 
80
        if dry: return query
 
81
        self.db.query(query)
 
82
 
 
83
    def close(self):
 
84
        """Close the DB connection. Do not call any other functions after
 
85
        this. (The behaviour of doing so is undefined).
 
86
        """
 
87
        self.db.close()