Mercurial > public > src > rhodecode
comparison pylons_app/lib/db_manage.py @ 234:a0116e944da1
changed naming convention for db modules.
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Thu, 27 May 2010 21:31:30 +0200 |
parents | 69a29242ba61 |
children | b18f89d6d17f |
comparison
equal
deleted
inserted
replaced
233:08e0347351d0 | 234:a0116e944da1 |
---|---|
1 import logging | 1 import logging |
2 from os.path import dirname as dn | 2 from os.path import dirname as dn |
3 from os.path import join as jn | |
3 from sqlalchemy.engine import create_engine | 4 from sqlalchemy.engine import create_engine |
4 import os | 5 import os |
5 import sys | 6 import sys |
6 ROOT = dn(dn(dn(os.path.realpath(__file__)))) | 7 ROOT = dn(dn(dn(os.path.realpath(__file__)))) |
7 sys.path.append(ROOT) | 8 sys.path.append(ROOT) |
8 | 9 |
9 from pylons_app.model.db import Users | 10 from pylons_app.model.db import User |
10 from pylons_app.model.meta import Session, Base | 11 from pylons_app.model.meta import Session, Base |
11 | 12 |
12 from pylons_app.lib.auth import get_crypt_password | 13 from pylons_app.lib.auth import get_crypt_password |
13 from pylons_app.model import init_model | 14 from pylons_app.model import init_model |
14 | 15 |
20 log.addHandler(console_handler) | 21 log.addHandler(console_handler) |
21 | 22 |
22 class DbManage(object): | 23 class DbManage(object): |
23 def __init__(self, log_sql): | 24 def __init__(self, log_sql): |
24 self.dbname = 'hg_app.db' | 25 self.dbname = 'hg_app.db' |
25 dburi = 'sqlite:////%s' % os.path.join(ROOT, self.dbname) | 26 dburi = 'sqlite:////%s' % jn(ROOT, self.dbname) |
26 engine = create_engine(dburi, echo=log_sql) | 27 engine = create_engine(dburi, echo=log_sql) |
27 init_model(engine) | 28 init_model(engine) |
28 self.sa = Session() | 29 self.sa = Session() |
29 | 30 |
30 def check_for_db(self, override): | 31 def check_for_db(self, override): |
31 log.info('checking for exisiting db') | 32 log.info('checking for exisiting db') |
32 if os.path.isfile(os.path.join(ROOT, self.dbname)): | 33 if os.path.isfile(jn(ROOT, self.dbname)): |
33 log.info('database exisist') | 34 log.info('database exisist') |
34 if not override: | 35 if not override: |
35 raise Exception('database already exists') | 36 raise Exception('database already exists') |
36 | 37 |
37 def create_tables(self, override=False): | 38 def create_tables(self, override=False): |
39 Create a auth database | 40 Create a auth database |
40 """ | 41 """ |
41 self.check_for_db(override) | 42 self.check_for_db(override) |
42 if override: | 43 if override: |
43 log.info("database exisist and it's going to be destroyed") | 44 log.info("database exisist and it's going to be destroyed") |
45 os.remove(jn(ROOT, self.dbname)) | |
44 Base.metadata.create_all(checkfirst=override) | 46 Base.metadata.create_all(checkfirst=override) |
45 log.info('Created tables for %s', self.dbname) | 47 log.info('Created tables for %s', self.dbname) |
46 | 48 |
47 def admin_prompt(self): | 49 def admin_prompt(self): |
48 import getpass | 50 import getpass |
51 self.create_user(username, password, True) | 53 self.create_user(username, password, True) |
52 | 54 |
53 def create_user(self, username, password, admin=False): | 55 def create_user(self, username, password, admin=False): |
54 log.info('creating administrator user %s', username) | 56 log.info('creating administrator user %s', username) |
55 | 57 |
56 new_user = Users() | 58 new_user = User() |
57 new_user.username = username | 59 new_user.username = username |
58 new_user.password = get_crypt_password(password) | 60 new_user.password = get_crypt_password(password) |
59 new_user.admin = admin | 61 new_user.admin = admin |
60 new_user.active = True | 62 new_user.active = True |
61 | 63 |