1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from datetime import date, datetime
6 from django.core.urlresolvers import reverse
7 from django.db import models
8 from django.utils.translation import ugettext_lazy as _, ugettext as __
10 from catalogue.models import Book
13 class Offer(models.Model):
14 """ A fundraiser for a particular book. """
15 author = models.CharField(_('author'), max_length=255)
16 title = models.CharField(_('title'), max_length=255)
17 slug = models.SlugField(_('slug'))
18 description = models.TextField(_('description'), blank=True)
19 target = models.DecimalField(_('target'), decimal_places=2, max_digits=10)
20 start = models.DateField(_('start'))
21 end = models.DateField(_('end'))
22 due = models.DateField(_('due'),
23 help_text=_('When will it be published if the money is raised.'))
24 redakcja_url = models.URLField(_('redakcja URL'), blank=True)
25 book = models.ForeignKey(Book, null=True, blank=True,
26 help_text=_('Published book.'))
29 verbose_name = _('offer')
30 verbose_name_plural = _('offers')
33 def __unicode__(self):
34 return u"%s – %s" % (self.author, self.title)
36 def get_absolute_url(self):
37 return reverse('funding_offer', args=[self.slug])
40 return self.start <= date.today() <= self.end
44 """ Returns current fundraiser or None. """
46 objects = cls.objects.filter(start__lte=today, end__gte=today)
54 """ QuerySet for all current and past fundraisers. """
56 return cls.objects.filter(start__lte=today)
58 def get_perks(self, amount=None):
59 """ Finds all the perks for the offer.
61 If amount is provided, returns the perks you get for it.
64 perks = Perk.objects.filter(
65 models.Q(offer=self) | models.Q(offer=None)
66 ).exclude(end_date__lt=date.today())
67 if amount is not None:
68 perks = perks.filter(price__lte=amount)
71 def funding_payed(self):
72 """ QuerySet for all completed payments for the offer. """
73 return Funding.payed().filter(offer=self)
76 """ The money gathered. """
77 return self.funding_payed().aggregate(s=models.Sum('amount'))['s'] or 0
80 if self.sum() >= self.target:
82 elif self.start <= date.today() <= self.end:
88 class Perk(models.Model):
91 If no attached to a particular Offer, applies to all.
94 offer = models.ForeignKey(Offer, verbose_name=_('offer'), null=True, blank=True)
95 price = models.DecimalField(_('price'), decimal_places=2, max_digits=10)
96 name = models.CharField(_('name'), max_length=255)
97 description = models.TextField(_('description'), blank=True)
98 end_date = models.DateField(_('end date'), null=True, blank=True)
101 verbose_name = _('perk')
102 verbose_name_plural = _('perks')
103 ordering = ['-price']
105 def __unicode__(self):
106 return "%s (%s%s)" % (self.name, self.price, u" for %s" % self.offer if self.offer else "")
109 class Funding(models.Model):
110 """ A person paying in a fundraiser.
112 The payment was completed if and only if payed_at is set.
115 offer = models.ForeignKey(Offer, verbose_name=_('offer'))
116 name = models.CharField(_('name'), max_length=127, blank=True)
117 email = models.EmailField(_('email'), blank=True)
118 amount = models.DecimalField(_('amount'), decimal_places=2, max_digits=10)
119 payed_at = models.DateTimeField(_('payed at'), null=True, blank=True)
120 perks = models.ManyToManyField(Perk, verbose_name=_('perks'), blank=True)
122 # Any additional info needed for perks?
126 """ QuerySet for all completed payments. """
127 return cls.objects.exclude(payed_at=None)
130 verbose_name = _('funding')
131 verbose_name_plural = _('fundings')
132 ordering = ['-payed_at']
134 def __unicode__(self):
135 return unicode(self.offer)
137 def get_absolute_url(self):
138 return reverse('funding_funding', args=[self.pk])
140 # Register the Funding model with django-getpaid for payments.
141 getpaid.register_to_payment(Funding, unique=False, related_name='payment')
144 class Spent(models.Model):
145 """ Some of the remaining money spent on a book. """
146 book = models.ForeignKey(Book)
147 amount = models.DecimalField(_('amount'), decimal_places=2, max_digits=10)
148 timestamp = models.DateField(_('when'))
151 verbose_name = _('money spent on a book')
152 verbose_name_plural = _('money spent on books')
153 ordering = ['-timestamp']
155 def __unicode__(self):
156 return u"Spent: %s" % unicode(self.book)
159 def new_payment_query_listener(sender, order=None, payment=None, **kwargs):
160 """ Set payment details for getpaid. """
161 payment.amount = order.amount
162 payment.currency = 'PLN'
163 getpaid.signals.new_payment_query.connect(new_payment_query_listener)
166 def payment_status_changed_listener(sender, instance, old_status, new_status, **kwargs):
167 """ React to status changes from getpaid. """
168 if old_status != 'paid' and new_status == 'paid':
169 instance.order.payed_at = datetime.now()
170 instance.order.save()
171 getpaid.signals.payment_status_changed.connect(payment_status_changed_listener)