Skip to content

Commit

Permalink
Merge pull request #3789 from davidfischer/sustainability-api-jsonp
Browse files Browse the repository at this point in the history
Use JSONP for sustainability API
  • Loading branch information
davidfischer authored Mar 26, 2018
2 parents 7b8efe3 + 24ee5e5 commit ca35951
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
10 changes: 5 additions & 5 deletions readthedocs/core/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

log = logging.getLogger(__name__)

WHITELIST_URLS = ['/api/v2/footer_html', '/api/v2/search',
'/api/v2/docsearch', '/api/v2/sustainability']
WHITELIST_URLS = [
'/api/v2/footer_html',
'/api/v2/search',
'/api/v2/docsearch',
]


webhook_github = Signal(providing_args=['project', 'data', 'event'])
Expand All @@ -42,9 +45,6 @@ def decide_if_cors(sender, request, **kwargs): # pylint: disable=unused-argumen
for url in WHITELIST_URLS:
if request.path_info.startswith(url):
valid_url = True
# Don't do domain checking for this API for now
if url == '/api/v2/sustainability':
return True

if valid_url:
project_slug = request.GET.get('project', None)
Expand Down
48 changes: 21 additions & 27 deletions readthedocs/core/static-src/core/js/doc-embed/sponsorship.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,6 @@ function create_footer_placement() {
return null;
}

/*
* Returns an array of possible places where a promo could go
*/
function get_placements() {
var placements = [];
var placement_funcs = [create_footer_placement, create_sidebar_placement];
var placement;

for (var i = 0; i < placement_funcs.length; i += 1) {
placement = placement_funcs[i]();
if (placement) {
placements.push(placement);
}
}

return placements;
}

function Promo(data) {
this.id = data.id; // analytics id
this.div_id = data.div_id || '';
Expand Down Expand Up @@ -128,41 +110,53 @@ Promo.prototype.post_promo_display = function () {
};

function init() {
var post_data = {};
var request_data = {format: "jsonp"};
var div_ids = [];
var display_types = [];
var placement_funcs = [create_footer_placement, create_sidebar_placement];
var params;
var placement;

rtd = rtddata.get();

if (!rtd.show_promo()) {
return;
}

post_data.placements = get_placements(rtd);
post_data.project = rtd.project;
for (var i = 0; i < placement_funcs.length; i += 1) {
placement = placement_funcs[i]();
if (placement) {
div_ids.push(placement.div_id);
display_types.push(placement.display_type);
}
}

request_data.div_ids = div_ids.join('|');
request_data.display_types = display_types.join('|');
request_data.project = rtd.project;

if (typeof URL !== 'undefined' && typeof URLSearchParams !== 'undefined') {
// Force a specific promo to be displayed
params = new URL(window.location).searchParams;
if (params.get('force_promo')) {
post_data.force_promo = params.get('force_promo');
request_data.force_promo = params.get('force_promo');
}

// Force a promo from a specific campaign
if (params.get('force_campaign')) {
post_data.force_campaign = params.get('force_campaign');
request_data.force_campaign = params.get('force_campaign');
}
}

// Request a promo to inject onto the page
$.ajax({
url: rtd.api_host + "/api/v2/sustainability/",
type: 'post',
crossDomain: true,
xhrFields: {
withCredentials: true,
},
dataType: "json",
data: JSON.stringify(post_data),
contentType: "application/json",
dataType: "jsonp",
data: request_data,
success: function (data) {
var promo;
if (data && data.div_id && data.html) {
Expand Down

0 comments on commit ca35951

Please sign in to comment.