-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
executable file
·100 lines (88 loc) · 2.92 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
#!/usr/bin/env python
import os
import config
from app import create_app, db
from app.auth.models import User, Role, Permission
from app.unis.models import Uni, City, Subject
from app.field_storage.models import Schema, Field, Category
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand
basedir = os.path.dirname(os.path.abspath(__file__))
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)
def make_shell_context():
return {
'app': app,
'db': db,
'User': User,
'Role': Role,
'Permission': Permission,
'Schema': Schema,
'Field': Field,
'Uni': Uni,
'City': City,
'Subject': Subject,
'Category': Category,
}
manager.add_command('db', MigrateCommand)
manager.add_command('shell', Shell(make_context=make_shell_context))
@manager.command
def createsuperuser(username="root", email="root@localhost"):
"""
Create a superuser
"""
import getpass
password = getpass.getpass()
admin_role = Role.query.filter_by(name='admin').first()
if admin_role is None:
raise ValueError('Admin role not found. Did you initialize the database with manage.py initdb')
# FIXME: check if the account exists first
u = User(username=username, email=email, role=admin_role, password=password)
db.session.add(u)
db.session.commit()
@manager.command
def initdb():
"""
Initialize the database on the first run
"""
db.create_all()
Role.insert_roles()
if Schema.query.filter_by(name='name') is None:
# Create name field
name = Schema()
name.name = 'name'
name.description = 'A basic name field.'
name.permit_comment = False
name.weight = -1000
name.data_type = "textfield"
db.session.add(name)
db.session.commit()
@manager.command
def translation(action):
"""
Update translations
"""
from babel.messages.frontend import CommandLineInterface
# Update translations
if action == "compile":
CommandLineInterface().run(['pybabel', 'compile',
'-d', os.path.join(basedir, 'app', 'translations')])
elif action == "update":
CommandLineInterface().run(['pybabel', 'extract',
'-F', os.path.join(basedir, 'app', 'babel.cfg'),
'-k', '_',
'-o', os.path.join(basedir, 'app', 'messages.pot'),
os.path.join(basedir, 'app')])
CommandLineInterface().run(['pybabel', 'update',
'-i', os.path.join(basedir, 'app', 'messages.pot'),
'-d', os.path.join(basedir, 'app', 'translations')])
else:
raise Exception("no action specified")
@manager.command
def test():
import unittest
tests = unittest.TestLoader().discover(os.path.join(basedir, 'tests'))
unittest.TextTestRunner(verbosity=2).run(tests)
if __name__ == '__main__':
manager.run()