forked from pyconsk/2018.pycon.sk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
361 lines (316 loc) · 12.1 KB
/
views.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/python
# -*- coding: utf8 -*-
import os
from datetime import datetime
from flask import Flask, g, request, render_template, abort, make_response
from flask_babel import Babel, gettext
app = Flask(__name__, static_url_path='/static')
app.config['BABEL_DEFAULT_LOCALE'] = 'sk'
app.jinja_options = {'extensions': ['jinja2.ext.with_', 'jinja2.ext.i18n']}
babel = Babel(app)
SRC_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__)))
LOGO_PYCON = 'logo/pycon.svg'
LANGS = ('en', 'sk')
TIME_FORMAT = '%Y-%m-%dT%H:%M:%S+00:00'
NOW = datetime.utcnow().strftime(TIME_FORMAT)
def get_mtime(filename):
mtime = datetime.fromtimestamp(os.path.getmtime(filename))
return mtime.strftime(TIME_FORMAT)
SITEMAP_DEFAULT = {'prio': '0.1', 'freq': 'weekly'}
SITEMAP = {
'sitemap.xml': {'prio': '0.9', 'freq': 'daily', 'lastmod': get_mtime(__file__)},
'index.html': {'prio': '1', 'freq': 'daily'},
}
LDJSON = {
"@context": "http://schema.org",
"@type": "Organization",
"name": "PyCon SK",
"url": "https://2018.pycon.sk",
"logo": "https://2018.pycon.sk/static/logo/pycon.png",
"sameAs": [
"https://facebook.com/pyconsk",
"https://twitter.com/pyconsk",
"https://www.linkedin.com/company/spy-o--z-",
"https://github.com/pyconsk",
]
}
@app.before_request
def before():
if request.view_args and 'lang_code' in request.view_args:
g.current_lang = request.view_args['lang_code']
if request.view_args['lang_code'] not in LANGS:
return abort(404)
request.view_args.pop('lang_code')
@babel.localeselector
def get_locale():
# try to guess the language from the user accept
# header the browser transmits. The best match wins.
# return request.accept_languages.best_match(['de', 'sk', 'en'])
return g.get('current_lang', app.config['BABEL_DEFAULT_LOCALE'])
def _get_template_variables(**kwargs):
variables = {
'title': gettext('PyCon SK'),
'logo': LOGO_PYCON,
'ld_json': LDJSON
}
variables.update(kwargs)
if 'current_lang' in g:
variables['lang_code'] = g.current_lang
else:
variables['lang_code'] = app.config['BABEL_DEFAULT_LOCALE']
return variables
@app.route('/<lang_code>/index.html')
def index():
lang = get_locale()
LDJSON_EVENT = {
"@context": "http://schema.org",
"@type": "Event",
"name": u"PyCon SK 2018",
"description": "PyCon will be back at Slovakia in 2018 again. PyCon SK is a community-organized conference for "
"the Python programming language.",
"startDate": "2018-03-09T9:00:00+01:00",
"endDate": "2018-03-11T18:00:00+01:00",
"image": "https://2018.pycon.sk/static/img/backgrounds/lecture_hall.jpg",
"location": {
"@type": "Place",
"name": "Bratislava"
},
"url": "https://2018.pycon.sk/" + lang + "/",
"workPerformed": {
"@type": "CreativeWork",
"name": "PyCon SK 2018",
"creator": {
"@type": "Organization",
"name": "SPy o.z.",
"url": "https://spy.python.sk/",
"logo": "https://spy.python.sk/img/logo/spy-logo.png",
}
}
}
return render_template('index.html', **_get_template_variables(ld_json=LDJSON_EVENT, li_index='active'))
@app.route('/<lang_code>/tickets.html')
def tickets():
lang = get_locale()
LDJSON_EVENT = {
"@context": "http://schema.org",
"@type": "Event",
"name": u"PyCon SK 2018",
"description": "PyCon will be back at Slovakia in 2018 again. PyCon SK is a community-organized conference for "
"the Python programming language.",
"startDate": "2018-03-09T9:00:00+01:00",
"endDate": "2018-03-11T18:00:00+01:00",
"image": "https://2018.pycon.sk/static/img/backgrounds/lecture_hall.jpg",
"location": {
"@type": "Place",
"name": "Bratislava"
},
"url": "https://2018.pycon.sk/" + lang + "/",
"workPerformed": {
"@type": "CreativeWork",
"name": "PyCon SK 2018",
"creator": {
"@type": "Organization",
"name": "SPy o.z.",
"url": "https://spy.python.sk/",
"logo": "https://spy.python.sk/img/logo/spy-logo.png",
}
}
}
return render_template('tickets.html', **_get_template_variables(ld_json=LDJSON_EVENT, li_tickets='active'))
@app.route('/<lang_code>/cfp.html')
def cfp():
lang = get_locale()
LDJSON_EVENT = {
"@context": "http://schema.org",
"@type": "Event",
"name": u"PyCon SK 2018",
"description": "PyCon will be back at Slovakia in 2018 again. PyCon SK is a community-organized conference for "
"the Python programming language.",
"startDate": "2018-03-09T9:00:00+01:00",
"endDate": "2018-03-11T18:00:00+01:00",
"image": "https://2018.pycon.sk/static/img/backgrounds/lecture_hall.jpg",
"location": {
"@type": "Place",
"name": "Bratislava"
},
"url": "https://2018.pycon.sk/" + lang + "/",
"workPerformed": {
"@type": "CreativeWork",
"name": "PyCon SK 2018",
"creator": {
"@type": "Organization",
"name": "SPy o.z.",
"url": "https://spy.python.sk/",
"logo": "https://spy.python.sk/img/logo/spy-logo.png",
}
}
}
return render_template('cfp.html', **_get_template_variables(ld_json=LDJSON_EVENT, li_cfp='active'))
@app.route('/<lang_code>/coc.html')
def coc():
lang = get_locale()
LDJSON_EVENT = {
"@context": "http://schema.org",
"@type": "Event",
"name": u"PyCon SK 2018",
"description": "PyCon will be back at Slovakia in 2018 again. PyCon SK is a community-organized conference for "
"the Python programming language.",
"startDate": "2018-03-09T9:00:00+01:00",
"endDate": "2018-03-11T18:00:00+01:00",
"image": "https://2018.pycon.sk/static/img/backgrounds/lecture_hall.jpg",
"location": {
"@type": "Place",
"name": "Bratislava"
},
"url": "https://2018.pycon.sk/" + lang + "/",
"workPerformed": {
"@type": "CreativeWork",
"name": "PyCon SK 2018",
"creator": {
"@type": "Organization",
"name": "SPy o.z.",
"url": "https://spy.python.sk/",
"logo": "https://spy.python.sk/img/logo/spy-logo.png",
}
}
}
return render_template('coc.html', **_get_template_variables(ld_json=LDJSON_EVENT, li_coc='active'))
@app.route('/<lang_code>/hall-of-fame.html')
def hall_of_fame():
lang = get_locale()
LDJSON_EVENT = {
"@context": "http://schema.org",
"@type": "Event",
"name": u"PyCon SK 2018",
"description": "PyCon will be back at Slovakia in 2018 again. PyCon SK is a community-organized conference for "
"the Python programming language.",
"startDate": "2018-03-09T9:00:00+01:00",
"endDate": "2018-03-11T18:00:00+01:00",
"image": "https://2018.pycon.sk/static/img/backgrounds/lecture_hall.jpg",
"location": {
"@type": "Place",
"name": "Bratislava",
},
"url": "https://2018.pycon.sk/" + lang + "/",
"workPerformed": {
"@type": "CreativeWork",
"name": "PyCon SK 2018",
"creator": {
"@type": "Organization",
"name": "SPy o.z.",
"url": "https://spy.python.sk/",
"logo": "https://spy.python.sk/img/logo/spy-logo.png",
}
}
}
return render_template('hall-of-fame.html', **_get_template_variables(ld_json=LDJSON_EVENT,
li_hall_of_fame='active'))
@app.route('/<lang_code>/venue.html')
def venue():
lang = get_locale()
LDJSON_EVENT = {
"@context": "http://schema.org",
"@type": "Event",
"name": u"PyCon SK 2018",
"description": "PyCon will be back at Slovakia in 2018 again. PyCon SK is a community-organized conference for "
"the Python programming language.",
"startDate": "2018-03-09T9:00:00+01:00",
"endDate": "2018-03-11T18:00:00+01:00",
"image": "https://2018.pycon.sk/static/img/backgrounds/lecture_hall.jpg",
"location": {
"@type": "Place",
"name": "Bratislava"
},
"url": "https://2018.pycon.sk/" + lang + "/",
"workPerformed": {
"@type": "CreativeWork",
"name": "PyCon SK 2018",
"creator": {
"@type": "Organization",
"name": "SPy o.z.",
"url": "https://spy.python.sk/",
"logo": "https://spy.python.sk/img/logo/spy-logo.png",
}
}
}
return render_template('venue.html', **_get_template_variables(ld_json=LDJSON_EVENT, li_venue='active'))
@app.route('/<lang_code>/sponsoring.html')
def sponsoring():
lang = get_locale()
LDJSON_EVENT = {
"@context": "http://schema.org",
"@type": "Event",
"name": u"PyCon SK 2018",
"description": "PyCon will be back at Slovakia in 2018 again. PyCon SK is a community-organized conference for "
"the Python programming language.",
"startDate": "2018-03-09T9:00:00+01:00",
"endDate": "2018-03-11T18:00:00+01:00",
"image": "https://2018.pycon.sk/static/img/backgrounds/lecture_hall.jpg",
"location": {
"@type": "Place",
"name": "Bratislava"
},
"url": "https://2018.pycon.sk/" + lang + "/",
"workPerformed": {
"@type": "CreativeWork",
"name": "PyCon SK 2018",
"creator": {
"@type": "Organization",
"name": "SPy o.z.",
"url": "https://spy.python.sk/",
"logo": "https://spy.python.sk/img/logo/spy-logo.png",
}
}
}
return render_template('sponsoring.html', **_get_template_variables(ld_json=LDJSON_EVENT, li_sponsoring='active'))
def get_lastmod(route, sitemap_entry):
"""Used by sitemap() below"""
if 'lastmod' in sitemap_entry:
return sitemap_entry['lastmod']
template = route.rule.split('/')[-1]
template_file = os.path.join(SRC_DIR, 'templates', template)
if os.path.exists(template_file):
return get_mtime(template_file)
return NOW
@app.route('/sitemap.xml', methods=['GET'])
def sitemap():
"""Generate sitemap.xml. Makes a list of urls and date modified."""
domain = 'https://2018.pycon.sk'
pages = []
# static pages
for rule in app.url_map.iter_rules():
if "GET" in rule.methods:
if len(rule.arguments) == 0:
indx = rule.rule.replace('/', '')
sitemap_data = SITEMAP.get(indx, SITEMAP_DEFAULT)
pages.append({
'loc': domain + rule.rule,
'lastmod': get_lastmod(rule, sitemap_data),
'freq': sitemap_data['freq'],
'prio': sitemap_data['prio'],
})
elif 'lang_code' in rule.arguments:
indx = rule.rule.replace('/<lang_code>/', '')
for lang in LANGS:
alternate = []
for alt_lang in LANGS:
if alt_lang != lang:
alternate.append({
'lang': alt_lang,
'url': domain + rule.rule.replace('<lang_code>', alt_lang)
})
sitemap_data = SITEMAP.get(indx, SITEMAP_DEFAULT)
pages.append({
'loc': domain + rule.rule.replace('<lang_code>', lang),
'alternate': alternate,
'lastmod': get_lastmod(rule, sitemap_data),
'freq': sitemap_data['freq'],
'prio': sitemap_data['prio'],
})
sitemap_xml = render_template('sitemap_template.xml', pages=pages)
response= make_response(sitemap_xml)
response.headers["Content-Type"] = "application/xml"
return response
if __name__ == "__main__":
app.run(debug=True, host=os.environ.get('FLASK_HOST', '127.0.0.1'), port=int(os.environ.get('FLASK_PORT', 5000)),
use_reloader=True)