comparison pylons_app/lib/db_manage.py @ 66:163464441e0d

updated db manage script for creating interactive admin account and db
author Marcin Kuzminski <marcin@python-blog.com>
date Sat, 10 Apr 2010 19:07:13 +0200
parents 736078908f37
children ffd9ff6e2f33
comparison
equal deleted inserted replaced
65:3f65447f6c02 66:163464441e0d
1 import logging 1 import logging
2 import sqlite3 2 import sqlite3
3 log = logging.getLogger(__name__) 3
4 import os 4 import os
5 import crypt 5 import crypt
6 from os.path import dirname as dn 6 from os.path import dirname as dn
7 ROOT = dn(dn(dn(os.path.realpath(__file__)))) 7 ROOT = dn(dn(dn(os.path.realpath(__file__))))
8 logging.basicConfig(level=logging.DEBUG)
8 9
9 def get_sqlite_conn_cur(): 10 def get_sqlite_conn_cur():
10 conn = sqlite3.connect(os.path.join(ROOT, 'auth.sqlite')) 11 conn = sqlite3.connect(os.path.join(ROOT, 'hg_app.db'))
11 cur = conn.cursor() 12 cur = conn.cursor()
12 return conn, cur 13 return conn, cur
13 14
14 def create_user_table(): 15 def check_for_db():
16 if os.path.isfile(os.path.join(ROOT, 'hg_app.db')):
17 raise Exception('database already exists')
18
19 def create_tables():
15 """ 20 """
16 Create a auth database 21 Create a auth database
17 """ 22 """
23 check_for_db()
18 conn, cur = get_sqlite_conn_cur() 24 conn, cur = get_sqlite_conn_cur()
19 try: 25 try:
20 log.info('creating table %s', 'users') 26 logging.info('creating table %s', 'users')
21 cur.execute("""DROP TABLE IF EXISTS users """) 27 cur.execute("""DROP TABLE IF EXISTS users """)
22 cur.execute("""CREATE TABLE users 28 cur.execute("""CREATE TABLE users
23 (user_id INTEGER PRIMARY KEY AUTOINCREMENT, 29 (user_id INTEGER PRIMARY KEY AUTOINCREMENT,
24 username TEXT, 30 username TEXT,
25 password TEXT, 31 password TEXT,
26 active INTEGER, 32 active INTEGER,
27 admin INTEGER)""") 33 admin INTEGER)""")
28 log.info('creating table %s', 'user_logs') 34 logging.info('creating table %s', 'user_loggings')
29 cur.execute("""DROP TABLE IF EXISTS user_logs """) 35 cur.execute("""DROP TABLE IF EXISTS user_loggings """)
30 cur.execute("""CREATE TABLE user_logs 36 cur.execute("""CREATE TABLE user_loggings
31 (id INTEGER PRIMARY KEY AUTOINCREMENT, 37 (id INTEGER PRIMARY KEY AUTOINCREMENT,
32 user_id INTEGER, 38 user_id INTEGER,
33 last_action TEXT, 39 repository TEXT,
34 last_action_date DATETIME)""") 40 action TEXT,
41 action_date DATETIME)""")
35 conn.commit() 42 conn.commit()
36 except: 43 except:
37 conn.rollback() 44 conn.rollback()
38 raise 45 raise
39 46
40 cur.close() 47 cur.close()
48
49 def admin_prompt():
50 import getpass
51 username = raw_input('give username:')
52 password = getpass.getpass('Specify admin password:')
53 create_user(username, password, True)
41 54
42 def create_user(username, password, admin=False): 55 def create_user(username, password, admin=False):
43 conn, cur = get_sqlite_conn_cur() 56 conn, cur = get_sqlite_conn_cur()
44 password_crypt = crypt.crypt(password, '6a') 57 password_crypt = crypt.crypt(password, '6a')
45 log.info('creating user %s', username) 58 logging.info('creating user %s', username)
46 try: 59 try:
47 cur.execute("""INSERT INTO users values (?,?,?,?,?) """, 60 cur.execute("""INSERT INTO users values (?,?,?,?,?) """,
48 (None, username, password_crypt, 1, admin)) 61 (None, username, password_crypt, 1, admin))
49 conn.commit() 62 conn.commit()
50 except: 63 except:
51 conn.rollback() 64 conn.rollback()
52 raise 65 raise
66
67 if __name__ == '__main__':
68 create_tables()
69 admin_prompt()
70
71