Skip to content

Commit fb81024

Browse files
author
Michael Kelly
committed
Bug 1088752: Add Shape of the Web app to serve JSON.
Shape of the Web is a standalone site that pulls data from JSON files. In order to make the l10n easier initially, we’re going to host the JSON files on Bedrock to take advantage of the existing l10n process. Also adds a middleware to handle adding CORS headers to the locale redirects when loading the JSON.
1 parent 305192a commit fb81024

File tree

12 files changed

+6019
-0
lines changed

12 files changed

+6019
-0
lines changed

bedrock/mozorg/middleware.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import datetime
66
from email.utils import formatdate
7+
import re
78
import time
89

910
from django.conf import settings
@@ -45,3 +46,17 @@ def process_view(self, request, view, view_args, view_kwargs):
4546
else:
4647
f = super(MozorgRequestTimingMiddleware, self)
4748
f.process_view(request, view, view_args, view_kwargs)
49+
50+
51+
class CrossOriginResourceSharingMiddleware(object):
52+
53+
def process_response(self, request, response):
54+
"""
55+
If the URL pattern for the request matches one of those
56+
in the CORS_URLS setting, apply the matching
57+
Access-Control-Allow-Origin header to the response.
58+
"""
59+
for pattern, origin in settings.CORS_URLS.items():
60+
if re.search(pattern, request.path):
61+
response['Access-Control-Allow-Origin'] = origin
62+
return response
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
from django.http import HttpRequest, HttpResponse
5+
6+
from bedrock.mozorg.middleware import CrossOriginResourceSharingMiddleware
7+
from bedrock.mozorg.tests import TestCase
8+
9+
10+
class TestCrossOriginResourceSharingMiddleware(TestCase):
11+
def setUp(self):
12+
self.middleware = CrossOriginResourceSharingMiddleware()
13+
self.request = HttpRequest()
14+
self.response = HttpResponse()
15+
16+
def test_match(self):
17+
self.request.path = '/foo/bar/baz/'
18+
19+
cors_urls = {r'^/foo/bar': '*'}
20+
with self.settings(CORS_URLS=cors_urls):
21+
self.middleware.process_response(self.request, self.response)
22+
self.assertEqual(self.response['Access-Control-Allow-Origin'], '*')
23+
24+
def test_middle_match(self):
25+
# Ensure that matches in the middle of the URL work.
26+
self.request.path = '/foo/bar/baz/'
27+
28+
cors_urls = {r'/bar': '*'}
29+
with self.settings(CORS_URLS=cors_urls):
30+
self.middleware.process_response(self.request, self.response)
31+
self.assertEqual(self.response['Access-Control-Allow-Origin'], '*')
32+
33+
def test_no_match(self):
34+
self.request.path = '/foo/bar/baz/'
35+
36+
cors_urls = {r'^/biff/bak': '*'}
37+
with self.settings(CORS_URLS=cors_urls):
38+
self.middleware.process_response(self.request, self.response)
39+
self.assertFalse('Access-Control-Allow-Origin' in self.response)

bedrock/settings/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,7 @@ def JINJA_CONFIG():
10571057
'dnt.middleware.DoNotTrackMiddleware',
10581058
'lib.l10n_utils.middleware.FixLangFileTranslationsMiddleware',
10591059
'bedrock.mozorg.middleware.ConditionalAuthMiddleware',
1060+
'bedrock.mozorg.middleware.CrossOriginResourceSharingMiddleware',
10601061
))
10611062

10621063
AUTHENTICATED_URL_PREFIXES = ('/admin/', '/rna/')
@@ -1106,6 +1107,7 @@ def JINJA_CONFIG():
11061107
'%s.events' % PROJECT_MODULE,
11071108
'%s.releasenotes' % PROJECT_MODULE,
11081109
'%s.thunderbird' % PROJECT_MODULE,
1110+
'%s.shapeoftheweb' % PROJECT_MODULE,
11091111

11101112
# libs
11111113
'django_extensions',
@@ -1416,6 +1418,10 @@ def facebook_tab_url_lazy():
14161418
MOFO_SECURITY_ADVISORIES_PATH = path('mofo_security_advisories')
14171419
MOFO_SECURITY_ADVISORIES_REPO = 'https://github.com/mozilla/foundation-security-advisories.git'
14181420

1421+
CORS_URLS = {
1422+
r'^/([a-zA-Z-]+/)?shapeoftheweb': '*',
1423+
}
1424+
14191425
LOGGING = {
14201426
'root': {
14211427
'level': 'WARNING',

bedrock/shapeoftheweb/__init__.py

Whitespace-only changes.

bedrock/shapeoftheweb/models.py

Whitespace-only changes.

0 commit comments

Comments
 (0)