118 lines
4.3 KiB
Python
118 lines
4.3 KiB
Python
from .models import *
|
|
from django.template.loader import render_to_string
|
|
from django.utils.translation import get_language, activate
|
|
from datetime import datetime, timedelta
|
|
import json
|
|
|
|
|
|
def get_cur_user_subscribe(user):
|
|
|
|
user_subscribe = None
|
|
try:
|
|
user_subscribe = SubscribeForUser.objects.get(user=user)
|
|
except Exception as e:
|
|
pass
|
|
|
|
return user_subscribe
|
|
|
|
|
|
def get_subscribes_w_options():
|
|
all_options = SubscribeOption.objects.filter(enable=True)
|
|
subscribes = Subscribe.objects.filter(enable=True)
|
|
for subscribe in subscribes:
|
|
subscribe_options_ids = subscribe.options.values_list('id', flat=True)
|
|
subscribe.disabled_options = all_options.exclude(id__in=subscribe_options_ids)
|
|
|
|
return subscribes, all_options
|
|
|
|
|
|
def check_n_enable_subscribe_by_order(order):
|
|
|
|
subscribes_for_user = SubscribeForUser.objects.filter(user=order.user)
|
|
|
|
if order and order.enable and order.status == 'charged':
|
|
kwargs = {
|
|
'user': order.user,
|
|
'subscribe': order.subscribe,
|
|
'last_paid_DT': datetime.now(),
|
|
'paid_period_from_DT': datetime.now(),
|
|
'paid_period_to_DT': datetime.now() + timedelta(hours=order.subscribe.period),
|
|
'receive_finish_subscribe_msg': True,
|
|
}
|
|
subscribe_for_user = SubscribeForUser.objects.create(**kwargs)
|
|
order.subscribe_for_user = subscribe_for_user
|
|
order.save()
|
|
|
|
subscribes_for_user = [subscribe_for_user]
|
|
|
|
return subscribes_for_user
|
|
|
|
|
|
def get_profile_subscribe_page_content_Dict(request):
|
|
|
|
try:
|
|
|
|
from GeneralApp.funcs import get_and_set_lang
|
|
lang = get_and_set_lang(request)
|
|
|
|
data = {}
|
|
if request.body:
|
|
data = json.loads(request.body)
|
|
check_orders_required = False
|
|
if data and 'check_orders_required' in data: #Требуется проверка статусов заказов
|
|
check_orders_required = True
|
|
# all_options = SubscribeOption.objects.filter(enable=True)
|
|
subscribes, all_options = get_subscribes_w_options()
|
|
|
|
subscribes_for_user = None
|
|
subscribe_for_user = None
|
|
|
|
orders = None
|
|
if request.user and request.user.is_authenticated:
|
|
from BillingApp.funcs import get_orders_for_user, get_order_status
|
|
orders = get_orders_for_user(request.user)
|
|
if orders:
|
|
if check_orders_required:
|
|
for order in orders:
|
|
order = get_order_status(order)
|
|
subscribes_for_user = check_n_enable_subscribe_by_order(order)
|
|
if not subscribes_for_user:
|
|
error = f'{order.status}'
|
|
if 'status' in order.json_data and 'failure_message' in order.json_data['status']:
|
|
error = f'{error} ({order.json_data["status"]["failure_message"]})'
|
|
subscribes.get(id=order.subscribe.id).order_error = error
|
|
check_orders_required = False
|
|
else:
|
|
check_orders_required = True
|
|
|
|
|
|
if not subscribes_for_user:
|
|
subscribes_for_user = SubscribeForUser.objects.filter(user=request.user)
|
|
|
|
if not subscribes_for_user:
|
|
tpl_name = 'blocks/profile/b_subscribe_variants.html'
|
|
else:
|
|
tpl_name = 'blocks/profile/b_subscribe_current.html'
|
|
subscribe_for_user = subscribes_for_user[0]
|
|
subscribe_options_ids = subscribe_for_user.subscribe.options.values_list('id', flat=True)
|
|
subscribe_for_user.subscribe.disabled_options = all_options.exclude(id__in=subscribe_options_ids)
|
|
|
|
# subscribes = Subscribe.objects.filter(enable=True)
|
|
# for subscribe in subscribes:
|
|
# subscribe_options_ids = subscribe.options.values_list('id', flat=True)
|
|
# subscribe.disabled_options = all_options.exclude(id__in=subscribe_options_ids)
|
|
|
|
Dict = {
|
|
'subscribe_for_user': subscribe_for_user,
|
|
'subscribes': subscribes,
|
|
}
|
|
|
|
html = render_to_string(tpl_name, Dict, request=request)
|
|
return {
|
|
'html': html,
|
|
'check_orders_required': check_orders_required,
|
|
}
|
|
|
|
except Exception as e:
|
|
msg = f'show_cur_subscribe_ajax Error = {str(e)}'
|
|
return msg |