forked from douban/code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.py
85 lines (66 loc) · 2.83 KB
/
web.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
#!/usr/bin/python
# coding:utf8
import os
import sys
import time
import traceback
from quixote.qwip import QWIP
from quixote.publish import Publisher
from vilya import views as controllers
from vilya.config import DEVELOP_MODE
from vilya.libs.gzipper import make_gzip_middleware
from vilya.libs.permdir import get_tmpdir
from vilya.libs.auth.check_auth import check_auth
from vilya.libs.import_obj import import_obj_set
from vilya.libs.template import st
from vilya.models.user import User
from vilya.views.util import is_mobile_device
PERFORMANCE_METRIC_MARKER = '<!-- _performtips_ -->'
def show_performance_metric(request, output):
idx = output.find(PERFORMANCE_METRIC_MARKER)
if idx > 0:
pt = int((time.time() - request.start_time) * 1000)
cls = pt > 250 and 'red' or pt > 100 and 'orange' or 'green'
block = '<li class="hidden-phone"><span style="color:%s"> %d ms </span></li>' % (cls, pt)
output = (output[:idx] + block + output[idx + len(PERFORMANCE_METRIC_MARKER):])
return output
class CODEPublisher(Publisher):
def __init__(self, *args, **kwargs):
Publisher.__init__(self, *args, **kwargs)
self.configure(DISPLAY_EXCEPTIONS='plain',
SECURE_ERRORS=0,
UPLOAD_DIR=get_tmpdir() + '/upload/')
def start_request(self, request):
Publisher.start_request(self, request)
os.environ['SQLSTORE_SOURCE'] = request.get_url()
resp = request.response
resp.set_content_type('text/html; charset=utf-8')
resp.set_header('Pragma', 'no-cache')
resp.set_header('Cache-Control', 'must-revalidate, no-cache, private')
# FIXME: quixote with origin?
resp.set_header('Access-Control-Allow-Origin', '*')
request.enable_ajax = False
request.browser = request.guess_browser_version()
request.method = request.get_method()
request.url = request.get_path()
request.is_mobile = is_mobile_device(request)
request.start_time = time.time()
request.user = User.check_session(request)
import_obj_set("request", request)
def try_publish(self, request, path):
output = Publisher.try_publish(self, request, path)
output = show_performance_metric(request, output)
return output
def finish_failed_request(self, request):
if DEVELOP_MODE:
exc_type, exc_value, tb = sys.exc_info()
raise exc_type, exc_value, tb
else:
return Publisher.finish_failed_request(self, request)
def _generate_cgitb_error(self, request, original_response,
exc_type, exc_value, tb):
traceback.print_exc()
return st('/errors/500.html', **locals())
def create_publisher():
return CODEPublisher(controllers)
app = make_gzip_middleware(QWIP(create_publisher()))