Show More
@@ -1,72 +1,63 b'' | |||
|
1 | 1 | import logging |
|
2 | import sqlite3 | |
|
3 | ||
|
2 | from os.path import dirname as dn | |
|
3 | from sqlalchemy.engine import create_engine | |
|
4 | 4 | import os |
|
5 | import crypt | |
|
6 | from os.path import dirname as dn | |
|
7 | ROOT = dn(dn(dn(os.path.realpath(__file__)))) | |
|
8 | logging.basicConfig(level=logging.DEBUG) | |
|
5 | from pylons_app.model.db import Users | |
|
6 | from pylons_app.model.meta import Session | |
|
9 | 7 | |
|
10 | def get_sqlite_conn_cur(): | |
|
11 | conn = sqlite3.connect(os.path.join(ROOT, 'hg_app.db')) | |
|
12 | cur = conn.cursor() | |
|
13 | return conn, cur | |
|
8 | from pylons_app.lib.auth import get_crypt_password | |
|
9 | from pylons_app.model import init_model | |
|
14 | 10 | |
|
15 | def check_for_db(override): | |
|
16 | if not override: | |
|
17 | if os.path.isfile(os.path.join(ROOT, 'hg_app.db')): | |
|
18 | raise Exception('database already exists') | |
|
11 | ROOT = dn(dn(dn(os.path.realpath(__file__)))) | |
|
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 | |
|
19 | 14 | |
|
20 | def create_tables(override=False): | |
|
21 | """ | |
|
22 | Create a auth database | |
|
23 | """ | |
|
24 | check_for_db(override) | |
|
25 | conn, cur = get_sqlite_conn_cur() | |
|
26 |
|
|
|
27 | logging.info('creating table %s', 'users') | |
|
28 | cur.execute("""DROP TABLE IF EXISTS users """) | |
|
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: | |
|
45 | conn.rollback() | |
|
46 | raise | |
|
15 | class DbManage(object): | |
|
16 | def __init__(self): | |
|
17 | dburi = 'sqlite:////%s' % os.path.join(ROOT, 'hg_app.db') | |
|
18 | engine = create_engine(dburi) | |
|
19 | init_model(engine) | |
|
20 | self.sa = Session() | |
|
21 | ||
|
22 | def check_for_db(self, override): | |
|
23 | if not override: | |
|
24 | if os.path.isfile(os.path.join(ROOT, 'hg_app.db')): | |
|
25 | raise Exception('database already exists') | |
|
26 | ||
|
27 | def create_tables(self, override=False): | |
|
28 | """ | |
|
29 | Create a auth database | |
|
30 | """ | |
|
31 | self.check_for_db(override) | |
|
32 | ||
|
33 | Base.metadata.create_all(checkfirst=override) | |
|
34 | logging.info('Created tables') | |
|
47 | 35 | |
|
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, |
|
|
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 |
|
|
|
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 | ||
|
51 | try: | |
|
52 | self.sa.add(new_user) | |
|
53 | self.sa.commit() | |
|
54 | except: | |
|
55 | self.sa.rollback() | |
|
56 | raise | |
|
67 | 57 | |
|
68 | 58 | if __name__ == '__main__': |
|
69 | create_tables(True) | |
|
70 | admin_prompt() | |
|
59 | dbmanage = DbManage() | |
|
60 | dbmanage.create_tables(override=True) | |
|
61 | dbmanage.admin_prompt() | |
|
71 | 62 | |
|
72 | 63 |
@@ -1,30 +1,36 b'' | |||
|
1 | import sqlalchemy | |
|
2 | 1 |
|
|
3 | from sqlalchemy import ForeignKey, Column | |
|
4 | 2 | from sqlalchemy.orm import relation, backref |
|
5 | ||
|
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 | |
|
3 | from sqlalchemy import * | |
|
11 | 4 | |
|
12 | 5 | class Users(Base): |
|
13 | 6 | __tablename__ = 'users' |
|
14 | 7 | __table_args__ = {'useexisting':True} |
|
15 | 8 | user_id = Column("user_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=1) |
|
16 | 9 | username = Column("username", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
17 | 10 | password = Column("password", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
18 | 11 | active = Column("active", BOOLEAN(), nullable=True, unique=None, default=None) |
|
19 | 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 | 20 | class UserLogs(Base): |
|
23 | 21 | __tablename__ = 'user_logs' |
|
24 | 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 | 24 | user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=True, unique=None, default=None) |
|
27 | 25 | repository = Column("repository", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
28 | 26 | action = Column("action", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
29 | 27 | action_date = Column("action_date", DATETIME(timezone=False), nullable=True, unique=None, default=None) |
|
28 | ||
|
30 | 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