forked from GTxx/youjiao
/
manage.py
155 lines (128 loc) · 4.51 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
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
146
147
148
149
150
151
152
153
154
155
# coding: utf-8
from flask_script import Manager
from flask_migrate import MigrateCommand
from flask import url_for
from youjiao.extensions import db, qiniu
from youjiao.app import create_app
from youjiao.user.models import User, Role, UserProfile
import os, json
import urlparse
# Used by app debug & livereload
PORT = 5000
app = create_app()
manager = Manager(app)
@manager.option('-p', '--port', dest='port', default=5000)
def run(port):
"""Run app."""
app.run(host='0.0.0.0', port=int(port))
@manager.command
def createdb():
# -*- coding: utf-8 -*-
"""Create database."""
db.create_all()
@manager.command
def init_db():
try:
role = Role(name='editor', description='editor role')
role.save()
role = Role(name='admin', description='admin role')
role.save()
create_admin('admin', '111111', 'admin@1.com')
create_editor('editor1', '111111', 'editor1@1.com')
create_editor('editor2', '111111', 'editor2@1.com')
create_common_user('wangbin', '111111', 'wangbin@1.com')
create_common_user('xx', '111111', 'xx@1.com')
create_common_user('jiyu', '111111', 'jiyu@1.com')
create_common_user('quwenyu', '111111', 'quwenyu@1.com')
from youjiao.test_data import book_list, activity_list
from youjiao.teach_material.models import Book
from youjiao.content.models import Activity
for book in book_list:
b = Book(**book._asdict())
b.save()
for activity in activity_list:
a = Activity(**activity._asdict())
a.save()
except Exception as e:
print(e)
@manager.command
def drop_table():
"""Create database."""
db.drop_all()
@manager.shell
def make_shell_context():
return dict(app=app, db=db)
@manager.command
def create_qiniu_conf():
static_dir = os.path.join(os.getcwd(), 'youjiao/static/build')
dest = "qiniu:access_key={}&secret_key={}&bucket={}&key_prefix={}".format(
qiniu.AK, qiniu.SK, qiniu.PUBLIC_BUCKET_NAME, qiniu.STATIC_CDN_PREFIX)
config = {"src": static_dir, "dest": dest, "debug_level": 1}
with open('qiniu.json', 'w') as f:
f.write(json.dumps(config))
@manager.option('-n', '--name', dest='name', default='admin')
@manager.option('-p', '--password', dest='password', default='123456')
@manager.option('-e', '--email', dest='email', default='admin@1.com')
def create_admin(name, password, email):
user = User.create_user(name, email, password)
profile = UserProfile()
profile.save()
user.profile = profile
role = Role.query.filter_by(name='admin').first()
if not role:
role = Role('admin', 'admin role')
role.save()
user.roles.append(role)
role = Role.query.filter_by(name='editor').first()
if not role:
role = Role('editor', 'editor role')
role.save()
user.roles.append(role)
user.save()
def create_editor(name, password, email):
user = User.create_user(name, email, password)
profile = UserProfile()
profile.save()
user.profile = profile
role = Role.query.filter_by(name='editor').first()
if not role:
role = Role('editor', 'editor role')
role.save()
user.roles.append(role)
user.save()
def create_common_user(name, password, email):
user = User.create_user(name, email, password)
profile = UserProfile()
profile.save()
user.profile = profile
user.save()
@app.template_filter('asset')
def asset_filter(file_string):
try:
if app.debug == True:
static_path = '/static/build'
else:
static_path = urlparse.urljoin(qiniu.PUBLIC_CDN_DOMAIN, qiniu.STATIC_CDN_PREFIX)
filename = '.'.join(file_string.split('.')[:-1])
filetype = file_string.split('.').pop()
file_resolve_name = app.assets[filename][filetype]
file_path = os.path.join(static_path, filetype + '/' + file_resolve_name)
return file_path
except Exception as e:
return ''
@app.template_filter('vendor_asset')
def vendor_asset_filter(file_string):
static_path = 'build'
file_path = os.path.join(static_path, file_string)
try:
if app.debug == True:
return url_for('static', filename=file_path)
else:
relative_path = os.path.join(qiniu.STATIC_CDN_PREFIX, file_string)
res = urlparse.urljoin(qiniu.PUBLIC_CDN_DOMAIN, relative_path)
return res
except Exception as e:
return ''
manager.add_command('db', MigrateCommand)
if __name__ == "__main__":
manager.run()