X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/9a5423b58158ae6d970cdd7a3dc25e5559aa374a..0c9e7e914bc548bee30add46fb6e4aadf6ffb16c:/apps/funding/models.py?ds=inline diff --git a/apps/funding/models.py b/apps/funding/models.py index ac652b18e..de52ac9d8 100644 --- a/apps/funding/models.py +++ b/apps/funding/models.py @@ -15,6 +15,7 @@ from catalogue.models import Book from catalogue.utils import get_random_hash from polls.models import Poll from django.contrib.sites.models import Site +from . import app_settings class Offer(models.Model): @@ -26,14 +27,15 @@ class Offer(models.Model): target = models.DecimalField(_('target'), decimal_places=2, max_digits=10) start = models.DateField(_('start'), db_index=True) end = models.DateField(_('end'), db_index=True) - due = models.DateField(_('due'), - help_text=_('When will it be published if the money is raised.')) redakcja_url = models.URLField(_('redakcja URL'), blank=True) book = models.ForeignKey(Book, null=True, blank=True, help_text=_('Published book.')) cover = models.ImageField(_('Cover'), upload_to = 'funding/covers') poll = models.ForeignKey(Poll, help_text = _('Poll'), null = True, blank = True, on_delete = models.SET_NULL) - + + notified_near = models.DateTimeField(_('Near-end notifications sent'), blank=True, null=True) + notified_end = models.DateTimeField(_('End notifications sent'), blank=True, null=True) + def cover_img_tag(self): return u'' % self.cover.url cover_img_tag.short_description = _('Cover preview') @@ -60,7 +62,7 @@ class Offer(models.Model): return retval def is_current(self): - return self.start <= date.today() <= self.end + return self.start <= date.today() <= self.end and self == self.current() def is_win(self): return self.sum() >= self.target @@ -75,9 +77,16 @@ class Offer(models.Model): @classmethod def current(cls): - """ Returns current fundraiser or None. """ + """ Returns current fundraiser or None. + + Current fundraiser is the one that: + - has already started, + - hasn't yet ended, + - if there's more than one of those, it's the one that ends last. + + """ today = date.today() - objects = cls.objects.filter(start__lte=today, end__gte=today) + objects = cls.objects.filter(start__lte=today, end__gte=today).order_by('-end') try: return objects[0] except IndexError: @@ -85,9 +94,12 @@ class Offer(models.Model): @classmethod def past(cls): - """ QuerySet for all current and past fundraisers. """ - today = date.today() - return cls.objects.filter(end__lt=today) + """ QuerySet for all past fundraisers. """ + objects = cls.public() + current = cls.current() + if current is not None: + objects = objects.exclude(pk=current.pk) + return objects @classmethod def public(cls): @@ -112,6 +124,9 @@ class Offer(models.Model): """ QuerySet for all completed payments for the offer. """ return Funding.payed().filter(offer=self) + def funders(self): + return self.funding_payed().order_by('-amount', 'payed_at') + def sum(self): """ The money gathered. """ return self.funding_payed().aggregate(s=models.Sum('amount'))['s'] or 0 @@ -122,7 +137,8 @@ class Offer(models.Model): query_filter=models.Q(offer=self) ) - def notify_end(self): + def notify_end(self, force=False): + if not force and self.notified_end: return assert not self.is_current() self.notify_all( _('The fundraiser has ended!'), @@ -132,8 +148,11 @@ class Offer(models.Model): 'remaining': self.remaining(), 'current': self.current(), }) + self.notified_end = datetime.now() + self.save() - def notify_near(self): + def notify_near(self, force=False): + if not force and self.notified_near: return assert self.is_current() sum_ = self.sum() need = self.target - sum_ @@ -146,6 +165,8 @@ class Offer(models.Model): 'sum': sum_, 'need': need, }) + self.notified_near = datetime.now() + self.save() def notify_published(self): assert self.book is not None @@ -212,6 +233,9 @@ class Funding(models.Model): def get_absolute_url(self): return reverse('funding_funding', args=[self.pk]) + def perk_names(self): + return ", ".join(perk.name for perk in self.perks.all()) + def get_disable_notifications_url(self): return "%s?%s" % (reverse("funding_disable_notifications"), urlencode({ @@ -244,8 +268,9 @@ class Funding(models.Model): 'funding': self, 'site': Site.objects.get_current(), } - context.update(extra_context) - with override(self.language_code or 'pl'): + if extra_context: + context.update(extra_context) + with override(self.language_code or app_settings.DEFAULT_LANGUAGE): send_mail(subject, render_to_string(template_name, context), getattr(settings, 'CONTACT_EMAIL', 'wolnelektury@nowoczesnapolska.org.pl'),