0.12.50 subscribe page w dynamically load

This commit is contained in:
SDE
2024-05-07 13:51:20 +03:00
parent e8e4bf466b
commit a0ee72bc81
2 changed files with 40 additions and 30 deletions

View File

@@ -59,12 +59,13 @@ def get_profile_subscribe_page_content_html(request):
subscribe_for_user = None subscribe_for_user = None
orders = None
if request.user and request.user.is_authenticated: if request.user and request.user.is_authenticated:
from BillingApp.funcs import get_orders_for_user, get_order_status from BillingApp.funcs import get_orders_for_user, get_order_status
orders = get_orders_for_user(request.user) orders = get_orders_for_user(request.user)
for order in orders: # for order in orders:
res = get_order_status(order) # res = get_order_status(order)
subscribe_for_user = check_n_enable_subscribe_by_order(order) # subscribe_for_user = check_n_enable_subscribe_by_order(order)
if not subscribe_for_user: if not subscribe_for_user:
subscribe_for_user = SubscribeForUser.objects.filter(user=request.user) subscribe_for_user = SubscribeForUser.objects.filter(user=request.user)
@@ -84,7 +85,8 @@ def get_profile_subscribe_page_content_html(request):
Dict = { Dict = {
'subscribe_for_user': subscribe_for_user, 'subscribe_for_user': subscribe_for_user,
'subscribes': subscribes 'subscribes': subscribes,
'active_orders': orders,
} }
html = render_to_string(tpl_name, Dict, request=request) html = render_to_string(tpl_name, Dict, request=request)

View File

@@ -34,34 +34,42 @@ const showNotAllowed = (message) => {
// urlB64ToUint8Array is a magic function that will encode the base64 public key // urlB64ToUint8Array is a magic function that will encode the base64 public key
// to Array buffer which is needed by the subscription option // to Array buffer which is needed by the subscription option
function urlB64ToUint8Array2(base64String) { // function urlB64ToUint8Array2(base64String) {
const padding = '='.repeat((4 - (base64String.length % 4)) % 4) // const padding = '='.repeat((4 - (base64String.length % 4)) % 4)
const base64 = (base64String + padding).replace(/\-/g, '+').replace(/_/g, '/') // const base64 = (base64String + padding).replace(/\-/g, '+').replace(/_/g, '/')
const rawData = atob(base64) // const rawData = atob(base64)
const outputArray = new Uint8Array(rawData.length) // const outputArray = new Uint8Array(rawData.length)
for (let i = 0; i < rawData.length; ++i) { // for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i) // outputArray[i] = rawData.charCodeAt(i)
} // }
return outputArray // return outputArray
// }
function urlBase64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/')
;
const rawData = window.atob(base64);
return Uint8Array.from([...rawData].map((char) => char.charCodeAt(0)));
} }
// function urlB64ToUint8Array(base64String) {
// const padding = '='.repeat((4 - base64String.length % 4) % 4);
// const base64 = (base64String + padding)
// .replace(/\-/g, '+')
function urlB64ToUint8Array(base64String) { // .replace(/_/g, '/');
const padding = '='.repeat((4 - base64String.length % 4) % 4); //
const base64 = (base64String + padding) // const rawData = window.atob(base64);
.replace(/\-/g, '+') // const outputArray = new Uint8Array(rawData.length);
.replace(/_/g, '/'); // const outputData = outputArray.map((output, index) => rawData.charCodeAt(index));
//
const rawData = window.atob(base64); // return outputData;
const outputArray = new Uint8Array(rawData.length); // }
const outputData = outputArray.map((output, index) => rawData.charCodeAt(index));
return outputData;
}
const subscribe = async (reg) => { const subscribe = async (reg) => {
const subscription = await reg.pushManager.getSubscription(); const subscription = await reg.pushManager.getSubscription();
@@ -75,7 +83,7 @@ const subscribe = async (reg) => {
const options = { const options = {
userVisibleOnly: true, userVisibleOnly: true,
// if key exists, create applicationServerKey property // if key exists, create applicationServerKey property
...(key && {applicationServerKey: urlB64ToUint8Array2(key)}) ...(key && {applicationServerKey: urlBase64ToUint8Array(key)})
}; };
const sub = await reg.pushManager.subscribe(options); const sub = await reg.pushManager.subscribe(options);