Add long names to perks.
[wolnelektury.git] / apps / funding / models.py
index 8c1ee9a..8c3312e 100644 (file)
@@ -2,36 +2,68 @@
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
+from datetime import date, datetime
 from django.core.urlresolvers import reverse
 from django.core.urlresolvers import reverse
+from django.core.mail import send_mail
+from django.conf import settings
+from django.template.loader import render_to_string
 from django.db import models
 from django.db import models
-from django.utils.translation import ugettext_lazy as _, ugettext as __
-from datetime import date, datetime
+from django.utils.translation import ugettext_lazy as _, ugettext as __, override
+import getpaid
 from catalogue.models import Book
 from catalogue.models import Book
+from polls.models import Poll
 
 
 class Offer(models.Model):
 
 
 class Offer(models.Model):
+    """ A fundraiser for a particular book. """
     author = models.CharField(_('author'), max_length=255)
     title = models.CharField(_('title'), max_length=255)
     slug = models.SlugField(_('slug'))
     author = models.CharField(_('author'), max_length=255)
     title = models.CharField(_('title'), max_length=255)
     slug = models.SlugField(_('slug'))
-    book = models.ForeignKey(Book, null=True, blank=True)
-    redakcja_url = models.URLField(_('redakcja URL'), blank=True)
+    description = models.TextField(_('description'), blank=True)
     target = models.DecimalField(_('target'), decimal_places=2, max_digits=10)
     target = models.DecimalField(_('target'), decimal_places=2, max_digits=10)
-    start = models.DateField(_('start'))
-    end = models.DateField(_('end'))
-
+    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)
+        
+    def cover_img_tag(self):
+        return u'<img src="%s" />' % self.cover.url
+    cover_img_tag.short_description = _('Cover preview')
+    cover_img_tag.allow_tags = True
+        
     class Meta:
         verbose_name = _('offer')
         verbose_name_plural = _('offers')
         ordering = ['-end']
 
     def __unicode__(self):
     class Meta:
         verbose_name = _('offer')
         verbose_name_plural = _('offers')
         ordering = ['-end']
 
     def __unicode__(self):
-        return u"%s  %s" % (self.author, self.title)
+        return u"%s - %s" % (self.author, self.title)
 
     def get_absolute_url(self):
         return reverse('funding_offer', args=[self.slug])
 
 
     def get_absolute_url(self):
         return reverse('funding_offer', args=[self.slug])
 
+    def is_current(self):
+        return self.start <= date.today() <= self.end
+
+    def is_win(self):
+        return self.sum() >= self.target
+
+    def remaining(self):
+        if self.is_current():
+            return None
+        if self.is_win():
+            return self.sum() - self.target
+        else:
+            return self.sum()
+
     @classmethod
     def current(cls):
     @classmethod
     def current(cls):
+        """ Returns current fundraiser or None. """
         today = date.today()
         objects = cls.objects.filter(start__lte=today, end__gte=today)
         try:
         today = date.today()
         objects = cls.objects.filter(start__lte=today, end__gte=today)
         try:
@@ -39,43 +71,51 @@ class Offer(models.Model):
         except IndexError:
             return None
 
         except IndexError:
             return None
 
+    @classmethod
+    def past(cls):
+        """ QuerySet for all current and past fundraisers. """
+        today = date.today()
+        return cls.objects.filter(end__lt=today)
+
     @classmethod
     def public(cls):
     @classmethod
     def public(cls):
+        """ QuerySet for all current and past fundraisers. """
         today = date.today()
         today = date.today()
-        return cls.objects.filter(start__lte=today)        
+        return cls.objects.filter(start__lte=today)
 
     def get_perks(self, amount=None):
 
     def get_perks(self, amount=None):
+        """ Finds all the perks for the offer.
+        
+        If amount is provided, returns the perks you get for it.
+
+        """
         perks = Perk.objects.filter(
                 models.Q(offer=self) | models.Q(offer=None)
         perks = Perk.objects.filter(
                 models.Q(offer=self) | models.Q(offer=None)
-            )
+            ).exclude(end_date__lt=date.today())
         if amount is not None:
             perks = perks.filter(price__lte=amount)
         return perks
 
         if amount is not None:
             perks = perks.filter(price__lte=amount)
         return perks
 
-    def fund(self, name, email, amount):
-        funding = self.funding_set.create(
-            name=name, email=email, amount=amount,
-            payed_at=datetime.now())
-        funding.perks = self.get_perks(amount)
-        return funding
+    def funding_payed(self):
+        """ QuerySet for all completed payments for the offer. """
+        return Funding.payed().filter(offer=self)
 
     def sum(self):
 
     def sum(self):
-        return self.funding_set.aggregate(s=models.Sum('amount'))['s'] or 0
-
-    def state(self):
-        if self.sum() >= self.target:
-            return 'win'
-        elif self.start <= date.today() <= self.end:
-            return 'running'
-        else:
-            return 'lose'
+        """ The money gathered. """
+        return self.funding_payed().aggregate(s=models.Sum('amount'))['s'] or 0
 
 
 class Perk(models.Model):
 
 
 class Perk(models.Model):
+    """ A perk offer.
+    
+    If no attached to a particular Offer, applies to all.
+
+    """
     offer = models.ForeignKey(Offer, verbose_name=_('offer'), null=True, blank=True)
     price = models.DecimalField(_('price'), decimal_places=2, max_digits=10)
     name = models.CharField(_('name'), max_length=255)
     offer = models.ForeignKey(Offer, verbose_name=_('offer'), null=True, blank=True)
     price = models.DecimalField(_('price'), decimal_places=2, max_digits=10)
     name = models.CharField(_('name'), max_length=255)
-    description = models.TextField(_('description'), blank=True)
+    long_name = models.CharField(_('long name'), max_length=255)
+    end_date = models.DateField(_('end date'), null=True, blank=True)
 
     class Meta:
         verbose_name = _('perk')
 
     class Meta:
         verbose_name = _('perk')
@@ -87,12 +127,25 @@ class Perk(models.Model):
 
 
 class Funding(models.Model):
 
 
 class Funding(models.Model):
+    """ A person paying in a fundraiser.
+
+    The payment was completed if and only if payed_at is set.
+
+    """
     offer = models.ForeignKey(Offer, verbose_name=_('offer'))
     offer = models.ForeignKey(Offer, verbose_name=_('offer'))
-    name = models.CharField(_('name'), max_length=127)
-    email = models.EmailField(_('email'))
+    name = models.CharField(_('name'), max_length=127, blank=True)
+    email = models.EmailField(_('email'), blank=True)
     amount = models.DecimalField(_('amount'), decimal_places=2, max_digits=10)
     amount = models.DecimalField(_('amount'), decimal_places=2, max_digits=10)
-    payed_at = models.DateTimeField(_('payed_at'))
+    payed_at = models.DateTimeField(_('payed at'), null=True, blank=True, db_index=True)
     perks = models.ManyToManyField(Perk, verbose_name=_('perks'), blank=True)
     perks = models.ManyToManyField(Perk, verbose_name=_('perks'), blank=True)
+    language_code = models.CharField(max_length = 2, null = True, blank = True)
+
+    # Any additional info needed for perks?
+
+    @classmethod
+    def payed(cls):
+        """ QuerySet for all completed payments. """
+        return cls.objects.exclude(payed_at=None)
 
     class Meta:
         verbose_name = _('funding')
 
     class Meta:
         verbose_name = _('funding')
@@ -100,13 +153,57 @@ class Funding(models.Model):
         ordering = ['-payed_at']
 
     def __unicode__(self):
         ordering = ['-payed_at']
 
     def __unicode__(self):
-        return "%s payed %s for %s" % (self.name, self.amount, self.offer)
+        return unicode(self.offer)
+
+    def get_absolute_url(self):
+        return reverse('funding_funding', args=[self.pk])
+
+# Register the Funding model with django-getpaid for payments.
+getpaid.register_to_payment(Funding, unique=False, related_name='payment')
 
 
 class Spent(models.Model):
 
 
 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'))
     amount = models.DecimalField(_('amount'), decimal_places=2, max_digits=10)
     timestamp = models.DateField(_('when'))
-    book = models.ForeignKey(Book)
+
+    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 __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:
+            send_thank_you_email(instance.order.name, instance.order.email, instance.order.language_code)
+getpaid.signals.payment_status_changed.connect(payment_status_changed_listener)
+
+def send_thank_you_email(name, address, language_code):
+    with override(language_code or 'pl'):
+        send_mail(_('Thank you for your support!'), 
+                render_to_string('funding/email.txt', dict(name = name)),
+                getattr(settings, 'CONTACT_EMAIL', 'wolnelektury@nowoczesnapolska.org.pl'),
+                [address],
+                fail_silently=False
+                )