##// END OF EJS Templates
Updated db manage
marcink -
r88:911dab49 default
parent child Browse files
Show More
@@ -1,71 +1,72
1 import logging
1 import logging
2 import sqlite3
2 import sqlite3
3
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 logging.basicConfig(level=logging.DEBUG)
9
9
10 def get_sqlite_conn_cur():
10 def get_sqlite_conn_cur():
11 conn = sqlite3.connect(os.path.join(ROOT, 'hg_app.db'))
11 conn = sqlite3.connect(os.path.join(ROOT, 'hg_app.db'))
12 cur = conn.cursor()
12 cur = conn.cursor()
13 return conn, cur
13 return conn, cur
14
14
15 def check_for_db():
15 def check_for_db(override):
16 if os.path.isfile(os.path.join(ROOT, 'hg_app.db')):
16 if not override:
17 raise Exception('database already exists')
17 if os.path.isfile(os.path.join(ROOT, 'hg_app.db')):
18 raise Exception('database already exists')
18
19
19 def create_tables():
20 def create_tables(override=False):
20 """
21 """
21 Create a auth database
22 Create a auth database
22 """
23 """
23 check_for_db()
24 check_for_db(override)
24 conn, cur = get_sqlite_conn_cur()
25 conn, cur = get_sqlite_conn_cur()
25 try:
26 try:
26 logging.info('creating table %s', 'users')
27 logging.info('creating table %s', 'users')
27 cur.execute("""DROP TABLE IF EXISTS users """)
28 cur.execute("""DROP TABLE IF EXISTS users """)
28 cur.execute("""CREATE TABLE users
29 cur.execute("""CREATE TABLE users
29 (user_id INTEGER PRIMARY KEY AUTOINCREMENT,
30 (user_id INTEGER PRIMARY KEY AUTOINCREMENT,
30 username TEXT,
31 username TEXT,
31 password TEXT,
32 password TEXT,
32 active INTEGER,
33 active INTEGER,
33 admin INTEGER)""")
34 admin INTEGER)""")
34 logging.info('creating table %s', 'user_logs')
35 logging.info('creating table %s', 'user_logs')
35 cur.execute("""DROP TABLE IF EXISTS user_logs """)
36 cur.execute("""DROP TABLE IF EXISTS user_logs """)
36 cur.execute("""CREATE TABLE user_logs
37 cur.execute("""CREATE TABLE user_logs
37 (id INTEGER PRIMARY KEY AUTOINCREMENT,
38 (id INTEGER PRIMARY KEY AUTOINCREMENT,
38 user_id INTEGER,
39 user_id INTEGER,
39 repository TEXT,
40 repository TEXT,
40 action TEXT,
41 action TEXT,
41 action_date DATETIME)""")
42 action_date DATETIME)""")
42 conn.commit()
43 conn.commit()
43 except:
44 except:
44 conn.rollback()
45 conn.rollback()
45 raise
46 raise
46
47
47 cur.close()
48 cur.close()
48
49
49 def admin_prompt():
50 def admin_prompt():
50 import getpass
51 import getpass
51 username = raw_input('give username:')
52 username = raw_input('give username:')
52 password = getpass.getpass('Specify admin password:')
53 password = getpass.getpass('Specify admin password:')
53 create_user(username, password, True)
54 create_user(username, password, True)
54
55
55 def create_user(username, password, admin=False):
56 def create_user(username, password, admin=False):
56 conn, cur = get_sqlite_conn_cur()
57 conn, cur = get_sqlite_conn_cur()
57 password_crypt = crypt.crypt(password, '6a')
58 password_crypt = crypt.crypt(password, '6a')
58 logging.info('creating user %s', username)
59 logging.info('creating user %s', username)
59 try:
60 try:
60 cur.execute("""INSERT INTO users values (?,?,?,?,?) """,
61 cur.execute("""INSERT INTO users values (?,?,?,?,?) """,
61 (None, username, password_crypt, 1, admin))
62 (None, username, password_crypt, 1, admin))
62 conn.commit()
63 conn.commit()
63 except:
64 except:
64 conn.rollback()
65 conn.rollback()
65 raise
66 raise
66
67
67 if __name__ == '__main__':
68 if __name__ == '__main__':
68 create_tables()
69 create_tables(True)
69 admin_prompt()
70 admin_prompt()
70
71
71
72
General Comments 0
You need to be logged in to leave comments. Login now