##// 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
3 from sqlalchemy.engine import create_engine
4 import os
4 import os
5 import crypt
5 from pylons_app.model.db import Users
6 from os.path import dirname as dn
6 from pylons_app.model.meta import Session
7 ROOT = dn(dn(dn(os.path.realpath(__file__))))
8 logging.basicConfig(level=logging.DEBUG)
9
7
10 def get_sqlite_conn_cur():
8 from pylons_app.lib.auth import get_crypt_password
11 conn = sqlite3.connect(os.path.join(ROOT, 'hg_app.db'))
9 from pylons_app.model import init_model
12 cur = conn.cursor()
13 return conn, cur
14
10
15 def check_for_db(override):
11 ROOT = dn(dn(dn(os.path.realpath(__file__))))
16 if not override:
12 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)03d %(levelname)-5.5s [%(name)s] %(message)s')
17 if os.path.isfile(os.path.join(ROOT, 'hg_app.db')):
13 from pylons_app.model.meta import Base
18 raise Exception('database already exists')
19
14
20 def create_tables(override=False):
15 class DbManage(object):
21 """
16 def __init__(self):
22 Create a auth database
17 dburi = 'sqlite:////%s' % os.path.join(ROOT, 'hg_app.db')
23 """
18 engine = create_engine(dburi)
24 check_for_db(override)
19 init_model(engine)
25 conn, cur = get_sqlite_conn_cur()
20 self.sa = Session()
26 try:
21
27 logging.info('creating table %s', 'users')
22 def check_for_db(self, override):
28 cur.execute("""DROP TABLE IF EXISTS users """)
23 if not override:
29 cur.execute("""CREATE TABLE users
24 if os.path.isfile(os.path.join(ROOT, 'hg_app.db')):
30 (user_id INTEGER PRIMARY KEY AUTOINCREMENT,
25 raise Exception('database already exists')
31 username TEXT,
26
32 password TEXT,
27 def create_tables(self, override=False):
33 active INTEGER,
28 """
34 admin INTEGER)""")
29 Create a auth database
35 logging.info('creating table %s', 'user_logs')
30 """
36 cur.execute("""DROP TABLE IF EXISTS user_logs """)
31 self.check_for_db(override)
37 cur.execute("""CREATE TABLE user_logs
32
38 (id INTEGER PRIMARY KEY AUTOINCREMENT,
33 Base.metadata.create_all(checkfirst=override)
39 user_id INTEGER,
34 logging.info('Created tables')
40 repository TEXT,
41 action TEXT,
42 action_date DATETIME)""")
43 conn.commit()
44 except:
45 conn.rollback()
46 raise
47
35
48 cur.close()
36 def admin_prompt(self):
49
37 import getpass
50 def admin_prompt():
38 username = raw_input('give admin username:')
51 import getpass
39 password = getpass.getpass('Specify admin password:')
52 username = raw_input('give username:')
40 self.create_user(username, password, True)
53 password = getpass.getpass('Specify admin password:')
41
54 create_user(username, password, True)
42 def create_user(self, username, password, admin=False):
55
43 logging.info('creating user %s', username)
56 def create_user(username, password, admin=False):
44
57 conn, cur = get_sqlite_conn_cur()
45 new_user = Users()
58 password_crypt = crypt.crypt(password, '6a')
46 new_user.username = username
59 logging.info('creating user %s', username)
47 new_user.password = get_crypt_password(password)
60 try:
48 new_user.admin = admin
61 cur.execute("""INSERT INTO users values (?,?,?,?,?) """,
49 new_user.active = True
62 (None, username, password_crypt, 1, admin))
50
63 conn.commit()
51 try:
64 except:
52 self.sa.add(new_user)
65 conn.rollback()
53 self.sa.commit()
66 raise
54 except:
55 self.sa.rollback()
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