Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0] Add CORS headers on API #885

Merged
merged 3 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,9 @@ thinkhazard/static/build/%.css: $(LESS_FILES) .build/node_modules.timestamp

.build/venv:
mkdir -p $(dir $@)
# make a first virtualenv to get a recent version of virtualenv
virtualenv venv
venv/bin/pip install virtualenv
venv/bin/virtualenv .build/venv
# remove the temporary virtualenv
rm -rf venv
virtualenv --setuptools .build/venv
.build/venv/bin/pip install pip==8.1.1
.build/venv/bin/pip install setuptools==20.3.1

.build/node_modules.timestamp: package.json
mkdir -p $(dir $@)
Expand All @@ -215,7 +212,8 @@ thinkhazard/static/build/%.css: $(LESS_FILES) .build/node_modules.timestamp
.build/requirements.timestamp: .build/venv setup.py requirements.txt
mkdir -p $(dir $@)
.build/venv/bin/pip install numpy==1.10.1
.build/venv/bin/pip install -r requirements.txt
.build/venv/bin/pip install -r freeze.txt
.build/venv/bin/pip install -e .
touch $@

.build/flake8.timestamp: $(PY_FILES)
Expand Down
64 changes: 64 additions & 0 deletions freeze.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
affine==1.2.0
alembic==0.8.10
APScheduler==3.0.5
Babel==2.3.4
beautifulsoup4==4.4.1
Chameleon==3.1
click==6.3
click-plugins==1.0.3
cligj==0.4.0
colorlog==2.6.1
cssselect==0.9.1
enum34==1.1.2
futures==3.0.5
GeoAlchemy2==0.2.6
geojson==1.3.2
httplib2==0.9.2
Jinja2==2.9.5
lingua==4.12
lxml==3.6.0
Mako==1.0.4
Markdown==2.6.5
MarkupSafe==0.23
mock==3.0.5
nose==1.3.7
numpy==1.10.1
papyrus==2.0.dev3
Paste==2.0.2
PasteDeploy==1.5.2
polib==1.0.8
psycopg2==2.8.4
Pygments==2.1.3
pyparsing==2.1.0
pyproj==1.9.4
pyquery==1.2.9
pyramid==1.6a2
pyramid-debugtoolbar==2.4.2
pyramid-jinja2==2.6.2
pyramid-mako==1.0.2
pyramid-tm==0.12.1
python-editor==1.0
pytidylib==0.3.2
pytz==2016.2
PyYAML==3.11
rasterio==0.30.0
repoze.lru==0.6
requests==2.14.2
Shapely==1.5.13
simplejson==3.8.2
six==1.10.0
slugify==0.0.1
snuggs==1.3.1
SQLAlchemy==1.0.12
transaction==1.4.4
transifex-client==0.12.2
translationstring==1.3
tzlocal==1.2.2
urllib3==1.21.1
venusian==1.0
waitress==0.8.10
WebOb==1.6.0
WebTest==2.0.18
zope.deprecation==4.1.2
zope.interface==4.1.3
zope.sqlalchemy==0.7.6
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ lingua==4.12
transifex-client==0.12.2
jinja2==2.9.5
requests==2.14.2
zope.sqlalchemy==0.7.6
-e .
6 changes: 3 additions & 3 deletions thinkhazard/scripts/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def import_recommendations(argv=sys.argv):
# the other columns are hazard category (type / level)
for col_index in range(1, 28):
value = row[col_index]
if value is not '' and value is not 'Y':
if value != '' and value != 'Y':
association = hctra(order=value)
association.hazardcategory = categories[col_index - 1]
associations.append(association)
Expand Down Expand Up @@ -385,8 +385,8 @@ def import_contacts(argv=sys.argv):
url = unicode(row[10 + offset].decode('latin1'))
phone = unicode(row[11 + offset].decode('latin1'))
email = unicode(row[12 + offset].decode('latin1'))
if name is '' and url is '' and \
phone is '' and email is '':
if name == '' and url == '' and \
phone == '' and email == '':
continue

contact = DBSession.query(Contact) \
Expand Down
26 changes: 24 additions & 2 deletions thinkhazard/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,28 @@
HazardLevel,
)

cors_policy = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
}

@view_config(route_name='api_hazardcategory', renderer='json')

def cors_headers(cors_policy):

def add_cors_headers(view_callable):

def wrapper(context, request):
request.response.headers.update(cors_policy)
return view_callable(context, request)

return wrapper

return add_cors_headers


@view_config(route_name='api_hazardcategory',
decorator=cors_headers(cors_policy),
renderer='json')
def api_hazardcategory(request):
hazard_type = request.matchdict['hazard_type']
hazard_level = request.matchdict['hazard_level']
Expand All @@ -54,7 +74,9 @@ def api_hazardcategory(request):
}


@view_config(route_name='api_admindiv_hazardsets_hazardtype', renderer='json')
@view_config(route_name='api_admindiv_hazardsets_hazardtype',
decorator=cors_headers(cors_policy),
renderer='json')
def api_admindiv_hazardsets_hazardtype(request):
data = admindiv_hazardsets_hazardtype(request)
return data