This repository has been archived by the owner on Jul 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapp.py
516 lines (398 loc) · 13.4 KB
/
app.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# Core
import dateutil.parser
from datetime import datetime
from urllib.parse import urlparse, urlunparse, unquote
# Third-party
import flask
import talisker.flask
import talisker.logs
import talisker.requests
import xmltodict
from dateutil.relativedelta import relativedelta
# Local
import api
import feeds
import helpers
import redirects
INSIGHTS_ADMIN_URL = "https://admin.insights.ubuntu.com"
app = flask.Flask(__name__)
app.jinja_env.filters["monthname"] = helpers.monthname
app.url_map.strict_slashes = False
app.url_map.converters["regex"] = helpers.RegexConverter
talisker.flask.register(app)
talisker.logs.set_global_extra({"service": "blog.ubuntu.com"})
if not app.testing:
talisker.requests.configure(feeds.cached_session)
apply_redirects = redirects.prepare_redirects(
permanent_redirects_path="permanent-redirects.yaml",
redirects_path="redirects.yaml",
)
app.before_request(apply_redirects)
def _tag_view(tag_slug, page_slug, template):
"""
View function which gets all posts for a given tag,
and returns a response loading those posts with the template provided
"""
page = helpers.to_int(flask.request.args.get("page"), default=1)
tags = api.get_tags(slugs=[tag_slug])
if not tags:
flask.abort(404)
tag = tags[0]
posts, total_posts, total_pages = helpers.get_formatted_expanded_posts(
tag_ids=[tag["id"]], page=page
)
return flask.render_template(
template,
posts=posts,
tag=tag,
current_page=page,
page_slug=page_slug,
total_posts=total_posts,
total_pages=total_pages,
)
def _group_view(group_slug, page_slug, template):
"""
View function which gets all posts for a given group slug,
and returns a response loading those posts with the template provided
"""
page = int(flask.request.args.get("page") or "1")
category_slug = flask.request.args.get("category")
groups = api.get_groups(slugs=[group_slug])
category = None
if not groups:
flask.abort(404)
group = groups[0]
if category_slug:
categories = api.get_categories(slugs=[category_slug])
if categories:
category = categories[0]
posts, total_posts, total_pages = helpers.get_formatted_expanded_posts(
group_ids=[group["id"]],
category_ids=[category["id"]] if category else [],
page=page,
per_page=12,
)
return flask.render_template(
template,
posts=posts,
group=group,
category=category if category_slug else None,
current_page=page,
page_slug=page_slug,
total_posts=total_posts,
total_pages=total_pages,
)
@app.before_request
def clear_trailing():
"""
Remove trailing slashes from all routes
We like our URLs without slashes
"""
parsed_url = urlparse(unquote(flask.request.url))
path = parsed_url.path
if path != "/" and path.endswith("/"):
new_uri = urlunparse(parsed_url._replace(path=path[:-1]))
return flask.redirect(new_uri)
@app.route("/status")
def status():
"""
A simple response to test that the app is alive and working.
This can be targeted by Kubernetes readiness and liveness checks.
As used in snapcraft.io:
https://github.com/canonical-websites/snapcraft.io/pull/327/files
"""
return "alive"
@app.route("/")
def homepage():
category_slug = flask.request.args.get("category")
category = None
sticky_posts, _, _ = helpers.get_formatted_expanded_posts(sticky=True)
featured_posts = sticky_posts[:3] if sticky_posts else None
page = helpers.to_int(flask.request.args.get("page"), default=1)
posts_per_page = 12
upcoming_categories = api.get_categories(slugs=["events", "webinars"])
upcoming_category_ids = []
for upcoming_category_id in upcoming_categories:
upcoming_category_ids.append(upcoming_category_id["id"])
upcoming_events, _, _ = helpers.get_formatted_expanded_posts(
per_page=3, category_ids=upcoming_category_ids
)
if category_slug:
categories = api.get_categories(slugs=[category_slug])
if categories:
category = categories[0]
posts, total_posts, total_pages = helpers.get_formatted_expanded_posts(
per_page=posts_per_page,
category_ids=[category["id"]] if category else [],
page=page,
sticky=False,
)
# Manipulate the posts to add a newsletter placeholder
if page == 1:
print("page: " + str(page))
posts.insert(2, "newsletter")
posts.pop(11)
return flask.render_template(
"index.html",
posts=posts,
category=category,
current_page=page,
total_posts=total_posts,
total_pages=total_pages,
featured_posts=featured_posts,
upcoming_events=upcoming_events,
)
@app.route("/search")
def search():
query = flask.request.args.get("q") or ""
page = helpers.to_int(flask.request.args.get("page"), default=1)
posts = []
total_pages = None
total_posts = None
if query:
posts, total_posts, total_pages = helpers.get_formatted_posts(
query=query, page=page
)
return flask.render_template(
"search.html",
posts=posts,
query=query,
current_page=page,
total_posts=total_posts,
total_pages=total_pages,
)
@app.route("/press-centre")
def press_centre():
group = api.get_groups(slugs=["canonical-announcements"])[0]
posts, total_posts, total_pages = helpers.get_formatted_expanded_posts(
group_ids=[group["id"]]
)
return flask.render_template(
"press-centre.html",
posts=posts,
page_slug="press-centre",
group=group,
current_year=datetime.now().year,
)
@app.route("/cloud-and-server")
def cloud_and_server():
return _group_view(
page_slug="cloud-and-server",
group_slug="cloud-and-server",
template="cloud-and-server.html",
)
@app.route("/internet-of-things")
def internet_of_things():
return _group_view(
page_slug="internet-of-things",
group_slug="internet-of-things",
template="internet-of-things.html",
)
@app.route("/desktop")
def desktop():
return _group_view(
page_slug="desktop", group_slug="desktop", template="desktop.html"
)
@app.route("/tag/<slug>")
def tag(slug):
return _tag_view(tag_slug=slug, page_slug="tag", template="tag.html")
@app.route("/topics/design")
def design():
return _tag_view(
tag_slug="design", page_slug="topics", template="topics/design.html"
)
@app.route("/topics/juju")
def juju():
return _tag_view(
tag_slug="juju", page_slug="topics", template="topics/juju.html"
)
@app.route("/topics/maas")
def maas():
return _tag_view(
tag_slug="maas", page_slug="topics", template="topics/maas.html"
)
@app.route("/topics/snappy")
def snappy():
return _tag_view(
tag_slug="snappy", page_slug="topics", template="topics/snappy.html"
)
@app.route("/topics/robotics")
def robotics():
return _tag_view(
tag_slug="robotics",
page_slug="topics",
template="topics/robotics.html",
)
@app.route("/archives")
def archives():
page = helpers.to_int(flask.request.args.get("page"), default=1)
year = helpers.to_int(flask.request.args.get("year"))
month = helpers.to_int(flask.request.args.get("month"))
group_slug = flask.request.args.get("group")
category_slug = flask.request.args.get("category")
if month and month > 12:
month = None
friendly_date = None
group = None
after = None
before = None
if year:
if month:
after = datetime(year=year, month=month, day=1)
before = after + relativedelta(months=1)
friendly_date = after.strftime("%B %Y")
if not month:
after = datetime(year=year, month=1, day=1)
before = after + relativedelta(years=1)
friendly_date = after.strftime("%Y")
if group_slug:
groups = api.get_groups(slugs=[group_slug])
if groups:
group = groups[0]
if category_slug:
categories = api.get_categories(slugs=[category_slug])
category_ids = [category["id"] for category in categories]
else:
categories = []
category_ids = []
posts, total_posts, total_pages = helpers.get_formatted_posts(
page=page,
after=after,
before=before,
group_ids=[group["id"]] if group else [],
category_ids=category_ids if category_ids else [],
)
return flask.render_template(
"archives.html",
categories=categories,
category_ids=category_ids,
category_slug=category_slug if category_slug else None,
current_page=page,
friendly_date=friendly_date,
group=group,
now=datetime.now(),
posts=posts,
total_pages=total_pages,
total_posts=total_posts,
)
@app.route("/<type>/<slug>/feed")
@app.route("/<slug>/feed")
@app.route("/feed")
def feed(type=None, slug=None): # noqa
feed_url = "".join([INSIGHTS_ADMIN_URL, flask.request.full_path])
feed_text = feeds.cached_request(feed_url).text
feed_text = feed_text.replace(
"admin.insights.ubuntu.com", "insights.ubuntu.com"
)
feed = xmltodict.parse(feed_text)
if (
"rss" in feed
and "channel" in feed["rss"]
and "item" in feed["rss"]["channel"]
):
indexes_to_delete = []
for index, item in enumerate(feed["rss"]["channel"]["item"]):
if "category" in item:
for category in item["category"]:
if "lang:cn" in category or "lang:jp" in category:
indexes_to_delete.append(index)
# the original dict will change in size
# so whenever we remove an item we need to decrease
# all following index accesses by 1
for index, index_to_delete_at in enumerate(indexes_to_delete):
temp = dict(feed)
del temp["rss"]["channel"]["item"][index_to_delete_at - index]
feed = temp
feed = xmltodict.unparse(feed, pretty=True)
return flask.Response(feed, mimetype="text/xml")
@app.route("/author/<slug>")
def user(slug):
authors = api.get_users(slugs=[slug])
page = helpers.to_int(flask.request.args.get("page"), default=1)
if not authors:
flask.abort(404)
author = authors[0]
posts, total_posts, total_pages = helpers.get_formatted_expanded_posts(
author_ids=[author["id"]], page=page
)
return flask.render_template(
"author.html",
author=author,
posts=posts,
current_page=page,
total_posts=total_posts,
total_pages=total_pages,
)
@app.route(
'/<regex("[0-9]{4}"):year>/<regex("[0-9]{2}"):month>/'
'<regex("[0-9]{2}"):day>/<slug>'
)
@app.route('/<regex("[0-9]{4}"):year>/<regex("[0-9]{2}"):month>/<slug>')
@app.route('/<regex("[0-9]{4}"):year>/<slug>')
@app.route("/webinar/<slug>")
@app.route("/<slug>")
def post(slug, year=None, month=None, day=None):
posts, total_posts, total_pages = helpers.get_formatted_posts(slugs=[slug])
if not posts:
flask.abort(404)
if not (day and month and year):
pubdate = dateutil.parser.parse(posts[0]["date_gmt"])
day = pubdate.strftime("%d")
month = pubdate.strftime("%m")
year = pubdate.strftime("%Y")
return flask.redirect(
"/{year}/{month}/{day}/{slug}".format(**locals())
)
post = posts[0]
topics = api.get_topics(post_id=post["id"])
if topics:
post["topic"] = topics[0]
tags = api.get_tags(post_id=post["id"])
related_posts, total_posts, total_pages = helpers.get_formatted_posts(
tag_ids=[tag["id"] for tag in tags], per_page=3, exclude=post["id"]
)
# Even though we're filtering tags below, we need to know the snapcraft.io
# tag, specifically to add the canonical meta tag
snapcraft_io_tag = list(filter(lambda tag: tag["id"] == 2996, tags))
if snapcraft_io_tag:
canonical_link = "https://snapcraft.io/blog/" + slug
else:
canonical_link = None
display_tags = helpers.filter_tags_for_display(tags)
return flask.render_template(
"post.html",
post=post,
tags=display_tags,
related_posts=related_posts,
canonical_link=canonical_link,
)
@app.route("/upcoming")
def upcoming():
page = helpers.to_int(flask.request.args.get("page"), default=1)
posts_per_page = 12
upcoming_categories = api.get_categories(slugs=["events", "webinars"])
upcoming_category_ids = []
for upcoming_category_id in upcoming_categories:
upcoming_category_ids.append(upcoming_category_id["id"])
upcoming_events, _, _ = helpers.get_formatted_expanded_posts(
per_page=3, category_ids=upcoming_category_ids
)
posts, total_posts, total_pages = helpers.get_formatted_expanded_posts(
per_page=posts_per_page, category_ids=upcoming_category_ids, page=page
)
return flask.render_template(
"upcoming.html",
posts=posts,
current_page=page,
total_posts=total_posts,
total_pages=total_pages,
)
@app.errorhandler(404)
def page_not_found(e):
return flask.render_template("404.html"), 404
@app.errorhandler(410)
def page_deleted(e):
return flask.render_template("410.html"), 410
@app.errorhandler(500)
def server_error(e):
return flask.render_template("500.html"), 500