-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmanage.py
executable file
·102 lines (90 loc) · 2.65 KB
/
manage.py
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
#!/usr/bin/env python3
import re
import os
import json
from getpass import getpass
from app import run
from models import User, Profile, init_db
from utils import now
from argparse import ArgumentParser
def create_administrator():
mail = input('Email address: ')
name = input('Nickname: ')
password = getpass('Password: ')
confirm = getpass('Confirm password: ')
if password != confirm:
print('Passwords are inconsistent.')
return
else:
conflict = User.select().where(
(User.mail == mail.lower())
| (User.name == name)
)
if conflict:
print('Conflict detected. Failed to create account.')
return
user = User(
mail = mail.lower(),
name = name,
level = 2,
date_register = now(),
is_active = True
)
user.set_password(password)
user.save(force_insert=True)
Profile.create(user=user)
print('New administrator created successfully.')
def create_post_move_account():
name = input('Nickname: ')
password = getpass('Password: ')
confirm = getpass('Confirm password: ')
if password != confirm:
print('Passwords are inconsistent.')
return
else:
user = User(
mail = 'move_post@foobar',
name = name,
date_register = now(),
is_active = True
)
user.set_password(password)
user.save(force_insert=True)
Profile.create(user=user)
def gen_js_trans_file():
messages = {}
TRANS_STR = re.compile('_\(\'([^\']+)')
for filename in os.listdir('static'):
if filename.endswith('.js'):
path = os.path.join('static', filename)
f = open(path)
for line in f.readlines():
for match in TRANS_STR.finditer(line):
msgid = match.expand('\\1')
messages[msgid] = ''
msg_str = json.dumps(messages)
msg_str = msg_str.replace('{', '{\n')
msg_str = msg_str.replace(',', ',\n')
msg_str = msg_str.replace('}', '\n}')
print(msg_str)
def main():
commands = {
'run': run,
'init-db': init_db,
'create-admin': create_administrator,
'create-move': create_post_move_account,
'gen-js-trans': gen_js_trans_file
}
parser = ArgumentParser()
parser.add_argument(
'cmd',
metavar='Command',
help='(%s)' % '|'.join(list(commands))
)
args = parser.parse_args()
if commands.get(args.cmd):
commands[args.cmd]()
else:
print('Invalid command')
if __name__ == '__main__':
main()