forked from mozilla/osumo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osumo.py
149 lines (124 loc) · 4.09 KB
/
osumo.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
import json
import logging
import urllib
import os
from flask import (
Flask,
render_template,
make_response,
abort,
request,
send_file
)
from flask.ext.appcache import Appcache
from flask.ext.assets import Environment, Bundle
import requests
# Create the app
app = Flask(__name__)
app.config.from_pyfile("settings.py")
app.config.from_pyfile("settings_local.py", silent=True)
# setup logging
@app.before_first_request
def setup_logging():
if not app.debug:
app.logger.addHandler(logging.StreamHandler())
app.logger.setLevel(logging.INFO)
# Getting the supported languages from SUMO
LANGUAGES = requests.get((app.config['SUMO_URL'] +
'offline/get-languages')).json()
LANGUAGES = json.dumps(LANGUAGES['languages'])
# Sets up the assets
assets = Environment(app)
assets.auto_build = app.debug
assets.debug = app.debug
# we don't need this as manifest.appcache will change
assets.url_expire = False
css = Bundle(
'css/develop/gaia.css',
'css/develop/doc.css',
'css/develop/installer.css',
'css/develop/nav.css',
'css/develop/app.css',
filters='cssmin',
output='css/app.min.css'
)
assets.register('css_all', css)
scripts = ['js/develop/app.js']
for root, subdir, fnames in os.walk('static/js/develop'):
for filename in fnames:
if filename.endswith('.js') and filename != 'app.js':
# get rid of te 'static/'
scripts.append(os.path.join(root, filename)[7:])
js = Bundle(*scripts, filters='uglifyjs', output='js/app.min.js')
assets.register('js_all', js)
if not app.debug:
css.build()
js.build()
# Sets up the angular partials
PARTIALS = u''
if not app.debug:
inline_partial = unicode("""
<script id="{path}" type="text/ng-template">
{content}
</script>
""")
for root, subdir, fnames in os.walk('static/partials'):
for filename in fnames:
if filename.endswith('.html'):
with open(os.path.join(root, filename)) as f:
content = f.read().decode('utf-8')
content = unicode(content)
PARTIALS += inline_partial.format(path=('/static/partials/' +
filename),
content=content)
# Sets up Appcache
appcache = Appcache(app)
if app.debug:
appcache.add_excluded_urls('/static/js/app.min.js',
'/static/css/app.min.css')
else:
appcache.add_excluded_urls('/static/js/develop')
appcache.add_excluded_urls('/static/css/develop')
appcache.add_excluded_urls('/static/partials')
appcache.add_excluded_urls('/static/js/tests')
appcache.add_excluded_urls('/static/.webassets-cache')
appcache.add_folder('static')
appcache.add_urls('/meta.js', '/')
@app.before_request
def before_request():
app.jinja_env.globals['partials'] = PARTIALS
@app.route('/manifest.webapp')
def manifest_webapp():
return send_file('manifest.webapp',
mimetype='application/x-web-app-manifest+json')
@app.route('/images')
def images():
if 'url' not in request.args:
return abort(400)
target = 'https://support.cdn.mozilla.net/' + request.args['url']
response = requests.get(target)
if response.status_code == 200:
imgdata = ('data:image/png;base64,' +
urllib.quote(response.content.encode('base64')))
response = make_response(imgdata)
response.mimetype = 'text/plain'
return response
else:
return abort(response.status_code)
@app.route('/meta.js')
def meta_js():
context = {
'COMMIT_SHA': app.config['COMMIT_SHA'],
'APPCACHE_HASH': appcache.hash()[0],
'BASE_URL': app.config['BASE_URL'],
'SUMO_URL': app.config['SUMO_URL'],
'LANGUAGES': LANGUAGES
}
response = make_response(render_template('meta.js', **context))
response.mimetype = 'application/javascript'
return response
# Catch all URL for HTML push state
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def main(path):
return render_template('app.html')