initialize_db.py
3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import sys
import csv
import subprocess
import transaction
from getpass import getpass
from translationstring import (
TranslationString,
Translator,
ugettext_policy,
)
from translationstring.tests.translations import Translations
from sqlalchemy import engine_from_config
from ziggurat_foundations.models.services.user import UserService
from pyramid.paster import (
get_appsettings,
setup_logging,
)
from ..models import (
DBSession,
Base,
)
from ..models.ziggurat import (
Group,
GroupPermission,
UserGroup,
User,
)
class Penerjemah:
def __init__(self, domain='initialize_db'):
self.domain = domain
def configure(self, settings):
locale_ids = [settings['pyramid.default_locale_name']]
here = os.path.abspath(os.path.dirname(__file__))
localedir = os.path.join(here, '..', 'locale')
try:
self.translations = Translations.load(
localedir, locale_ids, self.domain)
except TypeError:
self.translations = None
self.translator = self.translations and Translator(
self.translations, ugettext_policy)
def terjemahkan(self, msg, mapping=None):
ts = TranslationString(msg, mapping=mapping)
if self.translator:
return self.translator(ts)
return ts.interpolate()
obj_penerjemah = Penerjemah()
def _(msg, mapping=None):
return obj_penerjemah.terjemahkan(msg, mapping)
def usage(argv):
cmd = os.path.basename(argv[0])
print('usage: %s <config_uri>\n'
'(example: "%s development.ini")' % (cmd, cmd))
sys.exit(1)
def read_file(filename):
f = open(filename)
s = f.read()
f.close()
return s
def alembic_run(ini_file, url):
bin_path = os.path.split(sys.executable)[0]
alembic_bin = os.path.join(bin_path, 'alembic')
command = (alembic_bin, 'upgrade', 'head')
s = read_file(ini_file)
s = s.replace('{db_url}', url)
f = open('alembic.ini', 'w')
f.write(s)
f.close()
subprocess.call(command)
os.remove('alembic.ini')
def get_file(filename):
base_dir = os.path.split(__file__)[0]
fullpath = os.path.join(base_dir, 'data', filename)
return open(fullpath)
def ask_password(name):
data = dict(name=name)
msg1 = _('Enter new password for ${name}: ', mapping=data)
msg2 = _('Retype new password for ${name}: ', mapping=data)
while True:
pass1 = getpass(msg1)
if not pass1:
continue
pass2 = getpass(msg2)
if pass1 == pass2:
return pass1
print(_('Sorry, passwords do not match'))
def restore_csv(table, filename):
q = DBSession.query(table)
if q.first():
return
with get_file(filename) as f:
reader = csv.DictReader(f)
for cf in reader:
row = table()
for fieldname in cf:
val = cf[fieldname]
if not val:
continue
setattr(row, fieldname, val)
DBSession.add(row)
return True
def main(argv=sys.argv):
if len(argv) != 2:
usage(argv)
config_uri = argv[1]
setup_logging(config_uri)
settings = get_appsettings(config_uri)
obj_penerjemah.configure(settings)
engine = engine_from_config(settings, 'sqlalchemy.')
Base.metadata.bind = engine
Base.metadata.create_all()
alembic_run('alembic.ini.tpl', settings['sqlalchemy.url'])
with transaction.manager:
if restore_csv(User, 'users.csv'):
DBSession.flush()
q = DBSession.query(User).filter_by(id=1)
user = q.first()
password = ask_password(user.user_name)
UserService.set_password(user, password)
UserService.regenerate_security_code(user)
restore_csv(Group, 'groups.csv')
restore_csv(UserGroup, 'users_groups.csv')