diff --git a/project/customer/context_processors.py b/project/customer/context_processors.py index 8d9de48..8d6cafb 100644 --- a/project/customer/context_processors.py +++ b/project/customer/context_processors.py @@ -26,28 +26,29 @@ def license_check_soon_ends(request): now_ = datetime.today() if not days_left or not cur_license: - current_licenses = (License.objects + current_license = (License.objects .filter(company=request.user.profile, date_from__lte=now_, date_to__gte=now_, deleted=False) - .filter(Q(status__in = [consts.LICENSE_TEST_PERIOD, consts.LICENSE_ACTIVE]) | Q(order_status=consts.ORDER_PAID)) + .filter(Q(status=consts.LICENSE_ACTIVE) | Q(status=consts.LICENSE_INACTIVE, order_status=consts.ORDER_PAID))[:1] # ) - if current_licenses: - cur_license = current_licenses[0] + if current_license: + cur_license = current_license[0] days_left = (cur_license.date_to - now_.date()).days cache.set('days_left_%s' % (request.user.username,), days_left, cache_duration) cache.set('cur_license_%s' % (request.user.username,), cur_license, cache_duration) if not license_cookie: if license_15days is None: - licenses_ends = (License.objects - .filter(company=request.user.profile, date_to__lte = now_ + timedelta(days=consts.LICENSE_DAYS_BEFORE_EXPIRE), deleted=False) - .filter(Q(status__in = [consts.LICENSE_TEST_PERIOD, consts.LICENSE_ACTIVE]) | Q(order_status=consts.ORDER_PAID)) + ending_license = (License.objects + .filter(company=request.user.profile, date_from__lte=now_, + date_to__lte = now_ + timedelta(days=consts.LICENSE_DAYS_BEFORE_EXPIRE), deleted=False) + .filter(Q(status__in = [consts.LICENSE_INACTIVE, consts.LICENSE_ACTIVE]) | Q(order_status=consts.ORDER_PAID))[:1] # ) next_licenses = License.objects.filter(company=request.user.profile, order_status=consts.ORDER_PAID, deleted=False) - if licenses_ends and not next_licenses: - days_to_end = licenses_ends[0].date_to + if ending_license and not next_licenses.exists(): + days_to_end = ending_licenses[0].date_to cache.set('license_15_%s' % (request.user.username,), days_to_end, cache_duration) license_15days = days_to_end else: diff --git a/project/customer/utils.py b/project/customer/utils.py index 8b6bf3a..bd5a873 100644 --- a/project/customer/utils.py +++ b/project/customer/utils.py @@ -15,14 +15,16 @@ SUPPORT_EMAIL = getattr(settings, 'SUPPORT_EMAIL') def check_one_profile(profile, License, now_, manual=False): profile_was_active = profile.active - licenses = (License.objects.filter(company=profile, date_from__lte=now_, date_to__gte=now_, deleted=False) - .filter(Q(status__in = [consts.LICENSE_TEST_PERIOD, consts.LICENSE_ACTIVE]) | Q(order_status=consts.ORDER_PAID)) - ) + # действующие лицензии + _base_qs = License.objects.filter(company=profile, date_from__lte=now_, date_to__gte=now_, deleted=False) - licenses.filter(order_status=consts.ORDER_PAID).update(status=consts.LICENSE_ACTIVE) + current_license = _base_qs.filter( + Q(status=consts.LICENSE_ACTIVE) | Q(status=consts.LICENSE_INACTIVE, order_status=consts.ORDER_PAID))[:1] # ближайшая лицензия + + _base_qs.filter(status=consts.LICENSE_INACTIVE, order_status=consts.ORDER_PAID).update(status=consts.LICENSE_ACTIVE) profile.active = False - if licenses: + if current_license: profile.active = True profile.save() @@ -36,58 +38,65 @@ def check_one_profile(profile, License, now_, manual=False): dict_context = { 'user_email': user_email, 'support_email': SUPPORT_EMAIL, - 'license_starts': licenses[0].date_from, - 'license_ends': licenses[0].date_to, + 'license_starts': current_license[0].date_from, + 'license_ends': current_license[0].date_to, } email_body = render_to_string(template_name, dict_context) email = EmailMessage(subject=subject, to=(user_email,), body=email_body) email.send() if not manual: - licenses_remain = License.objects.filter(company=profile, date_from__gt = now_ + timedelta(days=1), - order_status=consts.ORDER_PAID, deleted=False) + # оплаченные лицензии, срок действия которых ещё не наступил + licenses_remain = License.objects.filter(company=profile, date_from__gt = now_ + timedelta(days=1), deleted=False, + status=consts.LICENSE_INACTIVE, order_status=consts.ORDER_PAID) - if not licenses_remain: + # если нет ожидающих активации лицензий + if not licenses_remain.exists(): + # ожидающие оплаты лицензии (без учёта даты заказа) licenses_to_pay = License.objects.filter(company=profile, order_status=consts.ORDER_UNPAID, deleted=False) - licenses = License.objects.filter(company=profile, date_to__lt=now_, - status__in=[consts.LICENSE_TEST_PERIOD, consts.LICENSE_ACTIVE], deleted=False) + # истёкшие лицензии + _base_qs = License.objects.filter(company=profile, date_to__lt=now_, deleted=False, status=consts.LICENSE_ACTIVE) + + ended_license = _base_qs[:1] # ближайшая лицензия - licenses.update(status=consts.LICENSE_EXPIRED) + _base_qs.update(status=consts.LICENSE_EXPIRED) - if licenses: + if ended_license: template_name = 'myauth/license_ended.txt' subject = u'Документор: срок действия лицензии окончен' dict_context = { 'user_email': user_email, 'support_email': SUPPORT_EMAIL, - 'license_ends': licenses[0].date_to, + 'license_ends': ended_license[0].date_to, 'licenses_to_pay': licenses_to_pay, } email_body = render_to_string(template_name, dict_context) email = EmailMessage(subject=subject, to=(user_email,), body=email_body) email.send() - licenses = License.objects.filter(company=profile, date_to = now_ + timedelta(days=1), - status__in=[consts.LICENSE_TEST_PERIOD, consts.LICENSE_ACTIVE], deleted=False) + # лицензии с истекающим завтра сроком действия + ending_license = License.objects.filter(company=profile, date_to = now_ + timedelta(days=1), deleted=False, + status=consts.LICENSE_ACTIVE)[:1] # ближайшая лицензия - if licenses: + if ending_license: template_name = 'myauth/license_ends.txt' subject = u'Документор: окончание срока действия лицензии' dict_context = { 'user_email': user_email, 'support_email': SUPPORT_EMAIL, - 'license_ends': licenses[0].date_to, + 'license_ends': ending_license[0].date_to, 'licenses_to_pay': licenses_to_pay, } email_body = render_to_string(template_name, dict_context) email = EmailMessage(subject=subject, to=(user_email,), body=email_body) email.send() - licenses = License.objects.filter(company=profile, order_date = now_ - timedelta(days = consts.ORDER_PAY_DAYS - 1), - order_status=consts.ORDER_UNPAID, deleted=False) + # ожидающие оплаты лицензии (с учётом даты заказа) + unpaid_licenses = License.objects.filter(company=profile, order_date = now_ - timedelta(days = consts.ORDER_PAY_DAYS - 1), + deleted=False, order_status=consts.ORDER_UNPAID) - if licenses: + if unpaid_licenses.exists(): template_name = 'myauth/license_to_pay.txt' subject = u'Документор: есть неоплаченные счета' dict_context = {