-
Notifications
You must be signed in to change notification settings - Fork 171
/
runserver.py
executable file
·175 lines (140 loc) · 4.45 KB
/
runserver.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# **************************************************************************
# Copyright © 2016-2019 jianglin
# File Name: runserver.py
# Author: jianglin
# Email: [email protected]
# Created: 2016-10-25 22:01:29 (CST)
# Last Update: Monday 2022-12-12 15:29:04 (CST)
# By:
# Description:
# **************************************************************************
from flask import current_app
from flask.cli import FlaskGroup, run_command
from werkzeug.contrib.fixers import ProxyFix
from code import interact
from forums import create_app
from forums.extension import db, cache, search
from forums.api.user.db import User
import click
import os
import sys
app = create_app('config')
app.wsgi_app = ProxyFix(app.wsgi_app)
cli = FlaskGroup(add_default_commands=False, create_app=lambda r: app)
cli.add_command(run_command)
try:
from flask_migrate import Migrate
migrate = Migrate(app, db)
except ImportError:
pass
@cli.command('shell', short_help='Starts an interactive shell.')
def shell_command():
ctx = current_app.make_shell_context()
interact(local=ctx)
@cli.command()
@click.option('-p', '--port', default=8000)
def runserver(port):
app.run(port=port)
@cli.command()
def create_index():
return search.create_index()
@cli.command()
def update_index():
return search.create_index(update=True)
@cli.command()
def delete_index():
return search.create_index(delete=True)
@cli.command()
def clear_cache():
cache.clear()
@cli.command()
def init_perm():
from forums.api.user.db import Group
anonymous = Group.query.filter_by(name='anonymous').first()
if not anonymous:
anonymous = Group(name='anonymous')
anonymous.save()
logined = Group.query.filter_by(name='logined').first()
if not logined:
logined = Group(name='logined')
logined.save()
for rule in app.url_map.iter_rules():
# print(rule.rule, rule.subdomain, rule.methods, rule.endpoint)
print(rule.endpoint)
methods = []
for method in rule.methods:
methods.append(method)
method = 'get' if method in ['HEAD', 'OPTIONS'] else method.lower()
if not rule.endpoint.startswith('admin'):
anonymous.add_perm(
method,
rule.endpoint,
description='anonymous组允许{}'.format(methods))
logined.add_perm(
method,
rule.endpoint,
description='logined组允许{}'.format(methods))
@cli.command()
def initdb():
"""
Drops and re-creates the SQL schema
"""
db.drop_all()
db.configure_mappers()
db.create_all()
db.session.commit()
@cli.command()
@click.option('-l', '--lang', default='zh')
def babel_init(lang):
babel_conf = "translations/babel.cfg"
src_path = ["forums", "templates"]
os.system(
'pybabel extract -F {0} -k lazy_gettext -o messages.pot {1}'.format(
babel_conf, ' '.join(src_path)))
os.system(
'pybabel init -i messages.pot -d translations -l {0}'.format(lang))
os.unlink('messages.pot')
@cli.command()
def babel_update():
babel_conf = "translations/babel.cfg"
src_path = ["forums", "templates"]
os.system(
'pybabel extract -F {0} -k lazy_gettext -o messages.pot {1}'.format(
babel_conf, ' '.join(src_path)))
os.system('pybabel update -i messages.pot -d translations')
os.unlink('messages.pot')
@cli.command()
def babel_compile():
os.system('pybabel compile -d translations')
@cli.command()
@click.option('-u', '--username')
def delete_user(username):
user = User.query.filter_by(username=username).first()
user.delete()
@cli.command()
@click.option('-u', '--username')
@click.password_option('-p', '--password')
def password_user(username, password):
user = User.query.filter_by(username=username).first()
user.set_password(password)
user.save()
@cli.command(short_help='Create user.')
@click.option('-u', '--username', prompt=True, default="admin")
@click.option('-e', '--email', prompt=True)
@click.password_option('-p', '--password')
def create_user(username, email, password):
user = User(
username=username,
email=email,
is_superuser=True,
is_confirmed=True,
)
user.set_password(password)
user.save()
if __name__ == '__main__':
if len(sys.argv) == 1:
app.run()
else:
cli.main()