diff --git a/AuthApp/funcs.py b/AuthApp/funcs.py index 114792b..6389e84 100644 --- a/AuthApp/funcs.py +++ b/AuthApp/funcs.py @@ -43,7 +43,7 @@ def get_profile_page_content_html(request, page_name, data): return get_profile_support_page_content_html(request, data) elif page_name == 'my_subscribe': from SubscribesApp.funcs import get_profile_subscribe_page_content_Dict - res = get_profile_subscribe_page_content_Dict(request) + res = get_profile_subscribe_page_content_Dict(request, check_orders_required=True) return res['html'] elif page_name == 'change_profile': return get_profile_change_page_content_html(request) diff --git a/BillingApp/funcs.py b/BillingApp/funcs.py index ec30e72..d94dd3d 100644 --- a/BillingApp/funcs.py +++ b/BillingApp/funcs.py @@ -37,8 +37,15 @@ def get_orders_for_user(user): enable=True, user=user, subscribe_for_user=None, - # createDT__gt=datetime.now() - timedelta(hours=1) + createDT__gt=datetime.now() - timedelta(hours=1) ).order_by('subscribe', '-createDT').distinct('subscribe') + + SubscribeOrder.objects.filter( + user=user + ).exclude( + id__in=orders.values_list('id', flat=True) + ).update(enable=False) + return orders diff --git a/BillingApp/models.py b/BillingApp/models.py index 1c2e8fb..7ea004c 100644 --- a/BillingApp/models.py +++ b/BillingApp/models.py @@ -39,6 +39,32 @@ class SubscribeOrder(BaseModel): verbose_name = _('Заказ на подписку') verbose_name_plural = _('Заказы на подписки') + def activate_subscribe_for_user(self): + from datetime import datetime, timedelta + + kwargs = { + 'user': self.user, + 'subscribe': self.subscribe, + 'last_paid_DT': datetime.now(), + 'paid_period_from_DT': datetime.now(), + 'paid_period_to_DT': datetime.now() + timedelta(hours=self.subscribe.period), + 'receive_finish_subscribe_msg': True, + 'enable': True, + } + subscribe_for_user = SubscribeForUser.objects.create(**kwargs) + self.subscribe_for_user = subscribe_for_user + self.enable = False + self.save() + + subscribes_for_user = SubscribeForUser.objects.filter( + user=self.user + ).exclude( + id=subscribe_for_user.id + ) + subscribes_for_user.update(enable=False) + + return self + def __str__(self): res = 'Заказ' if self.subscribe: diff --git a/SubscribesApp/admin.py b/SubscribesApp/admin.py index 66b2131..dce4541 100644 --- a/SubscribesApp/admin.py +++ b/SubscribesApp/admin.py @@ -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) \ No newline at end of file diff --git a/SubscribesApp/funcs.py b/SubscribesApp/funcs.py index 72f7770..f2ccc23 100644 --- a/SubscribesApp/funcs.py +++ b/SubscribesApp/funcs.py @@ -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' diff --git a/SubscribesApp/models.py b/SubscribesApp/models.py index 6b0ffd4..57572df 100644 --- a/SubscribesApp/models.py +++ b/SubscribesApp/models.py @@ -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 = _('Подписки')