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

Use JSONP for sustainability API #3789

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions readthedocs/core/signals.py
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
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 get_data = {format: "jsonp"};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just call this data or request_data, so it doesn't require changing based on get/post? Not important, though :)

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);
}
}

get_data.div_ids = div_ids.join('|');
get_data.display_types = display_types.join('|');
get_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');
get_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');
get_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: get_data,
success: function (data) {
var promo;
if (data && data.div_id && data.html) {
Expand Down