Changeset - 163464441e0d
[Not reviewed]
default
0 1 0
Marcin Kuzminski - 16 years ago 2010-04-10 19:07:13
marcin@python-blog.com
updated db manage script for creating interactive admin account and db
1 file changed with 29 insertions and 10 deletions:
0 comments (0 inline, 0 general)
pylons_app/lib/db_manage.py
Show inline comments
 
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__))))
 
logging.basicConfig(level=logging.DEBUG)
 

	
 
def get_sqlite_conn_cur():
 
    conn = sqlite3.connect(os.path.join(ROOT, 'auth.sqlite'))
 
    conn = sqlite3.connect(os.path.join(ROOT, 'hg_app.db'))
 
    cur = conn.cursor()
 
    return conn, cur
 

	
 
def create_user_table():
 
def check_for_db():
 
    if os.path.isfile(os.path.join(ROOT, 'hg_app.db')):
 
        raise Exception('database already exists')
 

	
 
def create_tables():
 
    """
 
    Create a auth database
 
    """
 
    check_for_db()
 
    conn, cur = get_sqlite_conn_cur()
 
    try:
 
        log.info('creating table %s', 'users')
 
        logging.info('creating table %s', 'users')
 
        cur.execute("""DROP TABLE IF EXISTS users """)
 
        cur.execute("""CREATE TABLE users
 
                        (user_id INTEGER PRIMARY KEY AUTOINCREMENT, 
 
@@ -25,13 +31,14 @@ def create_user_table():
 
                         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
 
        logging.info('creating table %s', 'user_loggings')
 
        cur.execute("""DROP TABLE IF EXISTS user_loggings """)
 
        cur.execute("""CREATE TABLE user_loggings
 
                        (id INTEGER PRIMARY KEY AUTOINCREMENT,
 
                            user_id INTEGER,
 
                            last_action TEXT, 
 
                            last_action_date DATETIME)""")
 
                            repository TEXT,
 
                            action TEXT, 
 
                            action_date DATETIME)""")
 
        conn.commit()
 
    except:
 
        conn.rollback()
 
@@ -39,10 +46,16 @@ def create_user_table():
 
    
 
    cur.close()
 
    
 
def admin_prompt():
 
    import getpass
 
    username = raw_input('give username:')
 
    password = getpass.getpass('Specify admin password:')
 
    create_user(username, password, True)
 
    
 
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)
 
    logging.info('creating user %s', username)
 
    try:
 
        cur.execute("""INSERT INTO users values (?,?,?,?,?) """,
 
                    (None, username, password_crypt, 1, admin))     
 
@@ -50,3 +63,9 @@ def create_user(username, password, admi
 
    except:
 
        conn.rollback()
 
        raise
 
    
 
if __name__ == '__main__':
 
    create_tables()
 
    admin_prompt()  
 

	
 

	
0 comments (0 inline, 0 general)