-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
129 lines (105 loc) · 3.54 KB
/
app.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
#!/usr/bin/env python3
import os
import io
import json
import hashlib
import datetime
from flask import Flask, Response, session, render_template
from flask_login import current_user
from flask_wtf.csrf import CsrfProtect
from flask_babel import Babel
import captcha
from utils import _
from utils import *
from models import Config, Face
from pipeline import get_content_html
from user import user, login_manager
from moderate import moderate
from image import image
from forum import forum, topic_list, filter_deleted_post
from tieba_compatible import tieba
from config import (
PREFIX_ENABLED, PREFIX, DEBUG, SECRET_KEY, UPLOAD_FOLDER, MAX_UPLOAD_SIZE,
RICHTEXT_INFO, RICHTEXT_INFO_JSON, TIEBA_COMP, TIEBA_SYNC_ON, assert_config
)
if TIEBA_SYNC_ON:
from tieba_sync import start_sync as tieba_start_sync
class PrefixMiddleware(object):
def __init__(self, app, prefix=''):
self.app = app
self.prefix = prefix
def __call__(self, environ, start_response):
#environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):]
environ['SCRIPT_NAME'] = self.prefix
return self.app(environ, start_response)
app = Flask(__name__)
assert_config()
app.secret_key = SECRET_KEY
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = MAX_UPLOAD_SIZE
app.config['BABEL_DEFAULT_LOCALE'] = LOCALE
if PREFIX_ENABLED:
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=PREFIX)
babel = Babel(app)
login_manager.init_app(app)
CsrfProtect(app)
app.register_blueprint(user, url_prefix='/user')
app.register_blueprint(moderate, url_prefix='/moderate')
app.register_blueprint(image, url_prefix='/image')
app.register_blueprint(forum, url_prefix='/forum')
if TIEBA_COMP:
app.register_blueprint(tieba, url_prefix='/tieba')
app.add_template_filter(md5, 'md5')
app.add_template_filter(format_date, 'date')
app.add_template_filter(get_color, 'get_color')
app.add_template_filter(path_get_level, 'get_level')
app.add_template_filter(path_get_padding, 'get_padding')
app.add_template_filter(get_content_html, 'get_html')
app.add_template_filter(filter_deleted_post, 'filter_deleted_post')
app.add_template_filter(filter_append_time, 'append_time')
@app.context_processor
def inject_data():
def get_faces():
return Face.select().order_by(Face.name)
if LOCALE:
with open(
os.path.join(
'translations', LOCALE, 'LC_MESSAGES', 'messages.json'
)
) as f:
frontend_trans = f.read()
try:
json.loads(frontend_trans)
except json.decoder.JSONDecodeError:
raise AssertionError(
'Front-end translation file has a wrong format.'
)
else:
frontend_trans = '{}'
return {
'get_config': Config.Get,
'get_faces': get_faces,
'RT_INFO': RICHTEXT_INFO_JSON,
'FRONTEND_TRANS': frontend_trans
}
@app.route('/', methods=['GET', 'POST'])
def index():
return topic_list(tag_slug='')
@app.route('/get-captcha')
def get_captcha():
code = captcha.gen_captcha()
session['captcha'] = code.lower()
image = captcha.gen_image(code)
output = io.BytesIO()
image.save(output, format='PNG')
image_data = output.getvalue()
return Response(image_data, mimetype='image/png')
@app.route('/richtext-info')
def richtext_info():
return render_template('richtext_info.html', info=RICHTEXT_INFO)
def run():
if TIEBA_SYNC_ON:
tieba_start_sync()
app.run(debug=DEBUG)
if __name__ == '__main__':
run()