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