+ return unicode(self.offer)
+
+ def get_absolute_url(self):
+ return reverse('funding_funding', args=[self.pk])
+
+ 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(),
+ }
+ context.update(extra_context)
+ with override(self.language_code or 'pl'):
+ send_mail(subject,
+ render_to_string(template_name, context),
+ getattr(settings, 'CONTACT_EMAIL', 'wolnelektury@nowoczesnapolska.org.pl'),
+ [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']
+
+ def __unicode__(self):
+ return u"Spent: %s" % unicode(self.book)
+
+
+def new_payment_query_listener(sender, order=None, payment=None, **kwargs):
+ """ Set payment details for getpaid. """
+ payment.amount = order.amount
+ payment.currency = 'PLN'
+getpaid.signals.new_payment_query.connect(new_payment_query_listener)
+
+
+def user_data_query_listener(sender, order, user_data, **kwargs):
+ """ Set user data for payment. """
+ user_data['email'] = order.email
+getpaid.signals.user_data_query.connect(user_data_query_listener)
+
+def payment_status_changed_listener(sender, instance, old_status, new_status, **kwargs):
+ """ React to status changes from getpaid. """
+ if old_status != 'paid' and new_status == 'paid':
+ instance.order.payed_at = datetime.now()
+ instance.order.save()
+ if instance.order.email:
+ instance.order.notify(
+ _('Thank you for your support!'),
+ 'funding/email/thanks.txt'
+ )
+getpaid.signals.payment_status_changed.connect(payment_status_changed_listener)