From fb17e93f5771a82490401abbe1d183d426a2f512 Mon Sep 17 00:00:00 2001 From: apache Date: Fri, 3 Mar 2017 22:26:59 +0300 Subject: [PATCH] license: 1) don't fail on exceptions inside check_license 2) use there email from DokUser --- project/customer/tasks.py | 12 ++++++++++-- project/customer/utils.py | 22 ++++++++++++---------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/project/customer/tasks.py b/project/customer/tasks.py index f92ee3d..36cd88c 100644 --- a/project/customer/tasks.py +++ b/project/customer/tasks.py @@ -1,8 +1,12 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import + from datetime import datetime, timedelta +import traceback +from django.core.mail import mail_admins from celery import shared_task + from .models import License, UserProfile from .utils import check_one_profile @@ -19,7 +23,11 @@ def check_license(): licenses.update(status=4) for profile in profiles: - check_one_profile(profile, License, now) + try: + check_one_profile(profile, License, now) + except Exception as e: + mail_admins(subject=u'customer: check_license error', + message=u'Profile id=%s.\n\n%s' % (profile.pk, traceback.format_exc(e)) + ) return None - diff --git a/project/customer/utils.py b/project/customer/utils.py index 978e7a4..cea368c 100644 --- a/project/customer/utils.py +++ b/project/customer/utils.py @@ -6,7 +6,7 @@ from django.core.mail import EmailMessage from django.template.loader import render_to_string -SUPPORT_EMAIL = getattr(settings, 'SUPPORT_EMAIL', '') +SUPPORT_EMAIL = getattr(settings, 'SUPPORT_EMAIL') def check_one_profile(profile, License, now, manual=False): @@ -20,16 +20,18 @@ def check_one_profile(profile, License, now, manual=False): profile.active = False profile.save() + user_email = profile.users.get().email + if profile.active and not profile_is_active: template_name = 'myauth/license_activated.txt' subject = u'Документор: Профиль активирован' - dict_context = {'user_email': licenses[0].company, - 'support_email': SUPPORT_EMAIL, + 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=(licenses[0].company.email,), body=email_body) + 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) @@ -42,13 +44,13 @@ def check_one_profile(profile, License, now, manual=False): if licenses: template_name = 'myauth/license_ended.txt' subject = u'Документор: срок действия лицензии окончен' - dict_context = {'user_email': licenses[0].company, + 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=(licenses[0].company.email,), body=email_body) + 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) @@ -56,13 +58,13 @@ def check_one_profile(profile, License, now, manual=False): if licenses: template_name = 'myauth/license_ends.txt' subject = u'Документор: окончание срока действия лицензии' - dict_context = {'user_email': licenses[0].company, + 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=(licenses[0].company.email,), body=email_body) + email = EmailMessage(subject=subject, to=(user_email,), body=email_body) email.send() if not manual: @@ -71,11 +73,11 @@ def check_one_profile(profile, License, now, manual=False): if licenses: template_name = 'myauth/license_to_pay.txt' subject = u'Документор: есть неоплаченные счета' - dict_context = {'user_email': licenses[0].company, + dict_context = {'user_email': user_email, 'support_email': SUPPORT_EMAIL, } email_body = render_to_string(template_name, dict_context) - email = EmailMessage(subject=subject, to=(licenses[0].company.email,), body=email_body) + email = EmailMessage(subject=subject, to=(user_email,), body=email_body) email.send()