##// END OF EJS Templates
fixed import errors on db_manage
marcink -
r249:bad9ccac default
parent child Browse files
Show More
@@ -1,98 +1,97
1 1 from os.path import dirname as dn, join as jn
2 import os
3 import sys
4 ROOT = dn(dn(dn(os.path.realpath(__file__))))
5 sys.path.append(ROOT)
6
2 7 from pylons_app.lib.auth import get_crypt_password
3 8 from pylons_app.model import init_model
4 9 from pylons_app.model.db import User, Permission
5 10 from pylons_app.model.meta import Session, Base
6 11 from sqlalchemy.engine import create_engine
7 12 import logging
8 import os
9 import sys
10 ROOT = dn(dn(dn(os.path.realpath(__file__))))
11 sys.path.append(ROOT)
12
13
14 13
15 14 log = logging.getLogger('db manage')
16 15 log.setLevel(logging.DEBUG)
17 16 console_handler = logging.StreamHandler()
18 17 console_handler.setFormatter(logging.Formatter("%(asctime)s.%(msecs)03d"
19 18 " %(levelname)-5.5s [%(name)s] %(message)s"))
20 19 log.addHandler(console_handler)
21 20
22 21 class DbManage(object):
23 22 def __init__(self, log_sql):
24 23 self.dbname = 'hg_app.db'
25 24 dburi = 'sqlite:////%s' % jn(ROOT, self.dbname)
26 25 engine = create_engine(dburi, echo=log_sql)
27 26 init_model(engine)
28 27 self.sa = Session()
29 28 self.db_exists = False
30 29
31 30 def check_for_db(self, override):
32 31 log.info('checking for exisiting db')
33 32 if os.path.isfile(jn(ROOT, self.dbname)):
34 33 self.db_exists = True
35 34 log.info('database exisist')
36 35 if not override:
37 36 raise Exception('database already exists')
38 37
39 38 def create_tables(self, override=False):
40 39 """
41 40 Create a auth database
42 41 """
43 42 self.check_for_db(override)
44 43 if override:
45 44 log.info("database exisist and it's going to be destroyed")
46 45 if self.db_exists:
47 46 os.remove(jn(ROOT, self.dbname))
48 47 Base.metadata.create_all(checkfirst=override)
49 48 log.info('Created tables for %s', self.dbname)
50 49
51 50 def admin_prompt(self):
52 51 import getpass
53 52 username = raw_input('Specify admin username:')
54 53 password = getpass.getpass('Specify admin password:')
55 54 self.create_user(username, password, True)
56 55
57 56 def create_user(self, username, password, admin=False):
58 57 log.info('creating administrator user %s', username)
59 58
60 59 new_user = User()
61 60 new_user.username = username
62 61 new_user.password = get_crypt_password(password)
63 62 new_user.admin = admin
64 63 new_user.active = True
65 64
66 65 try:
67 66 self.sa.add(new_user)
68 67 self.sa.commit()
69 68 except:
70 69 self.sa.rollback()
71 70 raise
72 71
73 72 def create_permissions(self):
74 73 #module.(access|create|change|delete)_[name]
75 74 perms = [('admin.access_home', 'Access to admin user view'),
76 75
77 76 ]
78 77
79 78 for p in perms:
80 79 new_perm = Permission()
81 80 new_perm.permission_name = p[0]
82 81 new_perm.permission_longname = p[1]
83 82 try:
84 83 self.sa.add(new_perm)
85 84 self.sa.commit()
86 85 except:
87 86 self.sa.rollback()
88 87 raise
89 88
90 89
91 90
92 91 if __name__ == '__main__':
93 92 dbmanage = DbManage(log_sql=True)
94 93 dbmanage.create_tables(override=True)
95 94 dbmanage.admin_prompt()
96 95 dbmanage.create_permissions()
97 96
98 97
General Comments 0
You need to be logged in to leave comments. Login now