forked from pyvec/python.cz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
242 lines (176 loc) · 6.66 KB
/
Copy pathviews.py
File metadata and controls
242 lines (176 loc) · 6.66 KB
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
import logging
import os
import subprocess
import itertools
from fnmatch import fnmatch
from urllib.parse import quote_plus as url_quote_plus
from arrow import Arrow
from flask import (render_template as _render_template, url_for,
request, make_response, send_from_directory, Response)
from pythoncz import app, freezer
from pythoncz.models import jobs, beginners, github, events, meetups
INDEX_EVENTS_LIMIT = 3
logger = logging.getLogger(__name__)
# Templating
def render_template(filename, **kwargs):
kwargs['template_url'] = app.config['TEMPLATES_DIR_URL'] + filename
return _render_template(filename, **kwargs)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.template_filter('urlencode')
def urlencode_filter(s):
return url_quote_plus(str(s).encode('utf8'))
@app.template_filter('format_dt')
def format_dt_filter(dt: Arrow, fmt):
return dt.to('Europe/Prague').strftime(fmt)
@app.template_filter('format_dt_iso')
def format_dt_iso_filter(dt: Arrow):
return dt.to('Europe/Prague').isoformat()
@app.template_filter('format_month')
def format_month_filter(month, lang='cs'):
months = {'en': [None, 'January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September', 'October',
'November', 'December'],
'cs': [None, 'Leden', 'Únor', 'Březen', 'Duben', 'Květen',
'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen',
'Listopad', 'Prosinec']}
return months[lang][month]
@app.template_filter('format_weekday')
def format_weekday_filter(weekday, lang='cs'):
weekdays = {'en': ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday'],
'cs': ['neděle', 'pondělí', 'úterý', 'středa', 'čtvrtek',
'pátek', 'sobota']}
return weekdays[lang][weekday]
@app.template_filter('format_weekday_short')
def format_weekday_short_filter(weekday, lang='cs'):
weekdays = {'en': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
'cs': ['ne', 'po', 'út', 'st', 'čt', 'pá', 'so']}
return weekdays[lang][weekday]
@app.context_processor
def inject_context():
return {
'debug': app.debug,
'config': app.config,
'url': request.url,
'lang': 'cs',
}
def by_month(event):
return (event['event'].begin.year, event['event'].begin.month)
# Regular views
@app.route('/')
def index_cs():
data = itertools.groupby(events.data[:INDEX_EVENTS_LIMIT], key=by_month)
return render_template('index_cs.html', data=data)
@app.route('/en/')
def index_en():
data = itertools.groupby(events.data[:INDEX_EVENTS_LIMIT], key=by_month)
return render_template('index_en.html', lang='en', data=data)
@app.route('/zacatecnici/')
def beginners_cs():
return render_template('beginners_cs.html', data=beginners.data)
@app.route('/prace/')
def jobs_cs():
return render_template('jobs_cs.html', data=jobs.data)
@app.route('/en/jobs/')
def jobs_en():
return render_template('jobs_en.html', data=jobs.data, lang='en')
@app.route('/akce/')
def events_cs():
return render_template('events_cs.html',
data=itertools.groupby(events.data, key=by_month),
meetups=meetups.get_meetups())
@app.route('/en/events/')
def events_en():
return render_template('events_en.html', lang='en',
data=itertools.groupby(events.data, key=by_month),
meetups=meetups.get_meetups(lang='en'))
@app.route('/events.ics')
def events_ical():
return Response(str(events.get_calendar()), mimetype='text/calendar')
@app.route('/zapojse/')
def get_involved_cs():
disabled = os.getenv('DISABLE_GITHUB_ISSUES_FETCH', False)
issues = []
error = None
if not disabled:
try:
issues = github.get_issues(app.config['GITHUB_ORGANIZATIONS'])
except Exception as e:
logger.exception('Failed to fetch GitHub issues')
error = e
template = render_template('get_involved_cs.html', issues=issues, error=error)
return make_response(template)
# Redirects of legacy stuff
def redirect(url, code=None):
"""Return a response with a Meta redirect, code is unused"""
# 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)
@app.route('/english.html')
def index_en_legacy():
return redirect(url_for('index_en'), code=301)
@app.route('/pyladies/<path:target>')
def pyladies(target):
return redirect('http://pyladies.cz/v1/' + target, code=301)
@freezer.register_generator
def pyladies():
# This is hardcoded because it doesn't change & it's easier to hardcode it
targets = (
's001-install/',
's002-hello-world/',
's003-looping/',
's004-strings/',
's005-modules/',
's006-lists/',
's007-cards/',
's008-cards2/',
's009-git/',
's010-data/',
's011-dicts/',
's012-pyglet/',
's014-class/',
's015-asteroids/',
's016-micropython/',
)
yield from ({'target': t} for t in targets)
@app.route('/pyladies/')
def pyladies_index():
return redirect('http://pyladies.cz/', code=301)
# Talks, this used to redirect, but that's not possible with *.pdf HTML files
talks_dir = 'talks-archive'
@app.before_first_request
def clone_talks():
try:
os.chdir(talks_dir)
except FileNotFoundError:
subprocess.run(('git', 'clone',
'https://github.com/pyvec/talks-archive',
'--depth', '1'))
os.chdir(talks_dir)
else:
subprocess.run(('git', 'fetch', 'origin'), check=True)
subprocess.run(('git', 'reset', '--hard', 'origin/master'), check=True)
finally:
os.chdir('..')
@app.route('/talks/<path:target>')
def talks(target):
# sends from pythoncz directory, hence ../
return send_from_directory(f'../{talks_dir}', target)
@freezer.register_generator
def talks():
ignore = ['.travis.yml', '.gitignore']
for name, dirs, files in os.walk(talks_dir):
if '.git' in dirs:
dirs.remove('.git')
for file in files:
if file == '.git':
continue
if not any(fnmatch(file, ig) for ig in ignore):
path = os.path.relpath(os.path.join(name, file), talks_dir)
yield {'target': path}