Merge branch 'master' into rwd
[wolnelektury.git] / apps / funding / models.py
index 3b42ea6..de52ac9 100644 (file)
@@ -62,7 +62,7 @@ class Offer(models.Model):
         return retval
 
     def is_current(self):
-        return self.start <= date.today() <= self.end
+        return self.start <= date.today() <= self.end and self == self.current()
 
     def is_win(self):
         return self.sum() >= self.target
@@ -77,9 +77,16 @@ class Offer(models.Model):
 
     @classmethod
     def current(cls):
-        """ Returns current fundraiser or None. """
+        """ Returns current fundraiser or None.
+
+        Current fundraiser is the one that:
+        - has already started,
+        - hasn't yet ended,
+        - if there's more than one of those, it's the one that ends last.
+
+        """
         today = date.today()
-        objects = cls.objects.filter(start__lte=today, end__gte=today)
+        objects = cls.objects.filter(start__lte=today, end__gte=today).order_by('-end')
         try:
             return objects[0]
         except IndexError:
@@ -87,9 +94,12 @@ class Offer(models.Model):
 
     @classmethod
     def past(cls):
-        """ QuerySet for all current and past fundraisers. """
-        today = date.today()
-        return cls.objects.filter(end__lt=today)
+        """ QuerySet for all past fundraisers. """
+        objects = cls.public()
+        current = cls.current()
+        if current is not None:
+            objects = objects.exclude(pk=current.pk)
+        return objects
 
     @classmethod
     def public(cls):
@@ -114,6 +124,9 @@ class Offer(models.Model):
         """ QuerySet for all completed payments for the offer. """
         return Funding.payed().filter(offer=self)
 
+    def funders(self):
+        return self.funding_payed().order_by('-amount', 'payed_at')
+
     def sum(self):
         """ The money gathered. """
         return self.funding_payed().aggregate(s=models.Sum('amount'))['s'] or 0
@@ -220,6 +233,9 @@ class Funding(models.Model):
     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({
@@ -252,7 +268,8 @@ class Funding(models.Model):
             'funding': self,
             'site': Site.objects.get_current(),
         }
-        context.update(extra_context)
+        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),