Funding: behave nicer with time conflicts.
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Tue, 23 Jul 2013 12:05:06 +0000 (14:05 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Tue, 23 Jul 2013 12:05:06 +0000 (14:05 +0200)
apps/funding/models.py
apps/funding/templates/funding/offer_list.html
apps/funding/templatetags/funding_tags.py
apps/funding/urls.py
apps/funding/views.py

index b853181..3a74238 100644 (file)
@@ -62,7 +62,7 @@ class Offer(models.Model):
         return retval
 
     def is_current(self):
         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
 
     def is_win(self):
         return self.sum() >= self.target
@@ -77,9 +77,16 @@ class Offer(models.Model):
 
     @classmethod
     def current(cls):
 
     @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()
         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:
         try:
             return objects[0]
         except IndexError:
@@ -87,9 +94,12 @@ class Offer(models.Model):
 
     @classmethod
     def past(cls):
 
     @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):
 
     @classmethod
     def public(cls):
index 915307d..feb175d 100644 (file)
 
 
 {% autopaginate object_list 10 %}
 
 
 {% autopaginate object_list 10 %}
+{% if page_obj.number == 1 %}
+    <h2>{% trans "Current fundraiser:" %}</h2>
+{% endif %}
+
 {% for offer in object_list %}
 {% with is_win=offer.is_win is_current=offer.is_current %}
 {% for offer in object_list %}
 {% with is_win=offer.is_win is_current=offer.is_current %}
-
-    {% if is_current %}
-        <h2>{% trans "Current fundraiser:" %}</h2>
-    {% elif forloop.is_first %}
+    {% if not is_current  and forloop.first and page_obj.number == 1 %}
+        <p class="normal-text">{% trans "No fundraiser is currently running." %}</p>
         <h2>{% trans "Previous fundraisers:" %}</h2>
     {% endif %}
 
         <h2>{% trans "Previous fundraisers:" %}</h2>
     {% endif %}
 
index eb60601..5c6544a 100755 (executable)
@@ -8,6 +8,10 @@ register = template.Library()
 def funding(context, offer=None, link=False, closeable=False, show_title=True, show_title_calling = True, add_class=""):
     if offer is None and context.get('funding_no_show_current') is None:
         offer = Offer.current()
 def funding(context, offer=None, link=False, closeable=False, show_title=True, show_title_calling = True, add_class=""):
     if offer is None and context.get('funding_no_show_current') is None:
         offer = Offer.current()
+        is_current = True
+    elif offer is not None:
+        is_current = offer.is_current()
+
     if offer is None:
         return {}
 
     if offer is None:
         return {}
 
@@ -15,7 +19,7 @@ def funding(context, offer=None, link=False, closeable=False, show_title=True, s
     return {
         'offer': offer,
         'sum': offer_sum,
     return {
         'offer': offer,
         'sum': offer_sum,
-        'is_current': offer.is_current(),
+        'is_current': is_current,
         'is_win': offer_sum >= offer.target,
         'missing': offer.target - offer_sum,
         'percentage': 100 * offer_sum / offer.target,
         'is_win': offer_sum >= offer.target,
         'missing': offer.target - offer_sum,
         'percentage': 100 * offer_sum / offer.target,
index f05bc1d..3e55ba4 100644 (file)
@@ -12,6 +12,7 @@ from .views import (WLFundView, OfferDetailView, OfferListView,
 urlpatterns = patterns('',
 
     url(r'^$', CurrentView.as_view(), name='funding_current'),
 urlpatterns = patterns('',
 
     url(r'^$', CurrentView.as_view(), name='funding_current'),
+    url(r'^teraz/$', CurrentView.as_view()),
     url(r'^teraz/(?P<slug>[^/]+)/$', CurrentView.as_view(), name='funding_current'),
     url(r'^lektura/$', OfferListView.as_view(), name='funding'),
     url(r'^lektura/(?P<slug>[^/]+)/$', OfferDetailView.as_view(), name='funding_offer'),
     url(r'^teraz/(?P<slug>[^/]+)/$', CurrentView.as_view(), name='funding_current'),
     url(r'^lektura/$', OfferListView.as_view(), name='funding'),
     url(r'^lektura/(?P<slug>[^/]+)/$', OfferDetailView.as_view(), name='funding_offer'),
index 0b84a6c..dbbe219 100644 (file)
@@ -115,7 +115,7 @@ class CurrentView(OfferDetailView):
     def dispatch(self, request, slug=None):
         self.object = Offer.current()
         if self.object is None:
     def dispatch(self, request, slug=None):
         self.object = Offer.current()
         if self.object is None:
-            raise Http404
+            return redirect(reverse('funding'))
         elif slug != self.object.slug:
             return redirect(reverse('funding_current', args=[self.object.slug]))
         return super(CurrentView, self).dispatch(request, slug)
         elif slug != self.object.slug:
             return redirect(reverse('funding_current', args=[self.object.slug]))
         return super(CurrentView, self).dispatch(request, slug)