forked from PyLadiesCZ/pyladies.cz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyladies_cz.py
229 lines (175 loc) · 6.34 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
"""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 flask import Flask, render_template, url_for, send_from_directory
from flask import redirect
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/')
########
## Views
@app.route('/')
def index():
return render_template('index.html')
@app.route('/brno_info/')
def brno_info():
return render_template('brno_info.html')
@app.route('/praha_info/')
def praha_info():
return render_template('praha_info.html')
@app.route('/ostrava_info/')
def ostrava_info():
return render_template('ostrava_info.html')
@app.route('/praha_course/')
def praha_course():
return render_template('praha_course.html', meetups=read_meetups_yaml('meetups/praha.yml'))
@app.route('/brno_course/')
def brno_course():
return render_template('brno_course.html', meetups=read_meetups_yaml('meetups/brno.yml'))
@app.route('/ostrava_course/')
def ostrava_course():
return render_template('ostrava_course.html', meetups=read_meetups_yaml('meetups/ostrava.yml'))
@app.route('/brno/')
def brno():
return render_template('brno.html', plan=read_lessons_yaml('plans/brno.yml'))
@app.route('/praha/')
def praha():
return render_template('praha.html', plan=read_lessons_yaml('plans/praha.yml'))
@app.route('/ostrava/')
def ostrava():
return render_template('ostrava.html', plan=read_lessons_yaml('plans/ostrava.yml'))
@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):
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')
##########
## 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):
with open(filename, encoding='utf-8') as 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)
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()):
meetup['place']['url'] = (
'http://mapy.cz/zakladni?q={p[name]},'
'{p[latitude]}N+{p[longitude]}E'.format(p=meetup['place']))
# Figure out the status of registration
if 'registration' in meetup:
if 'end' in meetup['registration']:
if meetup['start'] <= today:
meetup['registration_status'] = 'meetup_started'
elif meetup['registration']['end'] >= today:
meetup['registration_status'] = 'running'
else:
meetup['registration_status'] = 'closed'
else:
meetup['registration_status'] = 'running'
return {
'current': [meetup for meetup in data
if ('end' not in meetup) or (meetup['end'] >= today)],
'past': [meetup for meetup in data
if ('end' in meetup) and (meetup['end'] < today)],
}
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(),
}
##########
## 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}
if __name__ == '__main__':
cli(app, freezer=freezer, base_url='http://pyladies.cz')