0.12.36 pays and subscribes

This commit is contained in:
SDE
2024-04-20 12:15:55 +03:00
parent 5c06aceb27
commit bb319780b6
17 changed files with 347 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
from .models import *
from django.template.loader import render_to_string
from django.utils.translation import get_language, activate
from datetime import datetime, timedelta
def get_cur_user_subscribe(user):
@@ -13,7 +14,7 @@ def get_cur_user_subscribe(user):
return user_subscribe
def get_subsribes_w_options():
def get_subscribes_w_options():
all_options = SubscribeOption.objects.filter(enable=True)
subscribes = Subscribe.objects.filter(enable=True)
for subscribe in subscribes:
@@ -23,6 +24,28 @@ def get_subsribes_w_options():
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_html(request):
try:
@@ -32,9 +55,20 @@ def get_profile_subscribe_page_content_html(request):
# data = json.loads(request.body)
# all_options = SubscribeOption.objects.filter(enable=True)
subscribes, all_options = get_subsribes_w_options()
subscribes, all_options = get_subscribes_w_options()
subscribe_for_user = 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)
for order in orders:
res = get_order_status(order)
subscribe_for_user = check_n_enable_subscribe_by_order(order)
if not subscribe_for_user:
subscribe_for_user = SubscribeForUser.objects.filter(user=request.user)
subscribe_for_user = SubscribeForUser.objects.filter(user=request.user)
if not subscribe_for_user:
tpl_name = 'blocks/profile/b_subscribe_variants.html'
else:

View File

@@ -3,7 +3,7 @@ from django.shortcuts import render
from uuid import uuid1
from .models import *
from django.contrib import auth
from django.http import HttpResponse, Http404, JsonResponse
from django.http import HttpResponse, Http404, JsonResponse, HttpResponseRedirect
from django.template import loader, RequestContext
from django.contrib.auth.decorators import login_required
from BaseModels.mailSender import techSendMail
@@ -17,6 +17,7 @@ from datetime import datetime, time, timedelta
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
from GeneralApp.funcs import get_and_set_lang
from django.shortcuts import redirect
@login_required()#login_url='/profile/login/')
@@ -33,6 +34,18 @@ def subscribe_now_ajax(request):
subscribe = Subscribe.objects.get(id=data['subscribe_id'])
kwargs_for_order = {
'user': request.user,
'subscribe': subscribe,
'currency': 'USD',
'sum': subscribe.price,
}
from BillingApp.funcs import create_subscribe_order
order = create_subscribe_order(kwargs_for_order)
if order:
return JsonResponse({'redirect': order.pay_page})
kwargs = {
'user': request.user,
'subscribe': subscribe,
@@ -41,13 +54,10 @@ def subscribe_now_ajax(request):
'paid_period_to_DT': datetime.now() + timedelta(hours=subscribe.period),
'receive_finish_subscribe_msg': True,
}
subscribe_for_user = SubscribeForUser.objects.filter(user=request.user)
if subscribe_for_user:
subscribe_for_user.update(**kwargs)
subscribe_for_user = subscribe_for_user[0]
else:
subscribe_for_user = SubscribeForUser.objects.create(**kwargs)
if not subscribe_for_user:
tpl_name = 'blocks/profile/b_subscribe_variants.html'

View File

@@ -49,4 +49,15 @@ class SubscribeForUser(BaseModel):
class Meta:
verbose_name = _('Пользовательская подписка')
verbose_name_plural = _('Пользовательские подписки')
verbose_name_plural = _('Пользовательские подписки')
def __str__(self):
res = 'Подписка'
if self.subscribe:
res += f' {self.subscribe.name}'
if self.user:
res += f' для {self.user.username}'
if not res:
res += f' {str(self.id)}'
return res