-
Notifications
You must be signed in to change notification settings - Fork 87
/
pyladies_cz.py
executable file
·343 lines (274 loc) · 10.1 KB
/
pyladies_cz.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
#!/usr/bin/env python3
"""Create or serve the pyladies.cz website
"""
import sys
if sys.version_info < (3, 0):
raise RuntimeError('You need Python 3.')
import os
import fnmatch
import datetime
from urllib.parse import urlencode
from pathlib import Path, PurePosixPath
import functools
from flask import Flask, render_template, url_for, send_from_directory, abort
from flask_frozen import Freezer
import yaml
import markdown
import markupsafe
from elsa import cli
app = Flask('pyladies_cz')
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.config['TRAP_HTTP_EXCEPTIONS'] = True
orig_path = os.path.join(app.root_path, 'original/')
v1_path = os.path.join(orig_path, 'v1/')
MISSING = object()
class YamlIOException(Exception):
pass
def redirect(url):
"""Return a response with a Meta redirect"""
# With static pages, we can't use HTTP redirects.
# Return a page wit <meta refresh> instead.
#
# When Frozen-Flask gets support for redirects
# (https://github.com/Frozen-Flask/Frozen-Flask/issues/81),
# this should be revisited.
return render_template('meta_redirect.html', url=url)
########
## Views
@app.route('/')
def index():
news = read_news_yaml('news.yml')
return render_template('index.html', news=news)
@app.route('/<city_slug>/')
def city(city_slug):
cities = read_yaml('cities.yml')
city = cities.get(city_slug)
if city is None:
abort(404)
meetups = read_meetups_yaml('meetups/' + city_slug + '.yml')
current_meetups = [m for m in meetups if m['current']]
past_meetups = [m for m in meetups if not m['current']]
registration_meetups = [
m for m in current_meetups if m.get('registration_status')=='running']
return render_template(
'city.html',
city_slug=city_slug,
city_title=city['title'],
team_name=city.get('team-name'),
newsletter=city.get('newsletter'),
current_meetups=current_meetups,
past_meetups=past_meetups,
registration_meetups=registration_meetups,
contacts=city.get('contacts'),
team=read_yaml('teams/' + city_slug + '.yml', default=()),
)
@app.route('/<city>_course/')
def course_redirect(city):
return redirect(url_for('city', city_slug=city, _anchor='meetups'))
@app.route('/<city>_info/')
def info_redirect(city):
return redirect(url_for('city', city_slug=city, _anchor='city-info'))
@app.route('/praha-cznic/')
def praha_cznic():
return redirect('https://naucse.python.cz/2018/pyladies-praha-jaro-cznic/')
@app.route('/praha-ntk/')
def praha_ntk():
return redirect('https://naucse.python.cz/2018/pyladies-praha-jaro-ntk/')
@app.route('/stan_se/')
def stan_se():
return render_template('stan_se.html')
@app.route('/faq/')
def faq():
return render_template('faq.html')
@app.route('/zpracovani_osobnich_udaju/')
def gdpr():
return render_template('gdpr.html')
@app.route('/v1/')
@app.route('/v1/<path:path>')
def v1(path='index.html'):
if path in REDIRECTS:
return redirect(REDIRECTS[path])
if path[-1] == '/':
path += 'index.html'
return send_from_directory(v1_path, path)
@app.route('/course.html')
def course_html():
return send_from_directory(orig_path, 'course.html')
@app.route('/googlecc704f0f191eda8f.html')
def google_verification():
# Verification page for GMail on our domain
return send_from_directory(app.root_path, 'google-verification.html')
##########
## Template variables
@app.context_processor
def inject_cities():
cities = {}
for city_name, city_info in read_yaml('cities.yml').items():
meetups = read_meetups_yaml(f'meetups/{city_name}.yml')
meetups_nonpermanent = [m for m in meetups if m.get('start')]
cities[city_name] = {
**city_info,
'active_registration': any(m.get('registration_status') == 'running' for m in meetups_nonpermanent),
}
return dict(cities=cities)
##########
## Helpers
md = markdown.Markdown(extensions=['meta', 'markdown.extensions.toc'])
@app.template_filter('markdown')
def convert_markdown(text, inline=False):
result = markupsafe.Markup(md.convert(text))
if inline and result[:3] == '<p>' and result[-4:] == '</p>':
result = result[3:-4]
return result
@app.template_filter('date_range')
def date_range(dates, sep='–'):
start, end = dates
pieces = []
if start != end:
if start.year != end.year:
pieces.append('{d.day}. {d.month}. {d.year}'.format(d=start))
elif start.month != end.month:
pieces.append('{d.day}. {d.month}.'.format(d=start))
else:
pieces.append('{d.day}.'.format(d=start))
pieces.append('–')
pieces.append('{d.day}. {d.month}. {d.year}'.format(d=end))
return ' '.join(pieces)
def read_yaml(filename, default=MISSING):
"""Read the given YAML file
If the file is not found:
- if default is given, return it
- else, raise FileNotFoundError
"""
# Reading YAML is slow, and we need info from cities.yml for every single
# page. To avoid loading all the time, we cache the result.
# To make the site react to changes in the files, we use the file size
# and last-modified time as part of the cache key. If either changes,
# the cache will be invalidated and the file will be read from disk again.
try:
info = os.stat(filename)
except FileNotFoundError:
if default is MISSING:
raise
return default
return _read_yaml_cached(filename, info.st_size, info.st_mtime)
@functools.lru_cache()
def _read_yaml_cached(filename, size, mtime):
with open(filename, encoding='utf-8') as file:
try:
data = yaml.safe_load(file)
except Exception:
err_message = f"""The YAML file '{filename}' can not be read by
the yaml parser. Please check if the file exist or if the file is
a valid yaml file."""
raise YamlIOException(err_message)
return data
def read_meetups_yaml(filename):
data = read_yaml(filename)
if data is None:
# Treat an empty YAML file as no courses.
# (Otherwise, the file would have to contain [])
data = []
today = datetime.date.today()
for meetup in data:
# 'date' means both start and end
if 'date' in meetup:
meetup['start'] = meetup['date']
meetup['end'] = meetup['date']
# Test that meetup doesn't end earlier than it starts
start = meetup.get('start', None)
end = meetup.get('end', None)
# Only check further if meetup has got an end date
if end is not None:
# If end date is set, there must be a start date as well
if start is None:
raise ValueError(f"There's no start date for meetup {meetup}.")
else:
# If both dates are set, end must be the same as or later than start
if end < start:
raise ValueError(
f"Start date is later than end date. Meetup: {meetup}."
)
# Derive a URL for places that don't have one from the location
if 'place' in meetup:
if ('url' not in meetup['place']
and {'latitude', 'longitude'} <= meetup['place'].keys()):
place = meetup['place']
place['url'] = 'http://mapy.cz/zakladni?' + urlencode({
'y': place['latitude'],
'x': place['longitude'],
'z': '16',
'id': place['longitude'] + ',' + place['latitude'],
'source': 'coor',
'q': place['name'],
})
# Figure out the status of registration
if 'registration' in meetup:
if 'start' in meetup and meetup['start'] <= today:
meetup['registration_status'] = 'meetup_started'
elif 'end' in meetup['registration'] and meetup['registration']['end'] < today:
meetup['registration_status'] = 'closed'
else:
meetup['registration_status'] = 'running'
meetup['current'] = ('end' not in meetup) or (meetup['end'] >= today)
return list(reversed(data))
def read_news_yaml(filename):
data = read_yaml(filename)
today = datetime.date.today()
news = []
for new in data:
if new['expires'] >= today:
news.append(new)
return news
def pathto(name, static=False):
if static:
prefix = '_static/'
if name.startswith(prefix):
return url_for('static', filename=name[len(prefix):])
prefix = 'v1/'
if name.startswith(prefix):
return url_for('v1', path=name[len(prefix):])
return name
return url_for(name)
@app.context_processor
def inject_context():
return {
'pathto': pathto,
'today': datetime.date.today(),
}
############
## Redirects
REDIRECTS_DATA = read_yaml('redirects.yml')
REDIRECTS = {}
for directory, pages in REDIRECTS_DATA['naucse-lessons'].items():
for html_filename, lesson in pages.items():
new_url = 'http://naucse.python.cz/lessons/{}/'.format(lesson)
REDIRECTS['{}/{}'.format(directory, html_filename)] = new_url
##########
## Freezer
freezer = Freezer(app)
@freezer.register_generator
def v1():
IGNORE = ['*.aux', '*.out', '*.log', '*.scss', '.travis.yml', '.gitignore']
for name, dirs, files in os.walk(v1_path):
if '.git' in dirs:
dirs.remove('.git')
for file in files:
if file == '.git':
continue
if not any(fnmatch.fnmatch(file, ig) for ig in IGNORE):
path = Path(name) / file
yield {'path': PurePosixPath(path.relative_to(v1_path))}
for path in REDIRECTS:
yield url_for('v1', path=path)
OLD_CITIES = 'praha', 'brno', 'ostrava'
@freezer.register_generator
def course_redirect():
for city in OLD_CITIES:
yield {'city': city}
@freezer.register_generator
def info_redirect():
for city in OLD_CITIES:
yield {'city': city}
if __name__ == '__main__':
cli(app, freezer=freezer, base_url='http://pyladies.cz')