From: Radek Czajka Date: Tue, 23 Jul 2013 12:05:06 +0000 (+0200) Subject: Funding: behave nicer with time conflicts. X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/09afe5bd72a407c6d6474ccce467b7a9dddda0bf?ds=sidebyside Funding: behave nicer with time conflicts. --- diff --git a/apps/funding/models.py b/apps/funding/models.py index b853181f1..3a74238e7 100644 --- a/apps/funding/models.py +++ b/apps/funding/models.py @@ -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): diff --git a/apps/funding/templates/funding/offer_list.html b/apps/funding/templates/funding/offer_list.html index 915307d81..feb175d7f 100644 --- a/apps/funding/templates/funding/offer_list.html +++ b/apps/funding/templates/funding/offer_list.html @@ -13,12 +13,14 @@ {% autopaginate object_list 10 %} +{% if page_obj.number == 1 %} +

{% trans "Current fundraiser:" %}

+{% endif %} + {% for offer in object_list %} {% with is_win=offer.is_win is_current=offer.is_current %} - - {% if is_current %} -

{% trans "Current fundraiser:" %}

- {% elif forloop.is_first %} + {% if not is_current and forloop.first and page_obj.number == 1 %} +

{% trans "No fundraiser is currently running." %}

{% trans "Previous fundraisers:" %}

{% endif %} diff --git a/apps/funding/templatetags/funding_tags.py b/apps/funding/templatetags/funding_tags.py index eb60601ad..5c6544a8f 100755 --- a/apps/funding/templatetags/funding_tags.py +++ b/apps/funding/templatetags/funding_tags.py @@ -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() + is_current = True + elif offer is not None: + is_current = offer.is_current() + 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, - '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, diff --git a/apps/funding/urls.py b/apps/funding/urls.py index f05bc1d86..3e55ba4b4 100644 --- a/apps/funding/urls.py +++ b/apps/funding/urls.py @@ -12,6 +12,7 @@ from .views import (WLFundView, OfferDetailView, OfferListView, urlpatterns = patterns('', url(r'^$', CurrentView.as_view(), name='funding_current'), + url(r'^teraz/$', CurrentView.as_view()), url(r'^teraz/(?P[^/]+)/$', CurrentView.as_view(), name='funding_current'), url(r'^lektura/$', OfferListView.as_view(), name='funding'), url(r'^lektura/(?P[^/]+)/$', OfferDetailView.as_view(), name='funding_offer'), diff --git a/apps/funding/views.py b/apps/funding/views.py index 0b84a6c52..dbbe21986 100644 --- a/apps/funding/views.py +++ b/apps/funding/views.py @@ -115,7 +115,7 @@ class CurrentView(OfferDetailView): 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)