1.0.12 subscribe buy routines

This commit is contained in:
SDE
2024-05-10 15:34:56 +03:00
parent 563a4fde3c
commit 3575391d3f
6 changed files with 71 additions and 38 deletions

View File

@@ -63,7 +63,7 @@ class Admin_SubscribeForUser(Admin_Trans_BaseModel):
fieldsets = (
(None, {
'classes': ['wide'],
'fields': ('name',
'fields': ('enable',
'user', 'subscribe',
'last_paid_DT',
'paid_period_from_DT', 'paid_period_to_DT',
@@ -88,6 +88,7 @@ class Admin_SubscribeForUser(Admin_Trans_BaseModel):
'auto_continue', 'receive_finish_subscribe_msg',
'modifiedDT', 'createDT'
]
list_editable = ['enable']
search_fields = ['name']
admin.site.register(SubscribeForUser,Admin_SubscribeForUser)

View File

@@ -16,42 +16,33 @@ def get_cur_user_subscribe(user):
return user_subscribe
def get_subscribes_w_options():
def get_subscribes_w_options(user=None, check_subscribe_orders=False):
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)
if user and check_subscribe_orders:
order = subscribe.get_last_order(user)
if order and order.status not in ['charged']:
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"]})'
subscribe.order_error = error
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:
if order.status == 'charged':
order = order.activate_subscribe_for_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,
'enable': True,
}
subscribe_for_user = SubscribeForUser.objects.create(**kwargs)
order.subscribe_for_user = subscribe_for_user
order.save()
SubscribeForUser.objects.filter(user=order.user).exclude(id=subscribe_for_user.id).update(enable=False)
subscribes_for_user = [subscribe_for_user]
return subscribes_for_user
return order
def get_profile_subscribe_page_content_Dict(request):
def get_profile_subscribe_page_content_Dict(request, check_orders_required=False):
try:
@@ -61,14 +52,15 @@ def get_profile_subscribe_page_content_Dict(request):
data = {}
if request.body:
data = json.loads(request.body)
check_orders_required = False
# check_orders_required = False
if data and 'check_orders_required' in data: #Требуется проверка статусов заказов
check_orders_required = data['check_orders_required']
# all_options = SubscribeOption.objects.filter(enable=True)
subscribes, all_options = get_subscribes_w_options()
subscribes_for_user = None
subscribes = []
subscribe_for_user = None
all_options = []
orders = None
if request.user and request.user.is_authenticated:
@@ -78,21 +70,21 @@ def get_profile_subscribe_page_content_Dict(request):
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"]})'
for subscribe in subscribes:
if subscribe == order.subscribe:
subscribe.order_error = error
order = check_n_enable_subscribe_by_order(order)
subscribe_for_user = order.subscribe_for_user
subscribes, all_options = get_subscribes_w_options(
request.user, check_subscribe_orders=True)
check_orders_required = False
else:
check_orders_required = True
if not subscribes:
subscribes, all_options = get_subscribes_w_options()
if not subscribes_for_user:
subscribes_for_user = SubscribeForUser.objects.filter(enable=True, user=request.user)
# if not subscribes_for_user:
subscribes_for_user = SubscribeForUser.objects.filter(enable=True, user=request.user)
if not subscribes_for_user:
tpl_name = 'blocks/profile/b_subscribe_variants.html'

View File

@@ -24,6 +24,13 @@ class Subscribe(BaseModel):
bg_color = ColorField(default='#FFFFFF', verbose_name=_('Цвет фона'))
text_color = ColorField(default='#000000', verbose_name=_('Цвет текста'))
def get_last_order(self, user):
order = None
orders = self.subscribe_orders_for_subscribe.filter(user=user, enable=True).order_by('-createDT')
if orders:
order = orders[0]
return order
class Meta:
verbose_name = _('Подписка')
verbose_name_plural = _('Подписки')