annotate pylons_app/lib/db_manage.py @ 243:9d64df490248

fixed bug when there was no dbfile, and dbmanage raise an exception
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 01 Jun 2010 22:06:48 +0200
parents 7c4fa2a66195
children bad9ccac26b7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
239
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
1 from os.path import dirname as dn, join as jn
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
2 from pylons_app.lib.auth import get_crypt_password
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
3 from pylons_app.model import init_model
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
4 from pylons_app.model.db import User, Permission
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
5 from pylons_app.model.meta import Session, Base
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
6 from sqlalchemy.engine import create_engine
59
736078908f37 added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
7 import logging
736078908f37 added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
8 import os
228
c49ebe560af2 fixed dbmanage script to work as standalone script
Marcin Kuzminski <marcin@python-works.com>
parents: 226
diff changeset
9 import sys
c49ebe560af2 fixed dbmanage script to work as standalone script
Marcin Kuzminski <marcin@python-works.com>
parents: 226
diff changeset
10 ROOT = dn(dn(dn(os.path.realpath(__file__))))
c49ebe560af2 fixed dbmanage script to work as standalone script
Marcin Kuzminski <marcin@python-works.com>
parents: 226
diff changeset
11 sys.path.append(ROOT)
c49ebe560af2 fixed dbmanage script to work as standalone script
Marcin Kuzminski <marcin@python-works.com>
parents: 226
diff changeset
12
59
736078908f37 added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
13
736078908f37 added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
14
229
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
15 log = logging.getLogger('db manage')
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
16 log.setLevel(logging.DEBUG)
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
17 console_handler = logging.StreamHandler()
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
18 console_handler.setFormatter(logging.Formatter("%(asctime)s.%(msecs)03d"
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
19 " %(levelname)-5.5s [%(name)s] %(message)s"))
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
20 log.addHandler(console_handler)
66
163464441e0d updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents: 59
diff changeset
21
226
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
22 class DbManage(object):
229
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
23 def __init__(self, log_sql):
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
24 self.dbname = 'hg_app.db'
234
a0116e944da1 changed naming convention for db modules.
Marcin Kuzminski <marcin@python-works.com>
parents: 229
diff changeset
25 dburi = 'sqlite:////%s' % jn(ROOT, self.dbname)
229
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
26 engine = create_engine(dburi, echo=log_sql)
226
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
27 init_model(engine)
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
28 self.sa = Session()
243
9d64df490248 fixed bug when there was no dbfile, and dbmanage raise an exception
Marcin Kuzminski <marcin@python-works.com>
parents: 240
diff changeset
29 self.db_exists = False
226
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
30
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
31 def check_for_db(self, override):
229
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
32 log.info('checking for exisiting db')
234
a0116e944da1 changed naming convention for db modules.
Marcin Kuzminski <marcin@python-works.com>
parents: 229
diff changeset
33 if os.path.isfile(jn(ROOT, self.dbname)):
243
9d64df490248 fixed bug when there was no dbfile, and dbmanage raise an exception
Marcin Kuzminski <marcin@python-works.com>
parents: 240
diff changeset
34 self.db_exists = True
229
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
35 log.info('database exisist')
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
36 if not override:
226
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
37 raise Exception('database already exists')
229
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
38
226
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
39 def create_tables(self, override=False):
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
40 """
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
41 Create a auth database
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
42 """
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
43 self.check_for_db(override)
229
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
44 if override:
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
45 log.info("database exisist and it's going to be destroyed")
243
9d64df490248 fixed bug when there was no dbfile, and dbmanage raise an exception
Marcin Kuzminski <marcin@python-works.com>
parents: 240
diff changeset
46 if self.db_exists:
9d64df490248 fixed bug when there was no dbfile, and dbmanage raise an exception
Marcin Kuzminski <marcin@python-works.com>
parents: 240
diff changeset
47 os.remove(jn(ROOT, self.dbname))
226
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
48 Base.metadata.create_all(checkfirst=override)
229
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
49 log.info('Created tables for %s', self.dbname)
59
736078908f37 added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
50
226
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
51 def admin_prompt(self):
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
52 import getpass
229
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
53 username = raw_input('Specify admin username:')
226
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
54 password = getpass.getpass('Specify admin password:')
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
55 self.create_user(username, password, True)
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
56
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
57 def create_user(self, username, password, admin=False):
229
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
58 log.info('creating administrator user %s', username)
226
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
59
234
a0116e944da1 changed naming convention for db modules.
Marcin Kuzminski <marcin@python-works.com>
parents: 229
diff changeset
60 new_user = User()
226
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
61 new_user.username = username
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
62 new_user.password = get_crypt_password(password)
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
63 new_user.admin = admin
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
64 new_user.active = True
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
65
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
66 try:
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
67 self.sa.add(new_user)
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
68 self.sa.commit()
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
69 except:
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
70 self.sa.rollback()
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
71 raise
66
163464441e0d updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents: 59
diff changeset
72
239
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
73 def create_permissions(self):
240
7c4fa2a66195 small fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 239
diff changeset
74 #module.(access|create|change|delete)_[name]
7c4fa2a66195 small fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 239
diff changeset
75 perms = [('admin.access_home', 'Access to admin user view'),
239
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
76
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
77 ]
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
78
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
79 for p in perms:
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
80 new_perm = Permission()
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
81 new_perm.permission_name = p[0]
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
82 new_perm.permission_longname = p[1]
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
83 try:
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
84 self.sa.add(new_perm)
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
85 self.sa.commit()
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
86 except:
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
87 self.sa.rollback()
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
88 raise
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
89
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
90
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
91
66
163464441e0d updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents: 59
diff changeset
92 if __name__ == '__main__':
229
69a29242ba61 db manage added more logging, set custom logger and add optional print sql statments
Marcin Kuzminski <marcin@python-works.com>
parents: 228
diff changeset
93 dbmanage = DbManage(log_sql=True)
226
c6526b7531e9 rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
Marcin Kuzminski <marcin@python-works.com>
parents: 88
diff changeset
94 dbmanage.create_tables(override=True)
239
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
95 dbmanage.admin_prompt()
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
96 dbmanage.create_permissions()
66
163464441e0d updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents: 59
diff changeset
97
163464441e0d updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents: 59
diff changeset
98