# -*- coding: utf-8 -*- from datetime import timedelta from dateutil.relativedelta import relativedelta from pytils import numeral from django.utils import timezone from django.conf import settings from django.core.mail import EmailMessage from django.template.loader import render_to_string from django.core.cache import cache from commons.utils import get_site_url def check_one_profile(profile, now, manual=False): from customer.models import License profile_is_active = profile.active licenses = License.objects. \ filter(company=profile, date_from__lte=now, date_to__gte=now, status__in=[-1, 1, 2], deleted=False) licenses.filter(status=1).update(status=2) if licenses: profile.active = True else: profile.active = False profile.save() user_email = profile.users.get().email if profile.active and not profile_is_active: template_name = 'emails/license_activated.txt' subject = 'Документор: Профиль активирован' dict_context = {'user_email': user_email, 'support_email': settings.SUPPORT_EMAIL, 'license_starts': licenses[0].date_from, 'license_ends': licenses[0].date_to, 'site_url': get_site_url() } email_body = render_to_string(template_name, dict_context) email = EmailMessage(subject=subject, to=(user_email,), body=email_body) email.send() licenses_remain = License.objects.filter(company=profile, date_from__gt=now + timedelta(1), status=1, deleted=False) licenses_to_pay = License.objects.filter(company=profile, status=0, deleted=False) if not licenses_remain and not manual: licenses = License.objects.filter(company=profile, date_to__lt=now, status__in=[-1, 2], deleted=False) licenses.update(status=3) if licenses: template_name = 'emails/license_ended.txt' subject = 'Документор: срок действия лицензии окончен' dict_context = {'user_email': user_email, 'support_email': settings.SUPPORT_EMAIL, 'license_ends': licenses[0].date_to, 'licenses_to_pay': licenses_to_pay, 'site_url': get_site_url() } 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(1), status__in=[-1, 2], deleted=False) if licenses: template_name = 'emails/license_ends.txt' subject = 'Документор: окончание срока действия лицензии' dict_context = {'user_email': user_email, 'support_email': settings.SUPPORT_EMAIL, 'license_ends': licenses[0].date_to, 'licenses_to_pay': licenses_to_pay, 'site_url': get_site_url() } 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 = License.objects.filter(company=profile, order_date=now - timedelta(9), status=0, deleted=False) if licenses: template_name = 'emails/license_to_pay.txt' subject = 'Документор: есть неоплаченные счета' dict_context = {'user_email': user_email, 'support_email': settings.SUPPORT_EMAIL, 'site_url': get_site_url() } email_body = render_to_string(template_name, dict_context) email = EmailMessage(subject=subject, to=(user_email,), body=email_body) email.send() def raise_if_no_profile(request): if not request.user.profile: raise Exception( f"Profile not found for user: {request.user.pk}, '{request.user.username}'" ) def check_confirm_bonus_to_user(user): """ Check user for displaying a message with an offer for purchase of licenses for additional bonus licenses Show message only for new users in the period from 5 to 15 day If user already have licenses don't display message :param user: DockUser instance :return: count of days remaining for getting bonus, integer or integer (-1) if nothing to use """ from customer.models import License lic = License.objects.filter( company=user.profile, status__in=[1, 2], deleted=False ) if lic: return -1 today = timezone.now().date() date_join_start = today - timezone.timedelta(days=15) date_join_end = today - timezone.timedelta(days=5) if date_join_start <= user.date_joined.date() <= date_join_end: delta = user.date_joined.date() - date_join_start return delta.days return -1 def get_display_message_for_bonus(day_count): """ Get text to display the message depending on count of days :param day_count: Count of days, integer :return: Message for display in page, string """ message = '' if type(day_count) == str: return message if day_count > 10: return message if day_count == 0: message = f'Сегодня последний день что бы получить бонус' if day_count > 0: message = f'У вас есть ещё {day_count} ' \ f'{numeral.choose_plural(day_count, "день, дня, дней")}, ' \ f'чтобы получить бонус' return message def create_bonus_license(lic): if check_confirm_bonus_to_user(lic.company.get_first_user()) and lic.term >= 12: bonus_term = 0 if lic.term == 12: bonus_term = 2 if lic.term == 24: bonus_term = 3 bonus_license = lic._meta.model.objects.create( company=lic.company, term=bonus_term, payform=-2, status=1, order_date=timezone.now().date(), date_from=lic.date_to + timedelta(1), date_to=lic.date_to + timedelta(1) + relativedelta(months=bonus_term, days=-1), pay_sum=0 ) cache.set(f'confirm_bonus_days_{lic.company.get_first_user().username}', -1, 3600) return bonus_license return False