# HG changeset patch # User Marcin Kuzminski # Date 2010-04-10 13:18:06 # Node ID 736078908f37d88f25bce35604b0206223bda80e # Parent 8fb1abd4178aaee619eea493810872fae567b44d added db_manage script diff --git a/pylons_app/lib/db_manage.py b/pylons_app/lib/db_manage.py new file mode 100644 --- /dev/null +++ b/pylons_app/lib/db_manage.py @@ -0,0 +1,52 @@ +import logging +import sqlite3 +log = logging.getLogger(__name__) +import os +import crypt +from os.path import dirname as dn +ROOT = dn(dn(dn(os.path.realpath(__file__)))) + +def get_sqlite_conn_cur(): + conn = sqlite3.connect(os.path.join(ROOT, 'auth.sqlite')) + cur = conn.cursor() + return conn, cur + +def create_user_table(): + """ + Create a auth database + """ + conn, cur = get_sqlite_conn_cur() + try: + log.info('creating table %s', 'users') + cur.execute("""DROP TABLE IF EXISTS users """) + cur.execute("""CREATE TABLE users + (user_id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT, + password TEXT, + active INTEGER, + admin INTEGER)""") + log.info('creating table %s', 'user_logs') + cur.execute("""DROP TABLE IF EXISTS user_logs """) + cur.execute("""CREATE TABLE user_logs + (id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER, + last_action TEXT, + last_action_date DATETIME)""") + conn.commit() + except: + conn.rollback() + raise + + cur.close() + +def create_user(username, password, admin=False): + conn, cur = get_sqlite_conn_cur() + password_crypt = crypt.crypt(password, '6a') + log.info('creating user %s', username) + try: + cur.execute("""INSERT INTO users values (?,?,?,?,?) """, + (None, username, password_crypt, 1, admin)) + conn.commit() + except: + conn.rollback() + raise