##// END OF EJS Templates
rewritten db manage script to use sqlalchemy. Fixed sqlalchemy models to more generic.
marcink -
r226:c6526b75 default
parent child Browse files
Show More
@@ -1,72 +1,63 b''
1 import logging
1 import logging
2 import sqlite3
2 from os.path import dirname as dn
3 from sqlalchemy.engine import create_engine
4 import os
5 from pylons_app.model.db import Users
6 from pylons_app.model.meta import Session
3
7
4 import os
8 from pylons_app.lib.auth import get_crypt_password
5 import crypt
9 from pylons_app.model import init_model
6 from os.path import dirname as dn
10
7 ROOT = dn(dn(dn(os.path.realpath(__file__))))
11 ROOT = dn(dn(dn(os.path.realpath(__file__))))
8 logging.basicConfig(level=logging.DEBUG)
12 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)03d %(levelname)-5.5s [%(name)s] %(message)s')
13 from pylons_app.model.meta import Base
9
14
10 def get_sqlite_conn_cur():
15 class DbManage(object):
11 conn = sqlite3.connect(os.path.join(ROOT, 'hg_app.db'))
16 def __init__(self):
12 cur = conn.cursor()
17 dburi = 'sqlite:////%s' % os.path.join(ROOT, 'hg_app.db')
13 return conn, cur
18 engine = create_engine(dburi)
19 init_model(engine)
20 self.sa = Session()
14
21
15 def check_for_db(override):
22 def check_for_db(self, override):
16 if not override:
23 if not override:
17 if os.path.isfile(os.path.join(ROOT, 'hg_app.db')):
24 if os.path.isfile(os.path.join(ROOT, 'hg_app.db')):
18 raise Exception('database already exists')
25 raise Exception('database already exists')
19
26
20 def create_tables(override=False):
27 def create_tables(self, override=False):
21 """
28 """
22 Create a auth database
29 Create a auth database
23 """
30 """
24 check_for_db(override)
31 self.check_for_db(override)
25 conn, cur = get_sqlite_conn_cur()
32
33 Base.metadata.create_all(checkfirst=override)
34 logging.info('Created tables')
35
36 def admin_prompt(self):
37 import getpass
38 username = raw_input('give admin username:')
39 password = getpass.getpass('Specify admin password:')
40 self.create_user(username, password, True)
41
42 def create_user(self, username, password, admin=False):
43 logging.info('creating user %s', username)
44
45 new_user = Users()
46 new_user.username = username
47 new_user.password = get_crypt_password(password)
48 new_user.admin = admin
49 new_user.active = True
50
26 try:
51 try:
27 logging.info('creating table %s', 'users')
52 self.sa.add(new_user)
28 cur.execute("""DROP TABLE IF EXISTS users """)
53 self.sa.commit()
29 cur.execute("""CREATE TABLE users
30 (user_id INTEGER PRIMARY KEY AUTOINCREMENT,
31 username TEXT,
32 password TEXT,
33 active INTEGER,
34 admin INTEGER)""")
35 logging.info('creating table %s', 'user_logs')
36 cur.execute("""DROP TABLE IF EXISTS user_logs """)
37 cur.execute("""CREATE TABLE user_logs
38 (id INTEGER PRIMARY KEY AUTOINCREMENT,
39 user_id INTEGER,
40 repository TEXT,
41 action TEXT,
42 action_date DATETIME)""")
43 conn.commit()
44 except:
54 except:
45 conn.rollback()
55 self.sa.rollback()
46 raise
47
48 cur.close()
49
50 def admin_prompt():
51 import getpass
52 username = raw_input('give username:')
53 password = getpass.getpass('Specify admin password:')
54 create_user(username, password, True)
55
56 def create_user(username, password, admin=False):
57 conn, cur = get_sqlite_conn_cur()
58 password_crypt = crypt.crypt(password, '6a')
59 logging.info('creating user %s', username)
60 try:
61 cur.execute("""INSERT INTO users values (?,?,?,?,?) """,
62 (None, username, password_crypt, 1, admin))
63 conn.commit()
64 except:
65 conn.rollback()
66 raise
56 raise
67
57
68 if __name__ == '__main__':
58 if __name__ == '__main__':
69 create_tables(True)
59 dbmanage = DbManage()
70 admin_prompt()
60 dbmanage.create_tables(override=True)
61 dbmanage.admin_prompt()
71
62
72
63
@@ -1,13 +1,6 b''
1 import sqlalchemy
2 from pylons_app.model.meta import Base
1 from pylons_app.model.meta import Base
3 from sqlalchemy import ForeignKey, Column
4 from sqlalchemy.orm import relation, backref
2 from sqlalchemy.orm import relation, backref
5
3 from sqlalchemy import *
6 if sqlalchemy.__version__ == '0.6.0':
7 from sqlalchemy.dialects.sqlite import *
8 else:
9 from sqlalchemy.databases.sqlite import SLBoolean as BOOLEAN, \
10 SLInteger as INTEGER, SLText as TEXT, SLDateTime as DATETIME
11
4
12 class Users(Base):
5 class Users(Base):
13 __tablename__ = 'users'
6 __tablename__ = 'users'
@@ -17,14 +10,27 b' class Users(Base):'
17 password = Column("password", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
10 password = Column("password", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
18 active = Column("active", BOOLEAN(), nullable=True, unique=None, default=None)
11 active = Column("active", BOOLEAN(), nullable=True, unique=None, default=None)
19 admin = Column("admin", BOOLEAN(), nullable=True, unique=None, default=None)
12 admin = Column("admin", BOOLEAN(), nullable=True, unique=None, default=None)
20 action_log = relation('UserLogs')
13 name = Column("name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
14 lastname = Column("lastname", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
15 email = Column("email", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
16 last_login = Column("last_login", DATETIME(timezone=False), nullable=True, unique=None, default=None)
17
18 user_log = relation('UserLogs')
21
19
22 class UserLogs(Base):
20 class UserLogs(Base):
23 __tablename__ = 'user_logs'
21 __tablename__ = 'user_logs'
24 __table_args__ = {'useexisting':True}
22 __table_args__ = {'useexisting':True}
25 id = Column("id", INTEGER(), nullable=False, unique=True, default=None, primary_key=1)
23 user_log_id = Column("id", INTEGER(), nullable=False, unique=True, default=None, primary_key=1)
26 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=True, unique=None, default=None)
24 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=True, unique=None, default=None)
27 repository = Column("repository", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
25 repository = Column("repository", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
28 action = Column("action", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
26 action = Column("action", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
29 action_date = Column("action_date", DATETIME(timezone=False), nullable=True, unique=None, default=None)
27 action_date = Column("action_date", DATETIME(timezone=False), nullable=True, unique=None, default=None)
28
30 user = relation('Users')
29 user = relation('Users')
30
31
32 class Permissions(Base):
33 __tablename__ = 'permissions'
34 __table_args__ = {'useexisting':True}
35 permission_id = Column("id", INTEGER(), nullable=False, unique=True, default=None, primary_key=1)
36 permission_name = Column("permission_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
General Comments 0
You need to be logged in to leave comments. Login now