comparison pylons_app/lib/db_manage.py @ 226:c6526b7531e9

rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 26 May 2010 23:55:20 +0200
parents 911dab498eb2
children c49ebe560af2
comparison
equal deleted inserted replaced
225:710e7a75bb6b 226:c6526b7531e9
1 import logging 1 import logging
2 import sqlite3 2 from os.path import dirname as dn
3 from sqlalchemy.engine import create_engine
4 import os
5 from pylons_app.model.db import Users
6 from pylons_app.model.meta import Session
3 7
4 import os 8 from pylons_app.lib.auth import get_crypt_password
5 import crypt 9 from pylons_app.model import init_model
6 from os.path import dirname as dn 10
7 ROOT = dn(dn(dn(os.path.realpath(__file__)))) 11 ROOT = dn(dn(dn(os.path.realpath(__file__))))
8 logging.basicConfig(level=logging.DEBUG) 12 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)03d %(levelname)-5.5s [%(name)s] %(message)s')
13 from pylons_app.model.meta import Base
9 14
10 def get_sqlite_conn_cur(): 15 class DbManage(object):
11 conn = sqlite3.connect(os.path.join(ROOT, 'hg_app.db')) 16 def __init__(self):
12 cur = conn.cursor() 17 dburi = 'sqlite:////%s' % os.path.join(ROOT, 'hg_app.db')
13 return conn, cur 18 engine = create_engine(dburi)
14 19 init_model(engine)
15 def check_for_db(override): 20 self.sa = Session()
16 if not override:
17 if os.path.isfile(os.path.join(ROOT, 'hg_app.db')):
18 raise Exception('database already exists')
19
20 def create_tables(override=False):
21 """
22 Create a auth database
23 """
24 check_for_db(override)
25 conn, cur = get_sqlite_conn_cur()
26 try:
27 logging.info('creating table %s', 'users')
28 cur.execute("""DROP TABLE IF EXISTS users """)
29 cur.execute("""CREATE TABLE users
30 (user_id INTEGER PRIMARY KEY AUTOINCREMENT,
31 username TEXT,
32 password TEXT,
33 active INTEGER,
34 admin INTEGER)""")
35 logging.info('creating table %s', 'user_logs')
36 cur.execute("""DROP TABLE IF EXISTS user_logs """)
37 cur.execute("""CREATE TABLE user_logs
38 (id INTEGER PRIMARY KEY AUTOINCREMENT,
39 user_id INTEGER,
40 repository TEXT,
41 action TEXT,
42 action_date DATETIME)""")
43 conn.commit()
44 except:
45 conn.rollback()
46 raise
47 21
48 cur.close() 22 def check_for_db(self, override):
49 23 if not override:
50 def admin_prompt(): 24 if os.path.isfile(os.path.join(ROOT, 'hg_app.db')):
51 import getpass 25 raise Exception('database already exists')
52 username = raw_input('give username:')
53 password = getpass.getpass('Specify admin password:')
54 create_user(username, password, True)
55 26
56 def create_user(username, password, admin=False): 27 def create_tables(self, override=False):
57 conn, cur = get_sqlite_conn_cur() 28 """
58 password_crypt = crypt.crypt(password, '6a') 29 Create a auth database
59 logging.info('creating user %s', username) 30 """
60 try: 31 self.check_for_db(override)
61 cur.execute("""INSERT INTO users values (?,?,?,?,?) """, 32
62 (None, username, password_crypt, 1, admin)) 33 Base.metadata.create_all(checkfirst=override)
63 conn.commit() 34 logging.info('Created tables')
64 except: 35
65 conn.rollback() 36 def admin_prompt(self):
66 raise 37 import getpass
38 username = raw_input('give admin username:')
39 password = getpass.getpass('Specify admin password:')
40 self.create_user(username, password, True)
41
42 def create_user(self, username, password, admin=False):
43 logging.info('creating user %s', username)
44
45 new_user = Users()
46 new_user.username = username
47 new_user.password = get_crypt_password(password)
48 new_user.admin = admin
49 new_user.active = True
50
51 try:
52 self.sa.add(new_user)
53 self.sa.commit()
54 except:
55 self.sa.rollback()
56 raise
67 57
68 if __name__ == '__main__': 58 if __name__ == '__main__':
69 create_tables(True) 59 dbmanage = DbManage()
70 admin_prompt() 60 dbmanage.create_tables(override=True)
61 dbmanage.admin_prompt()
71 62
72 63