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.
 
 
 
 

113 lines
5.2 KiB

# -*- coding: utf-8 -*-
from datetime import timedelta
from django.db.models import Q
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from django.conf import settings
from project.customer import consts
SUPPORT_EMAIL = getattr(settings, 'SUPPORT_EMAIL')
def check_one_profile(profile, License, now_, manual=False):
profile_was_active = profile.active
# действующие лицензии
_base_qs = License.objects.filter(company=profile, date_from__lte=now_, date_to__gte=now_, deleted=False)
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 current_license:
profile.active = True
profile.save()
user_email = profile.users.get().email
# если профиль только что активирован
if profile.active and not profile_was_active:
template_name = 'myauth/license_activated.txt'
subject = u'Документор: Профиль активирован'
dict_context = {
'user_email': user_email,
'support_email': SUPPORT_EMAIL,
'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), deleted=False,
status=consts.LICENSE_INACTIVE, order_status=consts.ORDER_PAID)
# если нет ожидающих активации лицензий
if not licenses_remain.exists():
# ожидающие оплаты лицензии (без учёта даты заказа)
licenses_to_pay = License.objects.filter(company=profile, order_status=consts.ORDER_UNPAID, deleted=False)
# истёкшие лицензии
_base_qs = License.objects.filter(company=profile, date_to__lt=now_, deleted=False, status=consts.LICENSE_ACTIVE)
ended_license = _base_qs[:1] # ближайшая лицензия
_base_qs.update(status=consts.LICENSE_EXPIRED)
if ended_license:
template_name = 'myauth/license_ended.txt'
subject = u'Документор: срок действия лицензии окончен'
dict_context = {
'user_email': user_email,
'support_email': SUPPORT_EMAIL,
'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()
# лицензии с истекающим завтра сроком действия
ending_license = License.objects.filter(company=profile, date_to = now_ + timedelta(days=1), deleted=False,
status=consts.LICENSE_ACTIVE)[:1] # ближайшая лицензия
if ending_license:
template_name = 'myauth/license_ends.txt'
subject = u'Документор: окончание срока действия лицензии'
dict_context = {
'user_email': user_email,
'support_email': SUPPORT_EMAIL,
'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()
# ожидающие оплаты лицензии (с учётом даты заказа)
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 unpaid_licenses.exists():
template_name = 'myauth/license_to_pay.txt'
subject = u'Документор: есть неоплаченные счета'
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(u"Profile not found for user: %d, '%s'" % (request.user.pk, request.user.username))