You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

93 lines
4.0 KiB

# -*- coding: utf-8 -*-
from datetime import timedelta
from django.conf import settings
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
SUPPORT_EMAIL = getattr(settings, 'SUPPORT_EMAIL')
def check_one_profile(profile, License, now, manual=False):
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': SUPPORT_EMAIL,
'license_starts': licenses[0].date_from,
'license_ends': licenses[0].date_to,
}
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': SUPPORT_EMAIL,
'license_ends': licenses[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(1),
status__in=[-1, 2], deleted=False)
if licenses:
template_name = 'emails/license_ends.txt'
subject = 'Документор: окончание срока действия лицензии'
dict_context = {'user_email': user_email,
'support_email': SUPPORT_EMAIL,
'license_ends': licenses[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()
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': SUPPORT_EMAIL,
}
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}'"
)