Show More
@@ -1,126 +1,126 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # encoding: utf-8 |
|
3 | 3 | # database managment for hg app |
|
4 | 4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
|
5 | 5 | |
|
6 | 6 | # This program is free software; you can redistribute it and/or |
|
7 | 7 | # modify it under the terms of the GNU General Public License |
|
8 | 8 | # as published by the Free Software Foundation; version 2 |
|
9 | 9 | # of the License or (at your opinion) any later version of the license. |
|
10 | 10 | # |
|
11 | 11 | # This program is distributed in the hope that it will be useful, |
|
12 | 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 | 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 14 | # GNU General Public License for more details. |
|
15 | 15 | # |
|
16 | 16 | # You should have received a copy of the GNU General Public License |
|
17 | 17 | # along with this program; if not, write to the Free Software |
|
18 | 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 | 19 | # MA 02110-1301, USA. |
|
20 | 20 | |
|
21 | 21 | """ |
|
22 | 22 | Created on April 10, 2010 |
|
23 | 23 | database managment and creation for hg app |
|
24 | 24 | @author: marcink |
|
25 | 25 | """ |
|
26 | 26 | |
|
27 | 27 | from os.path import dirname as dn, join as jn |
|
28 | 28 | import os |
|
29 | 29 | import sys |
|
30 | 30 | ROOT = dn(dn(dn(os.path.realpath(__file__)))) |
|
31 | 31 | sys.path.append(ROOT) |
|
32 | 32 | |
|
33 | 33 | from pylons_app.lib.auth import get_crypt_password |
|
34 | 34 | from pylons_app.model import init_model |
|
35 | 35 | from pylons_app.model.db import User, Permission |
|
36 | 36 | from pylons_app.model.meta import Session, Base |
|
37 | 37 | from sqlalchemy.engine import create_engine |
|
38 | 38 | import logging |
|
39 | 39 | |
|
40 | 40 | log = logging.getLogger('db manage') |
|
41 | 41 | log.setLevel(logging.DEBUG) |
|
42 | 42 | console_handler = logging.StreamHandler() |
|
43 | 43 | console_handler.setFormatter(logging.Formatter("%(asctime)s.%(msecs)03d" |
|
44 | 44 | " %(levelname)-5.5s [%(name)s] %(message)s")) |
|
45 | 45 | log.addHandler(console_handler) |
|
46 | 46 | |
|
47 | 47 | class DbManage(object): |
|
48 | 48 | def __init__(self, log_sql): |
|
49 | 49 | self.dbname = 'hg_app.db' |
|
50 | 50 | dburi = 'sqlite:////%s' % jn(ROOT, self.dbname) |
|
51 | 51 | engine = create_engine(dburi, echo=log_sql) |
|
52 | 52 | init_model(engine) |
|
53 | 53 | self.sa = Session() |
|
54 | 54 | self.db_exists = False |
|
55 | 55 | |
|
56 | 56 | def check_for_db(self, override): |
|
57 | 57 | log.info('checking for exisiting db') |
|
58 | 58 | if os.path.isfile(jn(ROOT, self.dbname)): |
|
59 | 59 | self.db_exists = True |
|
60 | 60 | log.info('database exisist') |
|
61 | 61 | if not override: |
|
62 | 62 | raise Exception('database already exists') |
|
63 | 63 | |
|
64 | 64 | def create_tables(self, override=False): |
|
65 | 65 | """ |
|
66 | 66 | Create a auth database |
|
67 | 67 | """ |
|
68 | 68 | self.check_for_db(override) |
|
69 | 69 | if override: |
|
70 | 70 | log.info("database exisist and it's going to be destroyed") |
|
71 | 71 | if self.db_exists: |
|
72 | 72 | os.remove(jn(ROOT, self.dbname)) |
|
73 | 73 | Base.metadata.create_all(checkfirst=override) |
|
74 | 74 | log.info('Created tables for %s', self.dbname) |
|
75 | 75 | |
|
76 | 76 | def admin_prompt(self): |
|
77 | 77 | import getpass |
|
78 | 78 | username = raw_input('Specify admin username:') |
|
79 | 79 | password = getpass.getpass('Specify admin password:') |
|
80 | 80 | self.create_user(username, password, True) |
|
81 | 81 | |
|
82 | 82 | def create_user(self, username, password, admin=False): |
|
83 | 83 | log.info('creating administrator user %s', username) |
|
84 | 84 | |
|
85 | 85 | new_user = User() |
|
86 | 86 | new_user.username = username |
|
87 | 87 | new_user.password = get_crypt_password(password) |
|
88 |
new_user. |
|
|
88 | new_user.name = 'Admin' | |
|
89 | 89 | new_user.lastname = 'Admin' |
|
90 | 90 | new_user.email = 'admin@localhost' |
|
91 | 91 | new_user.admin = admin |
|
92 | 92 | new_user.active = True |
|
93 | 93 | |
|
94 | 94 | try: |
|
95 | 95 | self.sa.add(new_user) |
|
96 | 96 | self.sa.commit() |
|
97 | 97 | except: |
|
98 | 98 | self.sa.rollback() |
|
99 | 99 | raise |
|
100 | 100 | |
|
101 | 101 | def create_permissions(self): |
|
102 | 102 | #module.(access|create|change|delete)_[name] |
|
103 | 103 | perms = [('admin.access_home', 'Access to admin user view'), |
|
104 | 104 | |
|
105 | 105 | ] |
|
106 | 106 | |
|
107 | 107 | for p in perms: |
|
108 | 108 | new_perm = Permission() |
|
109 | 109 | new_perm.permission_name = p[0] |
|
110 | 110 | new_perm.permission_longname = p[1] |
|
111 | 111 | try: |
|
112 | 112 | self.sa.add(new_perm) |
|
113 | 113 | self.sa.commit() |
|
114 | 114 | except: |
|
115 | 115 | self.sa.rollback() |
|
116 | 116 | raise |
|
117 | 117 | |
|
118 | 118 | |
|
119 | 119 | |
|
120 | 120 | if __name__ == '__main__': |
|
121 | 121 | dbmanage = DbManage(log_sql=True) |
|
122 | 122 | dbmanage.create_tables(override=True) |
|
123 | 123 | dbmanage.admin_prompt() |
|
124 | 124 | dbmanage.create_permissions() |
|
125 | 125 | |
|
126 | 126 |
General Comments 0
You need to be logged in to leave comments.
Login now