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
43 return self.sum() >= self.target
49 return self.sum() - self.target
55 """ Returns current fundraiser or None. """
57 objects = cls.objects.filter(start__lte=today, end__gte=today)
65 """ QuerySet for all current and past fundraisers. """
67 return cls.objects.filter(start__lte=today)
69 def get_perks(self, amount=None):
70 """ Finds all the perks for the offer.
72 If amount is provided, returns the perks you get for it.
75 perks = Perk.objects.filter(
76 models.Q(offer=self) | models.Q(offer=None)
77 ).exclude(end_date__lt=date.today())
78 if amount is not None:
79 perks = perks.filter(price__lte=amount)
82 def funding_payed(self):
83 """ QuerySet for all completed payments for the offer. """
84 return Funding.payed().filter(offer=self)
87 """ The money gathered. """
88 return self.funding_payed().aggregate(s=models.Sum('amount'))['s'] or 0
91 class Perk(models.Model):
94 If no attached to a particular Offer, applies to all.
97 offer = models.ForeignKey(Offer, verbose_name=_('offer'), null=True, blank=True)
98 price = models.DecimalField(_('price'), decimal_places=2, max_digits=10)
99 name = models.CharField(_('name'), max_length=255)
100 description = models.TextField(_('description'), blank=True)
101 end_date = models.DateField(_('end date'), null=True, blank=True)
104 verbose_name = _('perk')
105 verbose_name_plural = _('perks')
106 ordering = ['-price']
108 def __unicode__(self):
109 return "%s (%s%s)" % (self.name, self.price, u" for %s" % self.offer if self.offer else "")
112 class Funding(models.Model):
113 """ A person paying in a fundraiser.
115 The payment was completed if and only if payed_at is set.
118 offer = models.ForeignKey(Offer, verbose_name=_('offer'))
119 name = models.CharField(_('name'), max_length=127, blank=True)
120 email = models.EmailField(_('email'), blank=True)
121 amount = models.DecimalField(_('amount'), decimal_places=2, max_digits=10)
122 payed_at = models.DateTimeField(_('payed at'), null=True, blank=True)
123 perks = models.ManyToManyField(Perk, verbose_name=_('perks'), blank=True)
125 # Any additional info needed for perks?
129 """ QuerySet for all completed payments. """
130 return cls.objects.exclude(payed_at=None)
133 verbose_name = _('funding')
134 verbose_name_plural = _('fundings')
135 ordering = ['-payed_at']
137 def __unicode__(self):
138 return unicode(self.offer)
140 def get_absolute_url(self):
141 return reverse('funding_funding', args=[self.pk])
143 # Register the Funding model with django-getpaid for payments.
144 getpaid.register_to_payment(Funding, unique=False, related_name='payment')
147 class Spent(models.Model):
148 """ Some of the remaining money spent on a book. """
149 book = models.ForeignKey(Book)
150 amount = models.DecimalField(_('amount'), decimal_places=2, max_digits=10)
151 timestamp = models.DateField(_('when'))
154 verbose_name = _('money spent on a book')
155 verbose_name_plural = _('money spent on books')
156 ordering = ['-timestamp']
158 def __unicode__(self):
159 return u"Spent: %s" % unicode(self.book)
162 def new_payment_query_listener(sender, order=None, payment=None, **kwargs):
163 """ Set payment details for getpaid. """
164 payment.amount = order.amount
165 payment.currency = 'PLN'
166 getpaid.signals.new_payment_query.connect(new_payment_query_listener)
169 def payment_status_changed_listener(sender, instance, old_status, new_status, **kwargs):
170 """ React to status changes from getpaid. """
171 if old_status != 'paid' and new_status == 'paid':
172 instance.order.payed_at = datetime.now()
173 instance.order.save()
174 getpaid.signals.payment_status_changed.connect(payment_status_changed_listener)