forked from PyLadiesCZ/pyladies.cz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyladies_cz.py
executable file
·315 lines (250 loc) · 9.02 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
#!/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
import collections
from urllib.parse import urlencode
from flask import Flask, render_template, url_for, send_from_directory
from flask_frozen import Freezer
import yaml
import jinja2
import markdown
from elsa import cli
app = Flask('pyladies_cz')
app.config['TEMPLATES_AUTO_RELOAD'] = True
orig_path = os.path.join(app.root_path, 'original/')
v1_path = os.path.join(orig_path, 'v1/')
MISSING = object()
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'),
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('/v1/<path:path>')
def v1(path):
if path in REDIRECTS:
return redirect(REDIRECTS[path])
return send_from_directory(v1_path, path)
@app.route('/index.html')
def index_html():
return redirect(url_for('index'))
@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 = jinja2.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):
try:
file = open(filename, encoding='utf-8')
except FileNotFoundError:
if default is MISSING:
raise
return default
with file:
data = yaml.safe_load(file)
return data
def read_lessons_yaml(filename):
data = read_yaml(filename)
# workaround for http://stackoverflow.com/q/36157569/99057
# Convert datetime objects to strings
for lesson in data:
if 'date' in lesson:
lesson['dates'] = [lesson['date']]
if 'description' in lesson:
lesson['description'] = convert_markdown(lesson['description'],
inline=True)
for mat in lesson.get('materials', ()):
mat['name'] = convert_markdown(mat['name'], inline=True)
# If lesson has no `done` key, add them according to lesson dates
# All lesson's dates must be in past to mark it as done
done = lesson.get('done', None)
if done is None and 'dates' in lesson:
all_done = []
for date in lesson['dates']:
all_done.append(datetime.date.today() > date)
lesson['done'] = all(all_done)
return data
def read_meetups_yaml(filename):
data = read_yaml(filename)
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']
# 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 = os.path.relpath(os.path.join(name, file), v1_path)
yield {'path': 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')