Widget: Follow redirects

This commit is contained in:
Raphael Michel
2019-01-22 18:06:56 +01:00
parent 15a190cdf3
commit a031d72ca9
2 changed files with 36 additions and 13 deletions

View File

@@ -60,7 +60,9 @@ def _detect_event(request, require_live=True, require_plugin=None):
if request.port and request.port not in (80, 443): if request.port and request.port not in (80, 443):
domain = '%s:%d' % (domain, request.port) domain = '%s:%d' % (domain, request.port)
path = request.get_full_path().split("/", 2)[-1] path = request.get_full_path().split("/", 2)[-1]
return redirect(urljoin('%s://%s' % (request.scheme, domain), path)) r = redirect(urljoin('%s://%s' % (request.scheme, domain), path))
r['Access-Control-Allow-Origin'] = '*'
return r
if hasattr(request, 'event'): if hasattr(request, 'event'):
# Restrict locales to the ones available for this event # Restrict locales to the ones available for this event

View File

@@ -68,9 +68,9 @@ var api = {
xhr.onload = function (e) { xhr.onload = function (e) {
if (xhr.readyState === 4) { if (xhr.readyState === 4) {
if (xhr.status === 200) { if (xhr.status === 200) {
callback(JSON.parse(xhr.responseText)); callback(JSON.parse(xhr.responseText), xhr);
} else { } else {
console.error(xhr.statusText); err_callback(xhr, e);
} }
} }
}; };
@@ -103,12 +103,11 @@ var api = {
if (xhr.status === 200) { if (xhr.status === 200) {
callback(JSON.parse(xhr.responseText)); callback(JSON.parse(xhr.responseText));
} else { } else {
console.error(xhr.statusText); err_callback(xhr, e);
} }
} }
}; };
xhr.onerror = function (e) { xhr.onerror = function (e) {
console.error(xhr.statusText);
err_callback(xhr, e); err_callback(xhr, e);
}; };
xhr.send(params); xhr.send(params);
@@ -393,7 +392,9 @@ Vue.component('category', {
var shared_methods = { var shared_methods = {
buy: function (event) { buy: function (event) {
if (this.$root.useIframe) { if (this.$root.useIframe) {
event.preventDefault(); if (event) {
event.preventDefault();
}
} else { } else {
return; return;
} }
@@ -408,6 +409,13 @@ var shared_methods = {
} }
}, },
buy_error_callback: function (xhr, data) { buy_error_callback: function (xhr, data) {
if (xhr.status === 405) {
// Likely a redirect!
this.$root.event_url = xhr.responseURL.substr(0, xhr.responseURL.indexOf("/cart/add") - 18);
this.$root.overlay.frame_loading = false;
this.buy();
return;
}
this.$root.overlay.error_message = strings['cart_error']; this.$root.overlay.error_message = strings['cart_error'];
this.$root.overlay.frame_loading = false; this.$root.overlay.frame_loading = false;
}, },
@@ -422,8 +430,10 @@ var shared_methods = {
buy_callback: function (data) { buy_callback: function (data) {
if (data.redirect) { if (data.redirect) {
var iframe = this.$root.overlay.$children[0].$refs['frame-container'].children[0]; var iframe = this.$root.overlay.$children[0].$refs['frame-container'].children[0];
this.$root.cart_id = data.cart_id; if (data.cart_id) {
setCookie(this.$root.cookieName, data.cart_id, 30); this.$root.cart_id = data.cart_id;
setCookie(this.$root.cookieName, data.cart_id, 30);
}
if (data.redirect.substr(0, 1) === '/') { if (data.redirect.substr(0, 1) === '/') {
data.redirect = this.$root.event_url.replace(/^([^\/]+:\/\/[^\/]+)\/.*$/, "$1") + data.redirect; data.redirect = this.$root.event_url.replace(/^([^\/]+:\/\/[^\/]+)\/.*$/, "$1") + data.redirect;
} }
@@ -676,7 +686,16 @@ var shared_root_methods = {
url += "&cart_id=" + cart_id; url += "&cart_id=" + cart_id;
} }
var root = this.$root; var root = this.$root;
api._getJSON(url, function (data) { api._getJSON(url, function (data, xhr) {
if (xhr.responseURL !== url) {
var new_url = xhr.responseURL.substr(0, xhr.responseURL.indexOf("/widget/product_list?") + 1);
if (root.subevent) {
new_url = new_url.substr(0, new_url.lastIndexOf("/", new_url.length - 1) + 1);
}
root.event_url = new_url;
root.reload();
return;
}
root.categories = data.items_by_category; root.categories = data.items_by_category;
root.currency = data.currency; root.currency = data.currency;
root.display_net_prices = data.display_net_prices; root.display_net_prices = data.display_net_prices;
@@ -708,8 +727,9 @@ var shared_root_computed = {
}, },
voucherFormTarget: function () { voucherFormTarget: function () {
var form_target = this.event_url + 'w/' + widget_id + '/redeem?iframe=1&locale=' + lang; var form_target = this.event_url + 'w/' + widget_id + '/redeem?iframe=1&locale=' + lang;
if (getCookie(this.cookieName)) { var cookie = getCookie(this.cookieName);
form_target += "&take_cart_id=" + getCookie(this.cookieName); if (cookie) {
form_target += "&take_cart_id=" + cookie;
} }
if (this.subevent) { if (this.subevent) {
form_target += "&subevent=" + this.subevent; form_target += "&subevent=" + this.subevent;
@@ -722,8 +742,9 @@ var shared_root_computed = {
checkout_url += "checkout/start"; checkout_url += "checkout/start";
} }
var form_target = this.event_url + 'w/' + widget_id + '/cart/add?iframe=1&next=' + encodeURIComponent(checkout_url); var form_target = this.event_url + 'w/' + widget_id + '/cart/add?iframe=1&next=' + encodeURIComponent(checkout_url);
if (getCookie(this.cookieName)) { var cookie = getCookie(this.cookieName);
form_target += "&take_cart_id=" + getCookie(this.cookieName); if (cookie) {
form_target += "&take_cart_id=" + cookie;
} }
return form_target return form_target
}, },