-
Notifications
You must be signed in to change notification settings - Fork 2
/
manage.py
207 lines (176 loc) · 6.41 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# coding: utf-8
from flask import url_for
from youjiao.extensions import db, flask_qiniu, manager
from youjiao.app import create_app
from youjiao.user.models import User, Role, UserProfile
from flask_migrate import Migrate, MigrateCommand
import os
import urlparse
# import command
from youjiao.command.db import db_shell
from youjiao.command.qiniu_command import create_qiniu_conf, upload_static
app = manager(app=create_app)
@manager.option('-p', '--port', dest='port', default=5000)
@manager.option('-b', '--build', dest='build', default=False)
def run(port, build):
"""Run app."""
# build front end
if build:
current_path = os.getcwd()
static_path = os.path.join(current_path, 'youjiao/static')
os.chdir(static_path)
os.system('npm run build-dev')
os.chdir(current_path)
app.run(host='0.0.0.0', port=int(port))
@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.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
user.save()
role = Role.query.filter_by(name='admin').first()
if not role:
role = Role()
role.name = 'admin'
role.description = 'admin role'
role.save()
user.roles.append(role)
role = Role.query.filter_by(name='editor').first()
if not role:
role = Role()
role.name = 'editor'
role.description = '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()
@manager.command
def create_audio():
from youjiao.teach_material.models import Courseware
from youjiao.yj_media.models import Audio
ids = []
for course in Courseware.query.all():
if course.content:
try:
audio_list = [i for i in course.content if i['type'] == 'audio']
for audio in audio_list:
if audio['key'].lower().endswith('.wav'):
audio_obj = Audio(name=audio['key'], qiniu_key=audio['key'])
audio_obj.save()
ids.append(audio_obj)
except Exception as e:
print(e)
Audio.batch_convert_mp3([audio.id for audio in ids])
# begin to convert
# Audio.batch_convert_mp3(ids)
@manager.command
def replace_mp3():
from youjiao.teach_material.models import Courseware
from youjiao.yj_media.models import Audio
for course in Courseware.query.all():
if course.content:
content = []
modified = False
# if course.id == 1:
# import ipdb; ipdb.set_trace()
try:
for item in course.content:
if item['type'] != 'audio':
content.append(item)
else:
key = item['key']
if key.lower().endswith('.wav'):
audio = Audio.query.filter_by(qiniu_key=key).first()
if audio:
key = key + '.mp3'
modified = True
content.append({'type': 'audio', 'name': item['name'], 'key': key})
if modified:
course.content = content
course.save()
except:
pass
@app.template_filter('asset')
def asset_filter(file_string):
try:
if app.debug == True:
static_path = '/static/build'
else:
static_path = urlparse.urljoin(flask_qiniu.PUBLIC_CDN_DOMAIN, flask_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(flask_qiniu.STATIC_CDN_PREFIX, file_string)
res = urlparse.urljoin(flask_qiniu.PUBLIC_CDN_DOMAIN, relative_path)
return res
except Exception as e:
return ''
manager.add_command('db', MigrateCommand)
# flask_migrate
# add compare type to check like string max length
Migrate(app, db, compare_type=True)
if __name__ == "__main__":
manager.run()