Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #612 from TakwimuAfrica/fix/cors_use_same_origin-1…
Browse files Browse the repository at this point in the history
…68169159

Enable same-origin for HURUmap & Flourish Viz
  • Loading branch information
kilemensi authored Sep 12, 2019
2 parents cb1d7a8 + b25a4f1 commit f2b39e4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ function DataContainer({ id, data, theme, countryName }) {
data.data_stat_type
}&chartSourceLink=${data.data_source_link}&chartSourceTitle=${
data.data_source_title
}&chartQualifier=${data.chart_qualifier
.replace('<br/>', '%0A')
.replace(/<(.|\n)*?>/g, '')}&stylesheet=/static/css/embedchart.css"
}&chartQualifier=${
data.chart_qualifier
? data.chart_qualifier.replace('<br/>', '%0A').replace(/<(.|\n)*?>/g, '')
: ''
}&stylesheet=/static/css/embedchart.css"
/>`;

return (
Expand Down
4 changes: 2 additions & 2 deletions takwimu/takwimu_ui/src/components/DataContainer/IFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ function IFrame({ id, classes, data }) {
chartSourceLink: data.data_source_link,
chartSourceTitle: data.data_source_title,
chartQualifier: data.chart_qualifier
.replace(/<br[ /]*>/g, '\n')
.replace(/<[^>]*>/g, '')
? data.chart_qualifier.replace(/<br[ /]*>/g, '\n').replace(/<[^>]*>/g, '')
: ''
};
const queryString = Object.keys(params)
.map(k => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`)
Expand Down
3 changes: 3 additions & 0 deletions takwimu/takwimu_ui/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,6 @@ renderSearchResultsPage();
renderLegalPage();
render500Page();
render404Page();

// Same-origin policy
document.domain = new URL(PROPS.takwimu.url).hostname;
3 changes: 3 additions & 0 deletions takwimu/templates/settings_js.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
{{ block.super }}

var STATIC_PREFIX = '{% get_static_prefix %}';

// Same-origin policy for embeds
document.domain = new URL(SITE_URL).hostname;
{% endblock settings_javascript %}
14 changes: 13 additions & 1 deletion takwimu/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from wsgiref.util import FileWrapper

import requests
import bs4

from django.conf import settings as takwimu_settings
from django.core.serializers.json import DjangoJSONEncoder
from django.shortcuts import get_object_or_404, render
Expand Down Expand Up @@ -365,8 +367,18 @@ def get(self, request, *args, **kwargs):
file_path = zip_ref.extract(member, "/tmp/" + kwargs["document_id"])
zip_ref.close()
mode, content_type = ('r', 'text')
if not member.split('/')[-1].endswith( ('html', 'css', 'txt', 'svg')):
if not member.split('/')[-1].endswith(('html', 'css', 'txt', 'svg')):
mode, content_type = ('rb', 'media/*')

if member == 'index.html':
soup = bs4.BeautifulSoup(open(file_path).read())
# Same-origin policy
script_tag = soup.new_tag("script", type="text/javascript")
script_tag.append(
'document.domain = new URL("{}").hostname;'.format(takwimu_settings.HURUMAP['url'])
)
soup.head.append(script_tag)
return Response(str(soup))
return Response(open(file_path).read())


Expand Down

0 comments on commit f2b39e4

Please sign in to comment.