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