X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/ca336bd1f9658cf713681d1412d4153e5c4d9c93..d048787bd42b9d009aa7b3dfb5c08f7682f231e6:/src/club/models.py?ds=inline diff --git a/src/club/models.py b/src/club/models.py index f5cbf6e7f..1f47d587b 100644 --- a/src/club/models.py +++ b/src/club/models.py @@ -1,8 +1,10 @@ from datetime import datetime, timedelta from django.conf import settings from django.contrib.sites.models import Site +from django.core.mail import send_mail from django.urls import reverse from django.db import models +from django import template from django.utils.timezone import now from django.utils.translation import ugettext_lazy as _, ungettext, ugettext, get_language from catalogue.utils import get_random_hash @@ -22,7 +24,8 @@ class Plan(models.Model): ] interval = models.SmallIntegerField(_('inteval'), choices=intervals) - min_amount = models.DecimalField(_('min_amount'), max_digits=10, decimal_places=2) + min_amount = models.DecimalField(_('min amount'), max_digits=10, decimal_places=2) + default_amount = models.DecimalField(_('default amount'), max_digits=10, decimal_places=2) allow_recurring = models.BooleanField(_('allow recurring')) allow_one_time = models.BooleanField(_('allow one time')) active = models.BooleanField(_('active'), default=True) @@ -66,6 +69,7 @@ class Schedule(models.Model): is_cancelled = models.BooleanField(_('cancelled'), default=False) started_at = models.DateTimeField(_('started at'), auto_now_add=True) expires_at = models.DateTimeField(_('expires_at'), null=True, blank=True) + email_sent = models.BooleanField(default=False) class Meta: verbose_name = _('schedule') @@ -88,6 +92,9 @@ class Schedule(models.Model): def get_absolute_url(self): return reverse('club_schedule', args=[self.key]) + def get_thanks_url(self): + return reverse('club_thanks', args=[self.key]) + def get_payment_method(self): return method_by_slug[self.method] @@ -97,11 +104,21 @@ class Schedule(models.Model): def is_active(self): return self.expires_at is not None and self.expires_at > now() + def send_email(self): + ctx = {'schedule': self} + send_mail( + template.loader.render_to_string('club/email/thanks_subject.txt', ctx).strip(), + template.loader.render_to_string('club/email/thanks.txt', ctx), + settings.CONTACT_EMAIL, [self.email], fail_silently=False) + self.email_sent = True + self.save() + class Membership(models.Model): """ Represents a user being recognized as a member of the club. """ user = models.OneToOneField(settings.AUTH_USER_MODEL, verbose_name=_('user'), on_delete=models.CASCADE) created_at = models.DateTimeField(_('created at'), auto_now_add=True) + name = models.CharField(max_length=255, blank=True) honorary = models.BooleanField(default=False) class Meta: @@ -165,7 +182,7 @@ class PayUOrder(payu_models.Order): def get_continue_url(self): return "https://{}{}".format( Site.objects.get_current().domain, - self.schedule.get_absolute_url()) + self.schedule.get_thanks_url()) def get_description(self): return ugettext('Towarzystwo Wolnych Lektur') @@ -183,11 +200,15 @@ class PayUOrder(payu_models.Order): def status_updated(self): if self.status == 'COMPLETED': - new_exp = self.schedule.plan.get_next_installment(now()) + since = self.schedule.expires_at or now() + new_exp = self.schedule.plan.get_next_installment(since) if self.schedule.expires_at is None or self.schedule.expires_at < new_exp: self.schedule.expires_at = new_exp self.schedule.save() + if not self.schedule.email_sent: + self.schedule.send_email() + class PayUCardToken(payu_models.CardToken): schedule = models.ForeignKey(Schedule, on_delete=models.CASCADE)