Show More
@@ -1,63 +1,67 b'' | |||
|
1 | 1 | import logging |
|
2 | 2 | from os.path import dirname as dn |
|
3 | 3 | from sqlalchemy.engine import create_engine |
|
4 | 4 | import os |
|
5 | import sys | |
|
6 | ROOT = dn(dn(dn(os.path.realpath(__file__)))) | |
|
7 | sys.path.append(ROOT) | |
|
8 | ||
|
5 | 9 | from pylons_app.model.db import Users |
|
6 | 10 | from pylons_app.model.meta import Session |
|
7 | 11 | |
|
8 | 12 | from pylons_app.lib.auth import get_crypt_password |
|
9 | 13 | from pylons_app.model import init_model |
|
10 | 14 | |
|
11 | ROOT = dn(dn(dn(os.path.realpath(__file__)))) | |
|
15 | ||
|
12 | 16 | logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)03d %(levelname)-5.5s [%(name)s] %(message)s') |
|
13 | 17 | from pylons_app.model.meta import Base |
|
14 | 18 | |
|
15 | 19 | class DbManage(object): |
|
16 | 20 | def __init__(self): |
|
17 | 21 | dburi = 'sqlite:////%s' % os.path.join(ROOT, 'hg_app.db') |
|
18 | 22 | engine = create_engine(dburi) |
|
19 | 23 | init_model(engine) |
|
20 | 24 | self.sa = Session() |
|
21 | 25 | |
|
22 | 26 | def check_for_db(self, override): |
|
23 | 27 | if not override: |
|
24 | 28 | if os.path.isfile(os.path.join(ROOT, 'hg_app.db')): |
|
25 | 29 | raise Exception('database already exists') |
|
26 | 30 | |
|
27 | 31 | def create_tables(self, override=False): |
|
28 | 32 | """ |
|
29 | 33 | Create a auth database |
|
30 | 34 | """ |
|
31 | 35 | self.check_for_db(override) |
|
32 | 36 | |
|
33 | 37 | Base.metadata.create_all(checkfirst=override) |
|
34 | 38 | logging.info('Created tables') |
|
35 | 39 | |
|
36 | 40 | def admin_prompt(self): |
|
37 | 41 | import getpass |
|
38 | 42 | username = raw_input('give admin username:') |
|
39 | 43 | password = getpass.getpass('Specify admin password:') |
|
40 | 44 | self.create_user(username, password, True) |
|
41 | 45 | |
|
42 | 46 | def create_user(self, username, password, admin=False): |
|
43 | 47 | logging.info('creating user %s', username) |
|
44 | 48 | |
|
45 | 49 | new_user = Users() |
|
46 | 50 | new_user.username = username |
|
47 | 51 | new_user.password = get_crypt_password(password) |
|
48 | 52 | new_user.admin = admin |
|
49 | 53 | new_user.active = True |
|
50 | 54 | |
|
51 | 55 | try: |
|
52 | 56 | self.sa.add(new_user) |
|
53 | 57 | self.sa.commit() |
|
54 | 58 | except: |
|
55 | 59 | self.sa.rollback() |
|
56 | 60 | raise |
|
57 | 61 | |
|
58 | 62 | if __name__ == '__main__': |
|
59 | 63 | dbmanage = DbManage() |
|
60 | 64 | dbmanage.create_tables(override=True) |
|
61 | 65 | dbmanage.admin_prompt() |
|
62 | 66 | |
|
63 | 67 |
General Comments 0
You need to be logged in to leave comments.
Login now