+ @classmethod
+ def payed(cls):
+ """ QuerySet for all completed payments. """
+ return cls.objects.exclude(payed_at=None)
+
+ def __unicode__(self):
+ return unicode(self.offer)
+
+ 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({
+ 'email': self.email,
+ 'key': self.notify_key,
+ }))
+
+ def save(self, *args, **kwargs):
+ if self.email and not self.notify_key:
+ self.notify_key = get_random_hash(self.email)
+ return super(Funding, self).save(*args, **kwargs)
+
+ @classmethod
+ def notify_funders(cls, subject, template_name, extra_context=None,
+ query_filter=None, payed_only=True):
+ funders = cls.objects.exclude(email="").filter(notifications=True)
+ if payed_only:
+ funders = funders.exclude(payed_at=None)
+ if query_filter is not None:
+ funders = funders.filter(query_filter)
+ emails = set()
+ for funder in funders:
+ if funder.email in emails:
+ continue
+ emails.add(funder.email)
+ funder.notify(subject, template_name, extra_context)
+
+ def notify(self, subject, template_name, extra_context=None):
+ context = {
+ 'funding': self,
+ 'site': Site.objects.get_current(),
+ }
+ 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),
+ settings.CONTACT_EMAIL,
+ [self.email],
+ fail_silently=False
+ )
+
+ def disable_notifications(self):
+ """Disables all notifications for this e-mail address."""
+ type(self).objects.filter(email=self.email).update(notifications=False)
+
+
+# Register the Funding model with django-getpaid for payments.
+getpaid.register_to_payment(Funding, unique=False, related_name='payment')
+
+
+class Spent(models.Model):
+ """ Some of the remaining money spent on a book. """
+ book = models.ForeignKey(Book)
+ amount = models.DecimalField(_('amount'), decimal_places=2, max_digits=10)
+ timestamp = models.DateField(_('when'))
+
+ class Meta:
+ verbose_name = _('money spent on a book')
+ verbose_name_plural = _('money spent on books')
+ ordering = ['-timestamp']
+