diff --git a/project/customer/models.py b/project/customer/models.py index 0529c8c..8038795 100644 --- a/project/customer/models.py +++ b/project/customer/models.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- import os -from datetime import datetime +from datetime import datetime, timedelta from PIL import Image from pytils import numeral @@ -358,7 +358,7 @@ class License(models.Model): status = models.IntegerField(verbose_name=u'статус лицензии', choices=consts.LICENSE_STATUSES, default=0) order_date = models.DateField(verbose_name=u'дата заказа', auto_now_add=True) - paid_date = models.DateField(verbose_name=u'дата оплаты', null=True) + paid_date = models.DateField(verbose_name=u'дата оплаты', null=True, blank=True) pay_sum= models.IntegerField(verbose_name=u'сумма оплаты') deleted = models.BooleanField(u'удалено', default=False) @@ -378,12 +378,15 @@ class License(models.Model): def save(self, *args, **kwargs): if not self.__prev_date and self.paid_date: max_date_license = License.objects.filter(user=self.user).aggregate(Max('date_to'))['date_to__max'] + today = datetime.now().date() + if max_date_license < today: + max_date_license = today - timedelta(1) self.date_from = max_date_license + relativedelta(days=1) self.date_to = self.date_from + relativedelta(months=self.term, days=-1) self.user.profile.active = True self.user.profile.save() self.status = 1 - utils.check_one_profile(self.user.profile, License, datetime.now()) + utils.check_one_profile(self.user.profile, License, datetime.now(), manual=True) super(License, self).save(*args, **kwargs) diff --git a/project/customer/tasks.py b/project/customer/tasks.py index 7be84da..f92ee3d 100644 --- a/project/customer/tasks.py +++ b/project/customer/tasks.py @@ -2,25 +2,20 @@ from __future__ import absolute_import from datetime import datetime, timedelta -from django.conf import settings - from celery import shared_task from .models import License, UserProfile from .utils import check_one_profile -SUPPORT_EMAIL = getattr(settings, 'SUPPORT_EMAIL', '') - - @shared_task def check_license(): profiles = UserProfile.objects.all() - now = datetime.now() + now = datetime.today() - licenses = License.objects.filter(date_to__lt=now, status__in=[-1, 2], deleted=False) - licenses.update(status=3) + #licenses = License.objects.filter(date_to__lt=now, status__in=[-1, 2], deleted=False) + #licenses.update(status=3) - licenses = License.objects.filter(order_date__lt=now - timedelta(10), status=0, deleted=False) + licenses = License.objects.filter(order_date__lte=now - timedelta(10), status=0, deleted=False) licenses.update(status=4) for profile in profiles: diff --git a/project/customer/utils.py b/project/customer/utils.py index 8a87b6a..9ef1043 100644 --- a/project/customer/utils.py +++ b/project/customer/utils.py @@ -1,49 +1,81 @@ # -*- 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 -def check_one_profile(profile, License, now): +SUPPORT_EMAIL = getattr(settings, 'SUPPORT_EMAIL', '') + + +def check_one_profile(profile, License, now, manual=False): profile_is_active = profile.active licenses = License.objects.filter(user=profile.user, 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() - licenses.filter(status=1).update(status=2) - - licenses = License.objects.filter(user=profile.user, date_to__lt=now, status=2, deleted=False) - licenses.update(status=3) - licenses_to_pay = License.objects.filter(user=profile.user, status=0, deleted=False) - if licenses: - template_name = 'myauth/license_ended.txt' - subject = u'Документор: срок действия лицензии окончен' + if profile.active and not profile_is_active: + template_name = 'myauth/license_activated.txt' + subject = u'Документор: Профиль активирован' dict_context = {'user_email': licenses[0].user, 'support_email': SUPPORT_EMAIL, + 'license_starts': licenses[0].date_from, '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].user.email,), body=email_body) email.send() - licenses = License.objects.filter(user=profile.user, date_to=now + timedelta(1), status__in=[-1, 2], deleted=False) licenses_remain = License.objects.filter(user=profile.user, date_from__gt=now + timedelta(1), status=1, deleted=False) licenses_to_pay = License.objects.filter(user=profile.user, status=0, deleted=False) - if licenses and not licenses_remain: - template_name = 'myauth/license_ends.txt' - subject = u'Документор: окончание срока действия лицензии' - dict_context = {'user_email': licenses[0].user, - '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].user.email,), body=email_body) - email.send() + if not licenses_remain and not manual: + licenses = License.objects.filter(user=profile.user, date_to__lt=now, status__in=[-1, 2], deleted=False) + licenses.update(status=3) + + if licenses: + template_name = 'myauth/license_ended.txt' + subject = u'Документор: срок действия лицензии окончен' + dict_context = {'user_email': licenses[0].user, + '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].user.email,), body=email_body) + email.send() + + licenses = License.objects.filter(user=profile.user, date_to=now + timedelta(1), status__in=[-1, 2], deleted=False) + + if licenses: + template_name = 'myauth/license_ends.txt' + subject = u'Документор: окончание срока действия лицензии' + dict_context = {'user_email': licenses[0].user, + '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].user.email,), body=email_body) + email.send() + + if not manual: + + licenses = License.objects.filter(user=profile.user, order_date=now - timedelta(9), status=0, deleted=False) + if licenses: + template_name = 'myauth/license_to_pay.txt' + subject = u'Документор: есть неоплаченные счета' + dict_context = {'user_email': licenses[0].user, + 'support_email': SUPPORT_EMAIL, + } + email_body = render_to_string(template_name, dict_context) + email = EmailMessage(subject=subject, to=(licenses[0].user.email,), body=email_body) + email.send() diff --git a/project/settings.py b/project/settings.py index 19d5751..fb257ec 100644 --- a/project/settings.py +++ b/project/settings.py @@ -255,7 +255,7 @@ from datetime import timedelta CELERYBEAT_SCHEDULE = { 'check-license': { 'task': 'project.customer.tasks.check_license', - 'schedule': timedelta(hours=1), + 'schedule': timedelta(days=1), }, } diff --git a/project/templates/myauth/license_activated.txt b/project/templates/myauth/license_activated.txt new file mode 100644 index 0000000..59b568c --- /dev/null +++ b/project/templates/myauth/license_activated.txt @@ -0,0 +1,9 @@ +Здравствуйте! + + +Ваш профиль на сайте Документор (http://www.dokumentor.ru) был активирован. +Срок действия лицензии с {{ license_starts }} по {{ license_ends }}. + +Это письмо написано роботом. Отвечать на него не нужно. + +Связаться со службой поддержки сайта Документор Вы можете по адресу {{ support_email }} diff --git a/project/templates/myauth/license_to_pay.txt b/project/templates/myauth/license_to_pay.txt new file mode 100644 index 0000000..73ccda5 --- /dev/null +++ b/project/templates/myauth/license_to_pay.txt @@ -0,0 +1,8 @@ +Здравствуйте! + +У вас есть неоплаченный счёт, который можно оплатить до завтра. Через 1 день этот счёт оплатить будет невозможно. + +Это письмо написано роботом. Отвечать на него не нужно. + + +Связаться со службой поддержки сайта Документор Вы можете по адресу {{ support_email }}